Allow Windows service to interact with desktop

Solution 1:

I'm going to take some liberties in here in trying to interpret your question from keywords. In the future, please spend more time writing your questions so that they make sense to another person who is trying to read and understand them.

There is a checkbox under the Log On tab in the properties window for a Windows service that is called "Allow service to interact with desktop." If you're trying to check that box programmatically, you need to specify the SERVICE_INTERACTIVE_PROCESS flag when you create your service using the CreateService API. (See MSDN).

However, note that as of Windows Vista, services are strictly forbidden from interacting directly with a user:

Important: Services cannot directly interact with a user as of Windows Vista. Therefore, the techniques mentioned in the section titled Using an Interactive Service should not be used in new code.

This "feature" is broken, and conventional wisdom dictates that you shouldn't have been relying on it anyway. Services are not meant to provide a UI or allow any type of direct user interaction. Microsoft has been cautioning that this feature be avoided since the early days of Windows NT because of the possible security risks. Larry Osterman argues why it was always a bad idea. And he is not the only one.

There are some possible workarounds, however, if you absolutely must have this functionality. But I strongly urge you to consider its necessity carefully and explore alternative designs for your service.

Solution 2:

Because the service does not run in the context of a user session, you create a second application to interact with the service.

For example, the Microsoft SQL server has a monitoring tool. This application runs in the user session and connects to the service providing you information on whether the service is running and allowing you to stop and start the database service.

Since that application does run in a user session, you can interact with the desktop through that application.

Solution 3:

You need to add serviceinstaller and write down below code in commited event of serviceinstaller.

using System.Management;
using System.ComponentModel;
using System.Configuration.Install;

private void serviceInstaller1_Committed(object sender, InstallEventArgs e)
{
    ConnectionOptions coOptions = new ConnectionOptions();
    coOptions.Impersonation = ImpersonationLevel.Impersonate;
    ManagementScope mgmtScope = new ManagementScope(@"root\CIMV2", coOptions);
    mgmtScope.Connect();
    ManagementObject wmiService;
    wmiService = new ManagementObject("Win32_Service.Name='" + serviceInstaller1.ServiceName + "'");
    ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
    InParam["DesktopInteract"] = true;
    ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);
}