We write a service. Sometimes, to enable a service to start an application, we need to modify the service attributes and check to allow service interaction with the desktop,
You can modify the Registry. We must perform the operation after installation. Therefore, rewrite the OnAfterInstall of Installer.
Protected override void OnAfterInstall (System. Collections. IDictionary savedState ){
RegistryKey rk = Registry. LocalMachine;
String key = @ "SYSTEM \ CurrentControlSet \ Services \" + this. sInstaller. ServiceName;
RegistryKey sub = rk. OpenSubKey (key, true );
Int value = (int) sub. GetValue ("Type ");
If (value <256 ){
Sub. SetValue ("Type", value | 256 );
}
Base. OnAfterInstall (savedState );
}
However, the above method can only be used after restart... To take effect immediately, use the following code to replace it with onAfterInstall.
Set service and desktop Interaction
Private void setserviceappstopinsteract (string serviceName ){
ManagementObject wmiService = new ManagementObject (string. Format ("Win32_Service.Name = '{0}'", serviceName ));
ManagementBaseObject changeMethod = wmiService. GetMethodParameters ("Change ");
ChangeMethod ["Publish topinteract"] = true;
ManagementBaseObject OutParam = wmiService. InvokeMethod ("Change", changeMethod, null );
}
If you want to immediately start the service and call the System. ServiceProcess. ServiceController class for implementation
Modify the Registry during onstart.
[HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \ your service name]
"Type" = dword: 00000010
Key value + 256
For example, 00000010 is 16 + 256 = 272
16 refining is 00000110
C # allow the Service to interact with the desktop: You can directly operate the "service" under "Management Tools:
C # The specific implementation steps for allowing services to interact with the desktop are introduced here. I hope you can learn and understand C # It is helpful to allow services to interact with the desktop.