Click here to Skip to main content
11,930,048 members (39,415 online)
Click here to Skip to main content
Add your own
alternative version


38 bookmarked

Alternative way for Window services to interact with desktop where operating > XP

, 23 Sep 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Alternative way for Window services to interact with desktop where operating > XP

Windows services when interacting with Desktop works fine in XP but when we change the application to Vista or greater than XP, it does not work as we expect.


When we migrate the solution to Windows 7 operating system, it runs in session 0. So you get a message:

blocked by Interactive Service Dialog Detection

This article can be one solution for the above problem.


1) Why do I want the Windows service to interact with Desktop, it's not what they are made for?

Answer: Yes, Exactly! the Windows services are not make to interact with Desktop. But after introducing WCF services which can be hosted in Windows services, we may have the requirement to run the EXE file from the WCF services.

But before going through this article, please read:


Before implementing this solution, please be aware of your requirements and handle all the security problems.


Windows Services

Microsoft Windows services, formerly known as NT services, enables you to create long-running executable applications that run in their own Windows sessions. These services can be automatically started when the computer boots, can be paused and restarted, and do not show any user interface. These features make services ideal for use on a server or whenever you need long-running functionality that does not interfere with other users who are working on the same computer. You can also run services in the security context of a specific user account that is different from the logged-on user or the default computer account. For more information about services and Windows sessions:

Step 1
  • Open Visual Studio 2010
  • File -> New -> Project ->
  • Template VisualC# -> Windows
  • Project -> Windows Service
  • Name : InteractDesktopDemo
  • Ok ß


Step 2

Open Service1.cs, right Click in designer mode and Select AddInstaller .

Set the Properties of the installers, by default we get:

  1. serviceProcessInstaller
  2. serviceInstaller1

Go to Solution Explorer project.


Add the following code:

private void InitializeComponent()
      this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
      this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
      // serviceProcessInstaller1
     if (System.Environment.OSVersion.Version.Major <= 5)
     this.serviceProcessInstaller1.Account =
     this.serviceProcessInstaller1.Password = null;
     this.serviceProcessInstaller1.Username = null;
    // serviceInstaller1
    this.serviceInstaller1.ServiceName = "Service1";
    // ProjectInstaller
   this.Installers.AddRange(new System.Configuration.Install.Installer[] {


It will check for operating system version and if the operating system is greater than Windows XP, then it will ask for username and password and after entering the credentials, the service runs under that system account. If its XP or lesser than XP, the Windows service will run under local system account.

Step 3

Add an installer

Go to Solution Explorer, right click add new item Installer.cs:


And add the following code:

protected override void OnCommitted(IDictionary savedState)
         ServiceController nService = new ServiceController("Service1");
    catch (Exception ie)
       StreamWriter sw;
       sw = File.AppendText("C:\\sashidhar.txt");


The above code handles the oncommitted event which fires after the installation of the EXE file/Windows service. It helps the service to start automatically after installation.

Step 4

Add reference to Microsoft.Win32.TaskScheduler which is useful to create the task scheduler.

DLL can be found at

protected override void OnStart(string[] args)
          if (System.Environment.OSVersion.Version.Major > 5)
              //New Functionality
              // Get the service on the local machine
              TaskService ts = new TaskService();
              // Create a new task definition and assign properties
             TaskDefinition td = ts.NewTask();
             td.Principal.RunLevel = TaskRunLevel.Highest;
             td.RegistrationInfo.Description = "Does something";
            // Create a trigger that will fire the task when you are creating a new one 
            // or changing the task
             td.Triggers.Add(new RegistrationTrigger { Delay = TimeSpan.FromSeconds(1) });
            // Create an action that will launch Notepad whenever the trigger fires
            td.Actions.Add(new ExecAction("notepad"));
            // Register the task in the root folder
           ts.RootFolder.RegisterTaskDefinition(@"ssd", td);
          //For Xp the Windows Service to interact with Desktop
     catch (Exception ie)
       StreamWriter sw;
       sw = File.AppendText("C:\\Sashidhar.txt");


The above code behaves differently depending on the operating system, for operating system lesser than XP or XP. Windows Service will run in the localsystem account and interacting with desktop is checked. While in windows 7, it creates a task in the task scheduller and the task scheduler interacts with desktop. Make sure you run the Windows service in administrator account or the account which has the right to run the task in the task scheduler.

static public void ServiceDesktopPermission()
            ConnectionOptions coOptions = new ConnectionOptions();
            coOptions.Impersonation = ImpersonationLevel.Impersonate;
            // CIMV2 is a namespace that contains all of the core OS and hardware classes.
            // CIM (Common Information Model) which is an industry standard for describing
            // data about applications and devices so that administrators and software
            // management programs can control applications and devices on different
           // platforms in the same way, ensuring interoperability across a network.
           ManagementScope mgmtScope = new ManagementScope(@"root\CIMV2", coOptions);
           ManagementObject wmiService;
           wmiService = new ManagementObject("Win32_Service.Name='" + "Service1" + "'");
           ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
           InParam["DesktopInteract"] = true;
           wmiService.InvokeMethod("Change", InParam, null);
         catch (Exception ex)
             //TODO: Log this error
              StreamWriter sw;
             sw = File.AppendText("C:\\Sashidhar.txt");
             sw.WriteLine("Trying to open Service");


For making the Windows service interact with desktop for operating system Windows XP or lesser.


Open Visual Studio command prompt and type:

installutil <Path of the exe File>

When installing, provide the administrator credentials as shown below:



Open Visual studio command prompt and type:

installutil/u <Path of the exe File>


To be continued! Working with WCF services and WindowServices!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralRe: My vote of 1 Pin
sashidhar21-Sep-10 1:05
membersashidhar21-Sep-10 1:05 
GeneralStill not convinced it's needed. Pin
PIEBALDconsult18-Sep-10 4:11
mvpPIEBALDconsult18-Sep-10 4:11 
GeneralRe: Still not convinced it's needed. Pin
sashidhar18-Sep-10 6:22
membersashidhar18-Sep-10 6:22 
GeneralRe: Still not convinced it's needed. Pin
John Brett20-Sep-10 0:30
memberJohn Brett20-Sep-10 0:30 
GeneralRe: Still not convinced it's needed. Pin
sashidhar20-Sep-10 0:52
membersashidhar20-Sep-10 0:52 
GeneralRe: Still not convinced it's needed. Pin
John Brett20-Sep-10 0:58
memberJohn Brett20-Sep-10 0:58 
GeneralRe: Still not convinced it's needed. Pin
sashidhar20-Sep-10 1:27
membersashidhar20-Sep-10 1:27 
GeneralRe: Still not convinced it's needed. Pin
pzand23-Sep-10 16:14
memberpzand23-Sep-10 16:14 
GeneralRe: Still not convinced it's needed. Pin
sashidhar23-Sep-10 19:55
membersashidhar23-Sep-10 19:55 
GeneralNice Article..! Pin
CoderOnline17-Sep-10 19:12
memberCoderOnline17-Sep-10 19:12 
GeneralRe: Nice Article..! Pin
sashidhar17-Sep-10 21:32
membersashidhar17-Sep-10 21:32 
GeneralRe: Nice Article..! Pin
Member 464497017-Sep-10 23:23
memberMember 464497017-Sep-10 23:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 23 Sep 2010
Article Copyright 2010 by sashidhar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid