question

JamesJ-9502 avatar image
0 Votes"
JamesJ-9502 asked RLWA32-6355 answered

Not getting internet settings windows registry value while running as a service

Hi,

We have one utility application which we need to get the "ProxyOverride" value from HKEY_CURRENT_USER internet settings. When I run the code from desktop application. I am able to get the value from the following windows registry.

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings

But, When I run the application as a service then application is looking into the below windows registry
HKEY_USERS.DEFAULT\Software\Microsoft\Windows\CurrentVersion\Internet Settings

Find the below code snippet for your reference.
const string Subkey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\";
string bypassProxyValue = string.Empty;
using (RegistryKey regKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Default).OpenSubKey(Subkey))
{
string[] keys = regKey.GetValueNames();
bypassProxyValue = regKey.GetValue("ProxyOverride").ToString();
}

Above code is perfectly working when we run from desktop and the same code is not working when we run as a service. Appreciate your quick help.

Thanks

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered

Registry access from a windows service should not use HKCU. Refer to the discussion of HKCU at predefined-keys. Note that it says "This handle should not be used in a service or an application that impersonates different users. Instead, call the RegOpenCurrentUser function.
For more information, see HKEY_CURRENT_USER."

My understanding here is that your service would need to impersonate the user for which the information is desired and then call the RegOpenCurrentUser function so that the thread's token will be used thus avoiding the return of default values.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.