Click here to Skip to main content
Click here to Skip to main content

Interact With Desktop when Installing Windows Service

By , 31 Aug 2003
 

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

About the Author

Robert H. Davis II

United States United States
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionAn exception occurred during the Install phase.memberPouriya.GH28-Apr-12 23:34 
GeneralMy vote of 3member[TEC]Mihail18-Jan-12 6:53 
GeneralMy vote of 5membermartinrj3013-Dec-11 15:22 
GeneralAn alternative, better solutionmemberFtpDaemon23-Apr-11 11:30 
GeneralUse using(RegistryKey cKey = ..) or explicit call cKey.close()memberKoprinkov3-Feb-11 2:13 
QuestionWell Does this Work with Win7 + VS.Net 2010memberMarkWardell8-Jan-11 4:28 
GeneralMy vote of 5memberBigdeak4-Oct-10 0:19 
GeneralMy vote of 4membershanthiram710-Aug-10 10:24 
GeneralMy vote of 5memberusmanbinasif28-Jun-10 1:50 
Generalsimplified VB.NET 2005memberMember 452036318-May-09 9:23 
GeneralYet another way to do itmemberMember 96679523-Feb-09 1:13 
GeneralMoving code to OnCommited to make it work [modified]memberbergdoktor3-Feb-09 22:09 
GeneralRe: Moving code to OnCommited to make it workmemberrexwang111-Nov-09 22:26 
GeneralYou saved me a major panic just on the brink of a deadlinememberSimonWilliams8-Jul-08 7:43 
General'breadcrumb'memberizmoto8-Apr-08 1:52 
Questionthis.Installers.Clear(); ?memberkrishna_mag4-Jan-08 1:21 
GeneralReally Awsome!memberSapphire043026-Dec-07 14:37 
GeneralAbout Exceptions in Windows ServicememberPradeep Sattikar27-Nov-07 22:23 
GeneralAbout Exceptions in Windows ServicememberPradeep Sattikar27-Nov-07 22:22 
GeneralIntractive Servicemember73amit10-Oct-07 19:48 
GeneralUse the name of the service programaticlymemberYitzhak Gootvilig14-Aug-07 3:58 
QuestionCheckbox has no effect ! [modified]memberfloydus2731-Jul-07 4:50 
AnswerRe: Checkbox has no effect - same thing here [modified]memberdmihailescu8-Oct-07 12:11 
AnswerRe: Checkbox has no effect ! [modified]memberPouriya.GH28-Apr-12 23:46 
BugStart / Stop ServicesmemberPouriya.GH29-Apr-12 6:59 
GeneralHelp!!!memberCammail28-May-07 3:27 
GeneralRe: Help!!!memberCammail28-May-07 8:00 
GeneralFYI: Platform SDK way of doing this...member-midiman-12-Dec-06 2:03 
GeneralConvert to vb.netmemberalgel2-Jun-06 11:00 
GeneralRe: Convert to vb.netmemberICTech22-Jan-07 10:53 
Newshttp://united-arab-emirates-airline.tangoing.info/susshttp://united-arab-emirates-airline.tangoing.info/4-Dec-07 7:35 
GeneralIn my winXP System i have to add this code this.Installers.Clear()memberlkaou9-Nov-05 20:13 
GeneralIf that doesn't work...sussAnonymous1-Jan-05 6:59 
GeneralNot Able to run another applicationmemberchakkaradeepcc1-Dec-04 20:14 
GeneralRe: Not Able to run another applicationmemberSeydoux5-Oct-05 6:11 
Hi,
 
By the way did you resolve this problem ? because I have the same problem ?
 
Thanks
 

Laurent
GeneralRe: Not Able to run another applicationmemberLalit N Dubey17-Feb-06 1:39 
GeneralRe: Not Able to run another applicationmemberLalit N Dubey9-May-06 23:11 
GeneralAbsolutely delightful!sussAnonymous31-Aug-04 22:31 
QuestionOnly works with LocalSystem account?sussAnonymous28-Jul-04 11:01 
AnswerRe: Only works with LocalSystem account?memberdaDude18-Oct-05 11:06 
GeneralThank you guysmemberisrael@jp-inc.com2-Jun-04 22:41 
GeneralRe: Thank you guysmemberDave Midgley17-Mar-06 0:45 
GeneralRe: Thank you guysmember73amit10-Oct-07 22:34 
GeneralThank you, Thank you, Thank you, Thank you,,Thank you.membernekane17-Feb-04 23:39 
GeneralRe: Thank you, Thank you, Thank you, Thank you,,Thank you.sussAnonymous30-Jun-05 12:09 
GeneralRe: Thank you, Thank you, Thank you, Thank you,,Thank you.memberBested19-Jul-07 1:01 
GeneralInteract issuememberLord Fonty20-Jan-04 4:41 
GeneralThe right way to do itsussAnonymous24-Nov-03 21:31 
GeneralRe: The right way to do itmemberGennadiy Sterin15-Mar-04 4:19 
GeneralRe: The right way to do itmembercodeshasyou18-Oct-05 21:50 

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130619.1 | Last Updated 1 Sep 2003
Article Copyright 2003 by Robert H. Davis II
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid