Click here to Skip to main content
11,496,146 members (707 online)
Click here to Skip to main content

Interact With Desktop when Installing Windows Service

, 31 Aug 2003 286.3K 69
This article will describe how to set the "Allow Service to Interact With Desktop" on a windows service created in .NET
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.

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

 
QuestionOnly works with LocalSystem account? Pin
Anonymous28-Jul-04 12:01
sussAnonymous28-Jul-04 12:01 
AnswerRe: Only works with LocalSystem account? Pin
daDude18-Oct-05 12:06
memberdaDude18-Oct-05 12:06 
GeneralThank you guys Pin
israel@jp-inc.com2-Jun-04 23:41
memberisrael@jp-inc.com2-Jun-04 23:41 
GeneralRe: Thank you guys Pin
Dave Midgley17-Mar-06 1:45
memberDave Midgley17-Mar-06 1:45 
GeneralRe: Thank you guys Pin
73amit10-Oct-07 23:34
member73amit10-Oct-07 23:34 
GeneralThank you, Thank you, Thank you, Thank you,,Thank you. Pin
nekane18-Feb-04 0:39
membernekane18-Feb-04 0:39 
GeneralRe: Thank you, Thank you, Thank you, Thank you,,Thank you. Pin
Anonymous30-Jun-05 13:09
sussAnonymous30-Jun-05 13:09 
GeneralRe: Thank you, Thank you, Thank you, Thank you,,Thank you. Pin
Bested19-Jul-07 2:01
memberBested19-Jul-07 2:01 
GeneralInteract issue Pin
Lord Fonty20-Jan-04 5:41
memberLord Fonty20-Jan-04 5:41 
GeneralThe right way to do it Pin
Anonymous24-Nov-03 22:31
sussAnonymous24-Nov-03 22:31 
GeneralRe: The right way to do it Pin
Gennadiy Sterin15-Mar-04 5:19
memberGennadiy Sterin15-Mar-04 5:19 
GeneralRe: The right way to do it Pin
codeshasyou18-Oct-05 22:50
membercodeshasyou18-Oct-05 22:50 
GeneralRe: The right way to do it Pin
RichNFamous17-Mar-06 2:46
memberRichNFamous17-Mar-06 2:46 
GeneralRe: The right way to do it Pin
We1rd0 Pr0grammer23-Mar-06 17:55
memberWe1rd0 Pr0grammer23-Mar-06 17:55 
I got it to work and converted it to VB.Net in the process.
If you use the AfterInstall event the install rolls back but the service still runs. Next I tried the Committing event and found it to work without rolling back or errors.

Here is my VB code.   I made a class called ServiceOptions to make give the ability to use it with more than one service.

Imports System.Management            '-- This must be added as a Reference in VB first.
Partial Public Class ProjectInstaller
   Private Sub EchoServiceInstaller_Committing(ByVal sender As Object, _
                        ByVal e As System.Configuration.Install.InstallEventArgs) _
                  Handles EchoServiceInstaller.Committing
      Try
         '--   NOTE:   EchoServiceController is simply an instance of the ServiceController class
         '--            that I use to start the service during installation.

         '--      Use the variable name of the ServiceController that you have defined for your
         '--      service.
         ServiceOptions.InteractWithDesktop(EchoServiceController)
      Catch
      End Try
   End Sub
End Class

Public Class ServiceOptions
   Public Shared Function InteractWithDesktop( ByVal serviceController As _
                                    System.ServiceProcess.ServiceController) As String
      InteractWithDesktop = InteractWithDesktop(serviceController, False)
   End Function

   Public Shared Function InteractWithDesktop(ByVal serviceController As _
            System.ServiceProcess.ServiceController, ByVal startService As Boolean) As String
      Dim returnValue As String = ""
      Try
         Dim coOptions As ConnectionOptions = New ConnectionOptions
         coOptions.Impersonation = ImpersonationLevel.Impersonate
         Dim mgmtScope As ManagementScope = New ManagementScope("root\CIMV2", coOptions)
         mgmtScope.Connect()
         Dim wmiService As ManagementObject = Nothing
         wmiService = New ManagementObject( _
                           "Win32_Service.Name='" & serviceController.ServiceName & "'")
         Dim InParam As ManagementBaseObject = wmiService.GetMethodParameters("Change")
         InParam("DesktopInteract") = CObj(True)
         Dim OutParam As ManagementBaseObject = _
                  wmiService.InvokeMethod("Change", InParam, Nothing)

         If (startService = True) Then
            serviceController.Start()
         End If
      Catch ex As Exception
         ReturnValue = ex.ToString
      End Try
      Return returnValue
   End Function
End Class

Hope this helps,
-sng Smile | :)

-- modified at 0:37 Thursday 20th April, 2006

GeneralRe: The right way to do it Pin
RichNFamous27-Mar-06 8:39
memberRichNFamous27-Mar-06 8:39 
GeneralRe: The right way to do it Pin
jvargasr28-Apr-06 10:52
memberjvargasr28-Apr-06 10:52 
GeneralRe: The right way to do it Pin
BhargavaJK30-Jul-06 23:12
memberBhargavaJK30-Jul-06 23:12 
GeneralRe: The right way to do it Pin
Savan Gandhi17-Dec-07 0:41
memberSavan Gandhi17-Dec-07 0:41 
GeneralRe: The right way to do it Pin
cweaver12-Sep-06 21:09
membercweaver12-Sep-06 21:09 
GeneralRe: The right way to do it Pin
LucidObscurity21-Sep-06 16:21
memberLucidObscurity21-Sep-06 16:21 
GeneralRe: The right way to do it Pin
spiffnix7-May-08 12:52
memberspiffnix7-May-08 12:52 
GeneralRe: The right way to do it Pin
ChrisG421-Aug-08 4:24
memberChrisG421-Aug-08 4:24 
GeneralRe: The right way to do it ( Could not find System.Management Namespace. Pin
Anonymous6-Oct-04 2:43
sussAnonymous6-Oct-04 2:43 
GeneralRe: The right way to do it ( Could not find System.Management Namespace. Pin
sheppe12-Nov-04 9:20
membersheppe12-Nov-04 9:20 
GeneralRe: The right way to do it Pin
chakkaradeepcc1-Dec-04 20:53
memberchakkaradeepcc1-Dec-04 20:53 
GeneralRe: The right way to do it Pin
chakkaradeepcc2-Dec-04 21:27
memberchakkaradeepcc2-Dec-04 21:27 
GeneralRe: The right way to do it (This way works) Pin
Anonymous1-Jan-05 7:58
sussAnonymous1-Jan-05 7:58 
GeneralRe: The right way to do it (This way works) Pin
TriggerBG3-Jan-05 6:41
memberTriggerBG3-Jan-05 6:41 
GeneralRe: The right way to do it (This way works) Pin
Sven Buth4-Mar-05 0:43
sussSven Buth4-Mar-05 0:43 
GeneralRe: The right way to do it (This way works) Pin
00zimbo13-Jun-05 5:56
member00zimbo13-Jun-05 5:56 
GeneralRe: The right way to do it (This way works) Pin
Anonymous13-Jun-05 6:39
sussAnonymous13-Jun-05 6:39 
GeneralRe: The right way to do it Pin
Lance C14-Jan-05 5:34
memberLance C14-Jan-05 5:34 
GeneralRe: The right way to do it Pin
corsairX8-Mar-05 23:37
membercorsairX8-Mar-05 23:37 
GeneralRe: The right way to do it Pin
corsairX9-Mar-05 0:13
membercorsairX9-Mar-05 0:13 
GeneralRe: The right way to do it Pin
pithhelmet24-May-05 7:36
memberpithhelmet24-May-05 7:36 
GeneralRe: The right way to do it Pin
Ryan L. Schneider18-Oct-05 11:34
memberRyan L. Schneider18-Oct-05 11:34 
GeneralRe: The right way to do it Pin
andronicm28-Oct-05 23:30
memberandronicm28-Oct-05 23:30 
GeneralRe: The right way to do it Pin
haha6242-Jan-06 5:14
memberhaha6242-Jan-06 5:14 
GeneralRe: The right way to do it (not working after Windows restart) Pin
RichEv11-Jan-06 7:31
memberRichEv11-Jan-06 7:31 
GeneralRe: The right way to do it Pin
Ryan McFarren19-May-06 9:43
memberRyan McFarren19-May-06 9:43 
GeneralRe: The right way to do it Pin
sedamkar22-Jun-06 9:10
membersedamkar22-Jun-06 9:10 
GeneralRe: The right way to do it (does not work in windows server 2003) Pin
BhargavaJK2-Aug-06 19:58
memberBhargavaJK2-Aug-06 19:58 
GeneralRe: The right way to do it Pin
dmihailescu8-Oct-07 13:42
memberdmihailescu8-Oct-07 13:42 
GeneralRe: The right way to do it Pin
ChrisG421-Aug-08 2:22
memberChrisG421-Aug-08 2:22 
GeneralNeed help getting fix to work Pin
jobspyder3-Nov-03 9:23
memberjobspyder3-Nov-03 9:23 
GeneralAnother way Pin
Richard Deeming3-Sep-03 1:05
memberRichard Deeming3-Sep-03 1:05 
GeneralRe: Another way Pin
andronicm28-Oct-05 23:32
memberandronicm28-Oct-05 23:32 
GeneralLeft Out On Purpose Pin
Blake Coverett1-Sep-03 8:38
memberBlake Coverett1-Sep-03 8:38 
GeneralRe: Left Out On Purpose Pin
JOHN111-Sep-03 10:17
memberJOHN111-Sep-03 10:17 
GeneralRe: Left Out On Purpose Pin
Blake Coverett1-Sep-03 17:32
memberBlake Coverett1-Sep-03 17:32 

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