barbaris
barbaris

Reputation: 515

Dephi read registry on Windown 7 x64

I have value in registry (Windows 7 x64):

[HKEY_CLASSES_ROOT\.html]
@="ChromeHTML"

Then i read via my ServiceApplication on Delphi 7:

Reg := TRegistry.Create(KEY_ALL_ACCESS or KEY_WOW64_32KEY);
  try
    Reg.RootKey := HKEY_CLASSES_ROOT;

    Reg.OpenKey('.html', False);
    Default := Reg.ReadString('');

I have got htmlfile in my Default variable.

How can i get correct value?

ps: Same code in destop application reads correct value.

Upvotes: 0

Views: 423

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596206

HKEY_CLASSES_ROOT is a merged view of the HKEY_LOCAL_MACHINE\Software\Classes and HKEY_CURRENT_USER\Software\Classes keys, where a value present in HKCU has priority over a corresponding value present in HKLM. By default, a service does not run in the same user account as a desktop app does. So, for the user account that the service is actually running as, either htmlfile exists in that user's HKCU key, or no value exists in HKCU but htmlfile does exist in the HKLM key instead.

In order for the service to see the same data that the desktop app sees, the service has to access the Registry as the same user. You need to either:

  1. configure the service in the SCM to run as that user account.

  2. have the service dynamically impersonate the user account using ImpersonateLoggedOnUser() or similar function, then open that user's HKEY_CLASSES_ROOT key normally.

  3. have the service dynamically obtain a token to the user account, such as from LogonUser() or OpenProcessToken(), then use the LoadUserProfile() and RegOpenUserClassesRoot() functions to access that user's HKEY_CLASSES_ROOT key.

Upvotes: 3

Related Questions