Can service login properties be modified programmatically AFTER installation?

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

Is there a way to programmatically modify the "Log on as" property for
an existing Windows service? I can see the ServiceController class can
be used to start/stop the service. I don't see any classes that appear
to give access into the properties however.

Anyone have any ideas?
 
You could use the WMI namespace to modify settings on a service.

For example, I have a test service that needs to have "Interact With
Desktop" checked, so I use the following code (which I got from a thread on a
CodeProject article) in my Installer Custom Action:

if (System.Environment.OSVersion.Platform == System.PlatformID.Win32NT)
{
ConnectionOptions coOptions = new ConnectionOptions();

coOptions.Impersonation = ImpersonationLevel.Impersonate;

ManagementScope mgmtScope = new
System.Management.ManagementScope(@"root\CIMV2", coOptions);

mgmtScope.Connect();

ManagementObject wmiService;

wmiService = new ManagementObject("Win32_Service.Name='" +
this.serviceInstaller1.ServiceName + "'");

ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");

InParam["DesktopInteract"] = true;

ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam,
null);
}


I'm sure there's probally something like "LogonUser", "LogonDomain",
"LogonPassword" in there as well.

Or if it's a service you're installing, you can use a ServiceInstaller
object to set all this up for you. Search MSDN for a howto.
 
Back
Top