Click here to Skip to main content
11,413,961 members (76,528 online)
Click here to Skip to main content

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

Introduction

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

Background

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:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services

public WindowsServiceInstaller()
{
  // This call is required by the Designer.
  InitializeComponent();
  ServiceInstaller si = new ServiceInstaller();
  si.ServiceName = "WindowsService1"; 
  si.DisplayName = "WindowsService1";
  si.StartType = ServiceStartMode.Manual;
  this.Installers.Add(si);
  ServiceProcessInstaller spi = new ServiceProcessInstaller();
  spi.Account = System.ServiceProcess.ServiceAccount.LocalSystem; 
  spi.Password = null;
  spi.Username = null;
  this.Installers.Add(spi);
  
  // 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));
    }
  }
}

License

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

Share

About the Author

Robert H. Davis II

United States United States
No Biography provided

Comments and Discussions

 
GeneralAn alternative, better solution Pin
FtpDaemon, 23-Apr-11 12:30
memberFtpDaemon23-Apr-11 12:30 
GeneralUse using(RegistryKey cKey = ..) or explicit call cKey.close() Pin
Koprinkov, 3-Feb-11 3:13
memberKoprinkov3-Feb-11 3:13 
QuestionWell Does this Work with Win7 + VS.Net 2010 Pin
MarkWardell, 8-Jan-11 5:28
memberMarkWardell8-Jan-11 5:28 
GeneralMy vote of 5 Pin
Bigdeak, 4-Oct-10 1:19
memberBigdeak4-Oct-10 1:19 
GeneralMy vote of 4 Pin
shanthiram7, 10-Aug-10 11:24
membershanthiram710-Aug-10 11:24 
GeneralMy vote of 5 Pin
usmanbinasif, 28-Jun-10 2:50
memberusmanbinasif28-Jun-10 2:50 
Generalsimplified VB.NET 2005 Pin
Member 4520363, 18-May-09 10:23
memberMember 452036318-May-09 10:23 
GeneralYet another way to do it Pin
Member 966795, 23-Feb-09 2:13
memberMember 96679523-Feb-09 2:13 
GeneralRe: Yet another way to do it Pin
JeyaChithra Sankar, 11-Sep-13 23:59
memberJeyaChithra Sankar11-Sep-13 23:59 
GeneralMoving code to OnCommited to make it work [modified] Pin
bergdoktor, 3-Feb-09 23:09
memberbergdoktor3-Feb-09 23:09 
GeneralRe: Moving code to OnCommited to make it work Pin
rexwang1, 11-Nov-09 23:26
memberrexwang111-Nov-09 23:26 
GeneralYou saved me a major panic just on the brink of a deadline Pin
SimonWilliams, 8-Jul-08 8:43
memberSimonWilliams8-Jul-08 8:43 
General'breadcrumb' Pin
izmoto, 8-Apr-08 2:52
memberizmoto8-Apr-08 2:52 
Questionthis.Installers.Clear(); ? Pin
krishna_mag, 4-Jan-08 2:21
memberkrishna_mag4-Jan-08 2:21 
GeneralReally Awsome! Pin
Sapphire0430, 26-Dec-07 15:37
memberSapphire043026-Dec-07 15:37 
you solve my problem!
thanks a lot

--

http://jjwang0430.blogspot.com
GeneralAbout Exceptions in Windows Service Pin
Pradeep Sattikar, 27-Nov-07 23:23
memberPradeep Sattikar27-Nov-07 23:23 
GeneralAbout Exceptions in Windows Service Pin
Pradeep Sattikar, 27-Nov-07 23:22
memberPradeep Sattikar27-Nov-07 23:22 
GeneralIntractive Service Pin
73amit, 10-Oct-07 20:48
member73amit10-Oct-07 20:48 
GeneralUse the name of the service programaticly Pin
Yitzhak Gootvilig, 14-Aug-07 4:58
memberYitzhak Gootvilig14-Aug-07 4:58 
QuestionCheckbox has no effect ! [modified] Pin
floydus27, 31-Jul-07 5:50
memberfloydus2731-Jul-07 5:50 
AnswerRe: Checkbox has no effect - same thing here [modified] Pin
dmihailescu, 8-Oct-07 13:11
memberdmihailescu8-Oct-07 13:11 
GeneralRe: Checkbox has no effect - same thing here [modified] Pin
Akbar Ali Hussain, 12-Jul-13 9:49
memberAkbar Ali Hussain12-Jul-13 9:49 
AnswerRe: Checkbox has no effect ! [modified] Pin
Pouriya.GH, 29-Apr-12 0:46
memberPouriya.GH29-Apr-12 0:46 
BugStart / Stop Services Pin
Pouriya.GH, 29-Apr-12 7:59
memberPouriya.GH29-Apr-12 7:59 
GeneralHelp!!! Pin
Cammail, 28-May-07 4:27
memberCammail28-May-07 4:27 
GeneralRe: Help!!! Pin
Cammail, 28-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 vb.net Pin
algel, 2-Jun-06 12:00
memberalgel2-Jun-06 12:00 
GeneralRe: Convert to vb.net Pin
ICTech, 22-Jan-07 11:53
memberICTech22-Jan-07 11:53 
Newshttp://united-arab-emirates-airline.tangoing.info/ Pin
http://united-arab-emirates-airline.tangoing.info/, 4-Dec-07 8:35
susshttp://united-arab-emirates-airline.tangoing.info/4-Dec-07 8:35 
GeneralIn my winXP System i have to add this code this.Installers.Clear() Pin
lkaou, 9-Nov-05 21:13
memberlkaou9-Nov-05 21:13 
GeneralIf that doesn't work... Pin
Anonymous, 1-Jan-05 7:59
sussAnonymous1-Jan-05 7:59 
GeneralNot Able to run another application Pin
chakkaradeepcc, 1-Dec-04 21:14
memberchakkaradeepcc1-Dec-04 21:14 
GeneralRe: Not Able to run another application Pin
Seydoux, 5-Oct-05 7:11
memberSeydoux5-Oct-05 7:11 
GeneralRe: Not Able to run another application Pin
Lalit N Dubey, 17-Feb-06 2:39
memberLalit N Dubey17-Feb-06 2:39 
GeneralRe: Not Able to run another application Pin
Lalit N Dubey, 10-May-06 0:11
memberLalit N Dubey10-May-06 0:11 
GeneralAbsolutely delightful! Pin
Anonymous, 31-Aug-04 23:31
sussAnonymous31-Aug-04 23:31 
QuestionOnly works with LocalSystem account? Pin
Anonymous, 28-Jul-04 12:01
sussAnonymous28-Jul-04 12:01 
AnswerRe: Only works with LocalSystem account? Pin
daDude, 18-Oct-05 12:06
memberdaDude18-Oct-05 12:06 
GeneralThank you guys Pin
israel@jp-inc.com, 2-Jun-04 23:41
memberisrael@jp-inc.com2-Jun-04 23:41 
GeneralRe: Thank you guys Pin
Dave Midgley, 17-Mar-06 1:45
memberDave Midgley17-Mar-06 1:45 
GeneralRe: Thank you guys Pin
73amit, 10-Oct-07 23:34
member73amit10-Oct-07 23:34 
GeneralThank you, Thank you, Thank you, Thank you,,Thank you. Pin
nekane, 18-Feb-04 0:39
membernekane18-Feb-04 0:39 
GeneralRe: Thank you, Thank you, Thank you, Thank you,,Thank you. Pin
Anonymous, 30-Jun-05 13:09
sussAnonymous30-Jun-05 13:09 
GeneralRe: Thank you, Thank you, Thank you, Thank you,,Thank you. Pin
Bested, 19-Jul-07 2:01
memberBested19-Jul-07 2:01 
GeneralInteract issue Pin
Lord Fonty, 20-Jan-04 5:41
memberLord Fonty20-Jan-04 5:41 
GeneralThe right way to do it Pin
Anonymous, 24-Nov-03 22:31
sussAnonymous24-Nov-03 22:31 
GeneralRe: The right way to do it Pin
Gennadiy Sterin, 15-Mar-04 5:19
memberGennadiy Sterin15-Mar-04 5:19 
GeneralRe: The right way to do it Pin
codeshasyou, 18-Oct-05 22:50
membercodeshasyou18-Oct-05 22:50 
GeneralRe: The right way to do it Pin
RichNFamous, 17-Mar-06 2:46
memberRichNFamous17-Mar-06 2:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web04 | 2.8.150427.2 | Last Updated 1 Sep 2003
Article Copyright 2003 by Robert H. Davis II
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid