25

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

Palec
  • 12,743
  • 8
  • 69
  • 138
user514989
  • 415
  • 1
  • 5
  • 5
  • @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. – ProgrammingLlama Jun 03 '20 at 02: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. – Palec Jun 03 '20 at 08:12

3 Answers3

40

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.

StackzOfZtuff
  • 2,534
  • 1
  • 28
  • 25
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 1
    +1 Great write-up, especially considering the lack of info from the OP – mandreko Aug 19 '11 at 12:34
  • 2
    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... – vlabatut Jul 20 '18 at 15:02
8

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.

Pieter van Ginkel
  • 29,160
  • 8
  • 71
  • 111
  • i have timer and notifyicon in windows service where warning with notify in taskabar Context menu not enable – user514989 Nov 21 '10 at 09:55
  • 1
    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. – BrutalDev Feb 26 '15 at 21:29
5

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);
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291