Click to See Complete Forum and Search --> : Services and the registry


TerryB
November 17th, 2003, 06:10 AM
I'm trying to start a DCOM server. The first thing this does is to read some configuration data from the registry using RegOpenKeyEx. I want to be able to start this both manually from an icon, and from a service when the machine starts.

When I start the server from a manual 'kick-start' program it's fine.

When I try to start from a service it fails. RegOpenKeyEx returns 2, which appears to be 'file not found'.

We've put our keys in both the HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER sections of the registry, and get the same result.

I presume that the service is running in some sort of user role which cannot access the registry?

Does anyone know where keys should be placed to be read from a service started program?

TerryB
November 18th, 2003, 09:52 AM
For anyone interested... the answer is put it in HKEY_LOCAL_MACHINE, NOT HKEY_CURRENT_USER.

vicodin451
November 18th, 2003, 10:02 AM
Originally posted by TerryB
We've put our keys in both the HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER sections of the registry, and get the same result.



For anyone interested... the answer is put it in HKEY_LOCAL_MACHINE, NOT HKEY_CURRENT_USER.

Contradiction? :D

When running from a service, the credentials used are likely those of "LocalSystem". As such, there is no HKCU hive mapped for said user.
MSDN, "LocalSystem Account":

A service that runs in the context of the LocalSystem account inherits the security context of the SCM. The user SID is created from the SECURITY_LOCAL_SYSTEM_RID value. The account is not associated with any logged-on user account. This has several implications:

-The registry key HKEY_CURRENT_USER is associated with the default user, not the current user. To access another user's profile, impersonate the user, then access HKEY_CURRENT_USER

TerryB
November 18th, 2003, 10:10 AM
Not a contradiction, a mistake.

The build we thought had HKLM (nice abbreviation) in, didn't.

vicodin451
November 18th, 2003, 10:14 AM
Originally posted by TerryB
Not a contradiction, a mistake.

The build we thought had HKLM (nice abbreviation) in, didn't.
'twas the main reason I didn't reply in the first place... I figured that if HKLM wasn't working, something else was going on.