Click here to Skip to main content
12,064,291 members (45,312 online)
Click here to Skip to main content
Add your own
alternative version


72 bookmarked

Interact With Desktop when Installing Windows Service

, 31 Aug 2003
Rate this:
Please Sign up or sign in to vote.
This article will describe how to set the "Allow Service to Interact With Desktop" on a windows service created in .NET


In this article I explain how to set the "Allow Interact with Desktop" parameter when installing a .Net Windows Service.


I have written many windows services and it always bothered me and my colleagues that the ServiceInstaller class used to install to install Windows services does not give you the ability to set the "Allow Interact with Desktop" attribute for the service. I am assuming that Microsoft left this out for a reason, overlooked it, or is planning it for future releases. As the framework stands now, if you need desktop interaction, you have to either code the service setting into your installation program (WISE, InstallSheild, IndigoRose, etc) or manually set it after the service is installed. To my dismay I could not find any references to this issue on CodeProject or any of the other websites. With no information and no built in functionality, the gauntlet had been laid. The answer is extremely simple, but somehow eluding.

Using the code

Shown below is a copy of a ServiceInstaller constructor method. You will notice I have created the ServiceInstaller and the ServiceProcessInstaller and configured them like normal. To set the "Interact with Desktop" attribute, we have to manually edit the service in the registry. This has to be done after adding the ServiceInstaller and the ServiceProcessInstaller to the Installers container. At this point the registry entries for the service have been created and now we just need to modify the "Type" registry value under the services subkey. The "Type" value is a DWORD or blittable to an int in C#. This integer is used to store different attributes about the service. If the "Type" value is viewed in Hex, the third bit is the "Interact with Desktop" bit. To flip this bit we need to a bitwise OR on the value like so:

10h in decimal is 16
100h in decimal is 256

00010h OR 00100h = 00110h (Here the third digit is flipped )

...or in decimal it is:
16 OR 256 = 272 

I only mention decimal because it is easier to OR the values as decimals. Below you will see the code to do all this. A 8 line solution that is worth a million. Have fun and hopefully this article will keep others like myself from pulling their hair out over such a simple problem. All the services are in the registry under the following key:


public WindowsServiceInstaller()
  // This call is required by the Designer.
  ServiceInstaller si = new ServiceInstaller();
  si.ServiceName = "WindowsService1"; 
  si.DisplayName = "WindowsService1";
  si.StartType = ServiceStartMode.Manual;
  ServiceProcessInstaller spi = new ServiceProcessInstaller();
  spi.Account = System.ServiceProcess.ServiceAccount.LocalSystem; 
  spi.Password = null;
  spi.Username = null;
  // Here is where we set the bit on the value in the registry.
  // Grab the subkey to our service
  RegistryKey ckey = Registry.LocalMachine.OpenSubKey(
    @"SYSTEM\CurrentControlSet\Services\WindowsService1", true);
  // Good to always do error checking!
  if(ckey != null)
    // Ok now lets make sure the "Type" value is there, 
    //and then do our bitwise operation on it.
    if(ckey.GetValue("Type") != null)
      ckey.SetValue("Type", ((int)ckey.GetValue("Type") | 256));


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Robert H. Davis II
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Generalsimplified VB.NET 2005 Pin
Member 452036318-May-09 10:23
memberMember 452036318-May-09 10:23 
GeneralYet another way to do it Pin
Member 96679523-Feb-09 2:13
memberMember 96679523-Feb-09 2:13 
GeneralRe: Yet another way to do it Pin
JeyaChithra Sankar11-Sep-13 23:59
memberJeyaChithra Sankar11-Sep-13 23:59 
GeneralMoving code to OnCommited to make it work [modified] Pin
bergdoktor3-Feb-09 23:09
memberbergdoktor3-Feb-09 23:09 
GeneralRe: Moving code to OnCommited to make it work Pin
rexwang111-Nov-09 23:26
memberrexwang111-Nov-09 23:26 
GeneralYou saved me a major panic just on the brink of a deadline Pin
SimonWilliams8-Jul-08 8:43
memberSimonWilliams8-Jul-08 8:43 
General'breadcrumb' Pin
izmoto8-Apr-08 2:52
memberizmoto8-Apr-08 2:52 
Questionthis.Installers.Clear(); ? Pin
krishna_mag4-Jan-08 2:21
memberkrishna_mag4-Jan-08 2:21 
GeneralReally Awsome! Pin
Sapphire043026-Dec-07 15:37
memberSapphire043026-Dec-07 15:37 
GeneralAbout Exceptions in Windows Service Pin
Pradeep Sattikar27-Nov-07 23:23
memberPradeep Sattikar27-Nov-07 23:23 
GeneralAbout Exceptions in Windows Service Pin
Pradeep Sattikar27-Nov-07 23:22
memberPradeep Sattikar27-Nov-07 23:22 
GeneralIntractive Service Pin
73amit10-Oct-07 20:48
member73amit10-Oct-07 20:48 
GeneralUse the name of the service programaticly Pin
Yitzhak Gootvilig14-Aug-07 4:58
memberYitzhak Gootvilig14-Aug-07 4:58 
QuestionCheckbox has no effect ! [modified] Pin
floydus2731-Jul-07 5:50
memberfloydus2731-Jul-07 5:50 
I've used this code, the check box is successfully set to true but has no effect ? Mad | :mad: I still have to go manually change is state, apply and then change is state again, and finally restart the service to make it work.
Can you explain that behavior, anyone had this problem ? I have included most of the installer code, in case you can point out the error somewhere. I have no clue, is there a Commit action missing or anything ?
Thanks folks. Unsure | :~

using System;

namespace ProbeService
partial class ProjectInstaller : Installer
#region variables
private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller serviceInstaller1;

private System.ComponentModel.IContainer components = null;

protected override void Dispose(bool disposing)
if (disposing && (components != null))

#region Component Designer generated code

private void InitializeComponent()
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
this.serviceProcessInstaller1.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.serviceProcessInstaller1_AfterInstall);
this.serviceInstaller1.ServiceName = "DSD PROBE";
this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
this.serviceInstaller1.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.serviceInstaller1_AfterInstall);
this.Installers.AddRange(new System.Configuration.Install.Installer[] {



#region override methods
public override void Install(IDictionary mySavedState)

string SocketNumber = Context.Parameters["SocketNumber"];
string assemblypath = Context.Parameters["assemblypath"];
string appConfigPath = assemblypath + ".config";
XmlDocument doc = new XmlDocument();
XmlNode configuration = null;
foreach (XmlNode node in doc.ChildNodes)
if (node.Name == "configuration")
configuration = node;
if (configuration != null)
XmlNode settingNode = null;
foreach (XmlNode node in configuration.ChildNodes)
if (node.Name == "appSettings")
settingNode = node;

if (settingNode != null)
XmlNode NumNode = null;
foreach (XmlNode node in settingNode.ChildNodes)
if (node.Attributes["key"] != null)
if (node.Attributes["key"].Value == "SocketNumber")
NumNode = node;

if (NumNode != null)
XmlAttribute att = NumNode.Attributes["value"];
att.Value = SocketNumber; // Update the configuration file
catch (FormatException e)
string s = e.Message;
catch (Exception)

protected override void OnAfterInstall(IDictionary mySavedState)

RegistryKey ckey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\DSD PROBE", true); :wtf:
if (ckey != null)
if (ckey.GetValue("Type") != null)
ckey.SetValue("Type", ((int)ckey.GetValue("Type") | 256));
Process proc = new Process();
ProcessStartInfo pi = new ProcessStartInfo();
pi.FileName = "cmd";
pi.UseShellExecute = false;
pi.RedirectStandardInput = true;
pi.RedirectStandardOutput = true;
proc.StartInfo = pi;
proc.StandardInput.WriteLine("net start \"DSD PROBE\"");

catch (Exception)

protected override void OnBeforeUninstall(IDictionary mySavedState)

try :^)
Process proc = new Process();
ProcessStartInfo pi = new ProcessStartInfo();
pi.FileName = "cmd";
pi.UseShellExecute = false;
pi.RedirectStandardInput = true;
pi.RedirectStandardOutput = true;
proc.StartInfo = pi;
proc.StandardInput.WriteLine("net stop \"DSD PROBE\"");
catch (Exception)

public override void Rollback(IDictionary mySavedState)

public override void Uninstall(IDictionary savedState)

"We can't solve problems by using the same kind of thinking we used when we created them." - Albert Einstein

-- modified at 10:59 Tuesday 31st July, 2007
AnswerRe: Checkbox has no effect - same thing here [modified] Pin
dmihailescu8-Oct-07 13:11
memberdmihailescu8-Oct-07 13:11 
GeneralRe: Checkbox has no effect - same thing here [modified] Pin
Akbar Ali Hussain12-Jul-13 9:49
memberAkbar Ali Hussain12-Jul-13 9:49 
AnswerRe: Checkbox has no effect ! [modified] Pin
Pouriya.GH29-Apr-12 0:46
memberPouriya.GH29-Apr-12 0:46 
BugStart / Stop Services Pin
Pouriya.GH29-Apr-12 7:59
memberPouriya.GH29-Apr-12 7:59 
GeneralHelp!!! Pin
Cammail28-May-07 4:27
memberCammail28-May-07 4:27 
GeneralRe: Help!!! Pin
Cammail28-May-07 9:00
memberCammail28-May-07 9:00 
GeneralFYI: Platform SDK way of doing this... Pin
-midiman-12-Dec-06 3:03
member-midiman-12-Dec-06 3:03 
GeneralConvert to Pin
algel2-Jun-06 12:00
memberalgel2-Jun-06 12:00 
GeneralRe: Convert to Pin
ICTech22-Jan-07 11:53
memberICTech22-Jan-07 11:53 
News Pin 8:35
suss 8:35 
GeneralIn my winXP System i have to add this code this.Installers.Clear() Pin
lkaou9-Nov-05 21:13
memberlkaou9-Nov-05 21:13 

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
Web01 | 2.8.160204.4 | Last Updated 1 Sep 2003
Article Copyright 2003 by Robert H. Davis II
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid