« links for 2006-02-16 | 回到主頁面 | 用XMLSPY與VS2005來做XML 編輯與偵錯 »

February 17, 2006

"Windows Service" and "Registry key"


將此網頁加入【百度收藏】... 加入此網頁到【del.icio.us 書籤】 technorati

WindowsService (WinCE).JPG

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".



VC6相關問題

由 Evan 發表於 February 17, 2006 將此網頁加入【百度收藏】... 加入此網頁到【del.icio.us 書籤】 technorati
引用
本文的引用網址:


以下是前來引用的連結 '"Windows Service" and "Registry key"' 來自 Blog E
迴響

我在开发中也遇到这样的问题,但是调用
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  即可

Evan 發表於 April 6, 2007 11:58 PM
發表迴響









記住我的資訊?




(請輸入以下的驗證碼)