Sample change methods in windows that allow services to interact with the desktop

  • 2020-11-30 08:14:04
  • OfStack

Add the following methods to ProjectInstaller, the installation class for Windows services:


protected override void OnCommitted(System.Collections.IDictionary savedState)
{
base.OnCommitted(savedState);
// Change the service to allow desktop interaction 
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=' Here is the current service name '");
ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
InParam["DesktopInteract"] = true;
ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);
}


Related articles: