C # Allow service and desktop Interaction

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.