Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C#
Article

Interact With Desktop when Installing Windows Service

Rate me:
Please Sign up or sign in to vote.
4.48/5 (34 votes)
31 Aug 20032 min read 411.7K   72   97
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

C#
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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: The right way to do it Pin
cweaver12-Sep-06 20:09
cweaver12-Sep-06 20:09 
GeneralRe: The right way to do it Pin
LucidObscurity21-Sep-06 15:21
LucidObscurity21-Sep-06 15:21 
GeneralRe: The right way to do it Pin
spiffnix7-May-08 11:52
spiffnix7-May-08 11:52 
GeneralRe: The right way to do it Pin
ChrisG421-Aug-08 3:24
ChrisG421-Aug-08 3:24 
GeneralRe: The right way to do it ( Could not find System.Management Namespace. Pin
jagbhag6-Oct-04 1:43
jagbhag6-Oct-04 1:43 
GeneralRe: The right way to do it ( Could not find System.Management Namespace. Pin
sheppe12-Nov-04 8:20
sheppe12-Nov-04 8:20 
GeneralRe: The right way to do it Pin
chakkaradeepcc1-Dec-04 19:53
chakkaradeepcc1-Dec-04 19:53 
GeneralRe: The right way to do it Pin
chakkaradeepcc2-Dec-04 20:27
chakkaradeepcc2-Dec-04 20:27 
GeneralRe: The right way to do it (This way works) Pin
Anonymous1-Jan-05 6:58
Anonymous1-Jan-05 6:58 
GeneralRe: The right way to do it (This way works) Pin
TriggerBG3-Jan-05 5:41
TriggerBG3-Jan-05 5:41 
GeneralRe: The right way to do it (This way works) Pin
Sven Buth3-Mar-05 23:43
sussSven Buth3-Mar-05 23:43 
GeneralRe: The right way to do it (This way works) Pin
00zimbo13-Jun-05 4:56
00zimbo13-Jun-05 4:56 
GeneralRe: The right way to do it (This way works) Pin
Anonymous13-Jun-05 5:39
Anonymous13-Jun-05 5:39 
GeneralRe: The right way to do it Pin
Member 61249314-Jan-05 4:34
Member 61249314-Jan-05 4:34 
GeneralRe: The right way to do it Pin
corsairX8-Mar-05 22:37
corsairX8-Mar-05 22:37 
Hello, i'm new to C#, i am working as Java Developer but that project i must do in C++ / C# on my choice, and i've chosen C# cos it's more reliable on my Java point of view.

I'm trying to write a Windows Service which interacts with the User Desktop, using especcially the API function getForegroundWindow().

I created the service, the installer, ... add a timer there, the timer fires the "Tick" event well, but the HWND returned by the getForegroundWindow is always NULL.

So i searched over the net. Microsoft is Boring, i have searched in all MSDN 2005 for a thing "How to create an Interactive Process" and also the net. The solutions i found were:
- setting the flag by hand when creating the process : NO USEFUL, though i want to use the .NET installer to do this job
- The registry thing from upper i don't think it's a very good solution
- The first 2 were from the google, what Microsoft tells in MSDN is a thing like that: Allowing windows service to interact with desktop is a very huge security problem. However, if you need to, try setting the service interacting to the desktop. That's all. Nowhere a example, a way to do it, blabla.

The solution i found there i put into my after_install method but i have problems with ServiceController which is non-static method/object and it's obviously for me (i am a Java Developer) not to have access that kind of objects. The errors looks like this

PATH\projectinstaller.cs(84,63): error CS0120: An object reference is required for the nonstatic field, method, or property 'System.ServiceProcess.ServiceController.ServiceName'<br />
<br />
PATH\projectinstaller.cs(88,4): error CS0120: An object reference is required for the nonstatic field, method, or property 'System.ServiceProcess.ServiceController.Start()'


Can anybody help me please on that problem because i am very new to C# and i don't know what to do anymore. In java i have done it in 40 minutes maximum, but the ressources used would be 40 times more, and that i know it, even i love Java.

Thank you.
GeneralRe: The right way to do it Pin
corsairX8-Mar-05 23:13
corsairX8-Mar-05 23:13 
GeneralRe: The right way to do it Pin
pithhelmet24-May-05 6:36
pithhelmet24-May-05 6:36 
GeneralRe: The right way to do it Pin
Ryan L. Schneider18-Oct-05 10:34
Ryan L. Schneider18-Oct-05 10:34 
GeneralRe: The right way to do it Pin
andronicm28-Oct-05 22:30
andronicm28-Oct-05 22:30 
GeneralRe: The right way to do it Pin
haha6242-Jan-06 4:14
haha6242-Jan-06 4:14 
GeneralRe: The right way to do it (not working after Windows restart) Pin
Richard N Everett11-Jan-06 6:31
Richard N Everett11-Jan-06 6:31 
GeneralRe: The right way to do it Pin
Ryan McFarren19-May-06 8:43
Ryan McFarren19-May-06 8:43 
GeneralRe: The right way to do it Pin
sedamkar22-Jun-06 8:10
sedamkar22-Jun-06 8:10 
GeneralRe: The right way to do it (does not work in windows server 2003) Pin
BhargavaJK2-Aug-06 18:58
BhargavaJK2-Aug-06 18:58 
GeneralRe: The right way to do it Pin
dmihailescu8-Oct-07 12:42
dmihailescu8-Oct-07 12:42 

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.