To create a Windows service and a installer you just have to follow the instructions in this article. http://support.microsoft.com/kb/317421
In order to change the StartMode and start the service with the installer I found the following way.
Open your installer file, in my case ProjectInstaller.cs and implement the Committed event, see code below
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
private void ProjectInstaller_Committed(object sender, InstallEventArgs e)
{
ServiceStartModeUpdate(this.serviceInstaller1.ServiceName, "automatic");
ServiceController sc = new ServiceController( this.serviceInstaller1.ServiceName );
sc.Start();
}
public bool ServiceStartModeUpdate(string serviceName, string startMode)
{
uint success = 1;
string filter = String.Format("SELECT * FROM Win32_Service WHERE Name = '{0}'", serviceName);
ManagementObjectSearcher query = new ManagementObjectSearcher(filter);
if (query == null) return false;
ManagementObjectCollection services = query.Get();
foreach (ManagementObject service in services)
{
ManagementBaseObject inParams = service.GetMethodParameters("ChangeStartMode");
inParams["startmode"] = startMode;
ManagementBaseObject outParams = service.InvokeMethod("ChangeStartMode", inParams, null);
success = Convert.ToUInt16(outParams.Properties["ReturnValue"].Value);
}
return (success == 0);
}
}
I hope you liked this post,
Bart
Geen opmerkingen:
Een reactie posten