views:

505

answers:

5

I have a service that needs to update the registry every 5 minutes (counteract gpo). The code runs fine in a regular application, but when I put it in a windows service, it doesn't make the changes. I am using the local system account for the service and it is not throwing any exceptions

The code below works in a regular console app but not in the service:

RegistryKey key = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", true);
        if (key != null)
        {
            key.SetValue("ScreenSaverIsSecure", "0", RegistryValueKind.String);
            key.SetValue("ScreenSaveActive", "0", RegistryValueKind.String);
            key.SetValue("ScreenSaveTimeOut", "0", RegistryValueKind.String);
            key.SetValue("SCRNSAVE.EXE", "", RegistryValueKind.String);
        }


        key = Registry.CurrentUser.OpenSubKey(@"Software\Policies\Microsoft\Windows\Control Panel\Desktop", true);
        if (key != null)
        {
            key.SetValue("ScreenSaverIsSecure", "0", RegistryValueKind.String);
            key.SetValue("ScreenSaveActive", "0", RegistryValueKind.String);
            key.SetValue("ScreenSaveTimeOut", "0", RegistryValueKind.String);
            key.SetValue("SCRNSAVE.EXE", "", RegistryValueKind.String);
        }
        System.Diagnostics.Process.Start(@"c:\windows\System32\RUNDLL32.EXE", "user32.dll, UpdatePerUserSystemParameters");

Any Ideas?

EDIT:

Thanks for the replies. I don't know why the "CurrentUser" escaped my attention. Thanks for pointing that out.

My problem still remains that the group policy is being pushed against the currentuser. Right now I am considering just creating an app that loads at startup and stays active. Any other suggestions would be welcome.

EDIT:

As to Will's comment, I am unable to find the UpdatePerUserSystemParameters function signature in the win32 api. Does that imply that it can be called without parameters?

   [System.Runtime.InteropServices.DllImport("user32.dll")]
    private static extern bool UpdatePerUserSystemParameters();
+2  A: 

Registry is always fun, but doesn't that mean that you are editing the user registry settings of the "local system" user? So unless your actual user is logged in as "local system" (which I seriously doubt), they aren't going to see anything...

I suspect you need to edit the machine-wide settings, or the active user(s).

Marc Gravell
+1  A: 

try putting the registry manipulation code in a try-catch block; services run in secondary threads, which eat exceptions

Steven A. Lowe
+2  A: 

Run your service under another account that actually has a login.

The Local System account isn't a user, and therefore there is no Registry.CurrentUser key (no login = no current user = no current user key.

Ken White
+2  A: 

If it works outside of the service, it may be a permissions issue. Are you getting an exception when you try to use key.SetValue()? Is the service running under an account that has write access to the registry?

David Lively
+4  A: 

See this page:

A service that runs in the context of the LocalSystem account inherits the security context of the SCM...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.

Stuart Dunkeld