There are something we should keep in mind when we tried to write a Windows Service. The most important is the "Registry Key".
In Windows programming, we usually use registry key to setting some value for difference user. Regstry key control become more and more in your code, and we also usually read the specific user in "HKEY_CURRENT_USER" first then open generic "HKEY_LOCAL_MACHINE" .
Here comes the problem, According this article(Services and the Registry) in MSDN. We should not use "HKEY_CURRENT_USER" to retrival current user's registry key value. Because Windows Services always startup before user login. It may happen some error or loading the wrong setting profile. If you still insist on using the current user registry key setting, please refer "RegOpenCurrentUser". In my experience use "RegQueryValueEx" with "HKEY_CURRENT_USER" will retrival the RootUser's setting. It make me confuse.
So the better way the use registry key in Windows Service is handle the "HKEY_LOCAL_MACHINE" rather than "HKEY_CURRENT_USER".
我在开发中也遇到这样的问题,但是调用
HKEY hKey = HKEY_CURRENT_USER;
RegOpenCurrentUser(KEY_ALL_ACCESS, &hKey);
还是将新建的key值写在了HKEY_USER\.Default下面。
难道是要在每次调用RegCreateKeyEx or RegSetValueEx前都要重新调用RegOpenCurrentUser吗?
希望你能够帮忙解答,thanks very much
由 kane 發表於 April 2, 2007 07:52 PM正常來說
你直接調用 RegCreateKeyEx 就可以讀取到你要用的registry key 才對
如果~ 你每次新建的key 都是在HKEY_USER
試試看以下程序
RegOpenKeyEx(HKEY_CURRENT_USER, ,,,);
再去調用 RegCreateKeyEx 即可