Window services with enhanced features

If you click on the window service and then go to the properties and then go to the tab "log on" there is a option whether you want ur window service to interact with desktop or not. This window service will interact programmatically with desktop.

Here is the sample code

  • After adding the serviceInstaller and serviceProcessinstaller. This is the event of projectInstaller, write this code.

private void ProjectInstaller_Committed(object sender, InstallEventArgs e)
{

    serviceController.ServiceName = "Service1";

    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.serviceController.ServiceName + "'");

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

    InParam["DesktopInteract"] = true; 

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

    this.serviceController.Start();
}

Ebook Download
View all
Learn
View all