Allow Windows service to interact with desktop

user514989 picture user514989 · Nov 21, 2010 · Viewed 63k times · Source

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

Answer

Cody Gray picture Cody Gray · Nov 21, 2010

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.