30

How do I enable "Allow service to interact with desktop" programmatically?

In services.msc > Action > Properties > Log On > Allow service to interact with desktop, I can enable my service to interact with the desktop. I want my service to play sound (MP3, WAV, etc.).

services.msc > Action > Properties > Log On > Allow service to interact with desktop

2
  • @Palec Your edit completely changed the meaning of this question. The question originally asked how to enable "Allow service to interact with desktop" programmatically. Your edit then changed the question to ask what this allows you to do. Commented Jun 3, 2020 at 2:39
  • @John, as I wrote in the edit description, I just added info from the comments under the question by the OP and possibly some others that aimed at clarification of the question. It revealed the intention behind this question. I agree that it shifted the question a bit, I did not get the original requirement of programmatic change. Will try to add the information back in the question while preserving the main idea of the question. Commented Jun 3, 2020 at 8:12

3 Answers 3

45

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.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 Great write-up, especially considering the lack of info from the OP
So how to you run GUI testing services (such as Selenium) ? interact with desktop is very useful for testing with real browsers (I mean no headless), without having to leave a session open... Browsers seem to be not stable enough when in pure service mode...
11

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.

2 Comments

i have timer and notifyicon in windows service where warning with notify in taskabar Context menu not enable
I have a good example of why I need this enabled. My service can execute custom user scripts. Sometimes the applications in these scripts have an application UI and no corresponding console version. Without this option enabled, the applications cannot launch and the scripts essentially fail. It's not that I personally have coded the service in a way where I require the option, this is a genuine use case for having it enabled.
6

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);
}

1 Comment

Can you please explain how this code will enable 'interact with service'?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.