简体   繁体   English

Windows服务从虚拟注册表中读取

[英]Windows service reading from virtualized registry

Please correct me if I'm wrong on this, as I'm certain I read it somewhere: Registry virtualization is disabled for Windows Services. 如果我错了,请更正我,因为我敢肯定我在某处读过:Windows服务已禁用注册表虚拟化。 Also, virtualization is disabled for x64 binaries. 此外,对于x64二进制文件,虚拟化被禁用。

I have a windows service written in C# that needs to load a value from HKEY_LOCAL_MACHINE\\SOFTWARE. 我有一个用C#编写的Windows服务,需要从HKEY_LOCAL_MACHINE \\ SOFTWARE加载值。 When I compile the service as x86 and start it, it fails to read the value. 当我将服务编译为x86并启动它时,它无法读取该值。 When I compile the same code as x64 and start it, it reads the value just fine. 当我编译与x64相同的代码并启动它时,它将读取该值。 I want my service to be able to run on x86 machines in addition to x64 machines, but it needs to also be able to read this value. 我希望我的服务能够在x64机器之外的x86机器上运行,但是它还必须能够读取此值。 How do I get my windows service to read the value non-virtualized? 如何获取Windows服务以读取非虚拟值?

Registry virtualization is disabled for Windows Services. Windows服务禁用了注册表虚拟化。 Also, virtualization is disabled for x64 binaries. 此外,对于x64二进制文件,虚拟化被禁用。

Yes, you're right, it's disabled for 64 bit processes and for " processes that are not interactive, such as services ". 是的,是的,它对于64位进程和“ 非交互式进程,例如服务 ”已禁用。

Take a look to my previous post here on SO for some details about registry views. 看看我以前关于SO的文章,了解有关注册表视图的一些详细信息。 Please do not confuse views with virtualization . 请不要将视图虚拟化混淆。 They're different. 他们不同。 Views are needed to isolate 32-bit application from 64-bit applications. 需要使用视图将32位应用程序与64位应用程序隔离。

In C# you can do the same simply asking the OS to open the 64 bit version of the registry: 在C#中,您可以简单地要求操作系统打开64位版本的注册表来执行以下操作:

RegistryKey baseKey = RegistryKey.OpenBaseKey(
    RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey key = baseKey.OpenSubKey("Software", false);
object value = key.GetValue("");

If your application/service will run in a 32-bit machine you'll always get the normal registry even if you ask for RegistryView.Registry64 . 如果您的应用程序/服务将在32位计算机上运行,​​即使您要求RegistryView.Registry64您也将始终获得常规注册表。 When running in a 64-bit machine you'll always get the normal version of the registry (= 64-bit version). 在64位计算机上运行时,您将始终获得注册表的正常版本(= 64位版本)。

Side question: why do you need to compile your application as X86 or X64? 附带问题:为什么需要将应用程序编译为X86或X64? Can't simply keep AnyCPU? 不能简单地保留AnyCPU?

Virtualization has a different purpose and it's not intended to separate the registry from 32 bit applications but to increase compatibility with legacy applications, we all hope that a 64 bit native application will use the registry in the correct way. 虚拟化的目的不同,它并不是要将注册表与32位应用程序分开,而是要增强与传统应用程序的兼容性,我们都希望64位本机应用程序将以正确的方式使用注册表。 To access the non-virtualized version of the registry you have to open the registry key using REG_KEY_DONT_VIRTUALIZE flag. 要访问注册表的非虚拟版本,您必须使用REG_KEY_DONT_VIRTUALIZE标志打开注册表项。 It's not possible to use this flag with Microsoft.Win32 classes so you have to DllImport them and P/Invoke. 不能在Microsoft.Win32类中使用此标志,因此必须DllImport它们和P / Invoke。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM