使用Windows服务读取注册表 [英] Reading registry with windows service

查看:316
本文介绍了使用Windows服务读取注册表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用XYNTService项目创建我的服务.运行此服务时,在此服务中,我正在使用项目 Registry Wrapper Class(CRegistry)读取位于HKEY_CURRENT_USER内部的一些注册表值. [ ^ ].但是我无法在Windows服务中读取它们.如果我创建任何MFC应用程序,然后读取该注册表,则可以读取它.还有一件事,我的服务XYNTService.exe在TaskManager中以"SYSTEM"身份运行可能是问题所在.如果这是问题所在,那么我应该在哪里进行更改以在XYNTService项目中以管理员"(我的当前用户名)身份运行它.

谢谢
Rahul

Hi,
I am using XYNTService project to create my service. When this service runs, inside this service I am reading some registry value located inside HKEY_CURRENT_USER using the project Registry Wrapper Class (CRegistry)[^]. But I am not able to read them inside windows service. If I create any MFC application and then read that registry then I am able to read it. One more thing, my service XYNTService.exe is running as "SYSTEM" inside TaskManager can that be the problem. If that is the problem then where should I make change to run it as "Administrator" [my current user name] in XYNTService project.

Thanks
Rahul

推荐答案

您是对的:问题仍然在于服务的运行方式.
您可以通过两种方式解决问题:


  • 使服务在已登录用户的上下文中运行,并在创建服务时添加标志SERVICE_INTERACTIVE_PROCESS(或在服务控制管理器中选中相应的复选框) /li>
  • 使服务在特定用户的上下文中运行:在这种情况下,当您访问注册表子树HKEY_CURRENT_USER时,您所访问的子树不是当前登录用户的子树,而是与您所使用的帐户相关的子树.选择在您的服务上运行
You are right: the problem stays on how your service is running.
You can fix your problem in two ways:


  • make the service run in the context of the logged-on user and add the flag SERVICE_INTERACTIVE_PROCESS when you create the service (or check the correspondant check-box from the Sevice Control Manager)
  • make the service run in the context of a specific user: in this case when you access the registry subtree HKEY_CURRENT_USER you are accessing the subtree not of the currently logged-on user, but the subtree related to the account you chose to run your service on


我已经有了该标志.这是我使用的代码

I already have that flag. Here is the code which I use

SC_HANDLE schService = CreateService
	( 
		schSCManager,	/* SCManager database      */ 
		pName,			/* name of service         */ 
		pName,			/* service name to display */ 
		SERVICE_ALL_ACCESS,        /* desired access          */ 
		SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS , /* service type            */ 
		SERVICE_AUTO_START,      /* start type              */ 
		SERVICE_ERROR_NORMAL,      /* error control type      */ 
		pPath,			/* service''s binary        */ 
		NULL,                      /* no load ordering group  */ 
		NULL,                      /* no tag identifier       */ 
		NULL,                      /* no dependencies         */ 
		NULL,                      /* LocalSystem account     */ 
		NULL
		); 



如何在当前用户的上下文中运行服务?



How to run service in current user''s context?


是的!我发现了为什么我无法读取注册表的问题.

当我在RegOpenKeyEx或CRegistry :: KeyExists中指定HKEY_CURRENT_USER时,服务正在HKEY_USERS \ .default \
中搜索
我仍然不知道为什么这样做,但是我只能读取HKEY_USERS \ .default \中的条目.

但是,如果我指定HKEY_LOCAL_MACHINE,那么我仍然不知道它在哪里搜索.
现在,我对在所有PC上的行为都一样吗?"感到好奇.

如果我指定
Yuppy!! I found the problem why I was not able to read registry.

When I was specifying HKEY_CURRENT_USER in either RegOpenKeyEx or CRegistry::KeyExists then Service was searching inside HKEY_USERS\.default\

I still don''t know why it is doing so but I am able to read only those entries which come inside HKEY_USERS\.default\.

But if I specify HKEY_LOCAL_MACHINE then I still don''t know where it searches.
Now I am curious about "Will it behave same in all PCs?"

If I specify
("HKEY_CURRENT_USERS","Softwares\\Microsoft")

,则它将搜索

then it searches for

inside

HKEY_USERS\.default\Softwares\Microsoft



谢谢,
拉胡尔(Rahul)



Thanks,
Rahul


这篇关于使用Windows服务读取注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆