Service Login -- API to Change it?

  • Thread starter Thread starter Camel Software
  • Start date Start date
C

Camel Software

I'd like to write a C# program that can change the login for a service
that's already installed. I'm wondering if there is an API to use for that.
I'm familiar with ServiceController, but I don't see a way to use it.

Is it possible?

If so, how should I approach it?

Thanks,
Steve Harclerode
 
I you talk about Web Service you probably can implement HttpModule with you
login method.
 
Sorry, I guess I should have said that I'm talking about a Windows Service,
not a web service.

- Steve
 
ChangeServiceConfig (or ChangeServiceConfig2)
AdvApi32.dll

-Rob Teixeira [MVP]
 
Use the System.Management classes with the WMI Win32_Service class to change
service properties.
Check the Platform SDK -Windows Management Instrumentation for details.

Here's a sample...

public static int ChangeService(string Name, string Password, string
serviceName)
{
ManagementBaseObject inParams = null;
ManagementObject srvc = new ManagementObject("Win32_Service=" + "\"" +
serviceName + "\"");
inParams = srvc.GetMethodParameters("Change");
inParams["StartName"] = Name; //account name .\\localaccount;
domain\\domainaccount (or account@somedomain on XP and up)
inParams["StartPassword"] = Password; // password
ManagementBaseObject outParams = srvc.InvokeMethod("Change", inParams,
null);
return System.Convert.ToInt32(outParams.Properties["ReturnValue"].Value);
}
}

Willy.
 
Thank you! Is WMI the recommended method to do this sort of thing now? Rob
mentioned ChangeServiceConfig(), but I'm guessing that someday it will be
considered the "old" way.

- Steve
 
WMI is a framework to provide for unified and remote (and neat scriptable)
system administration and management.
There is overhead in using WMI vs. a native Windows export function, but I
don't think either will entirely supplant the other. They both have their
advantages, and I don't see either going away any time soon.

-Rob Teixeira [MVP]
 
Back
Top