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

 
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 
System.InvalidOperationException: An exception occurred in the OnAfterInstall event handler of TestService.WindowsServiceInstaller.
The inner exception System.UnauthorizedAccessException was thrown with the following error message: Cannot write to the registry key..
 
Winmdows 7 x64
 
What can i do for that ?
GeneralMy vote of 3member[TEC]Mihail18-Jan-12 6:53 
eeee
GeneralMy vote of 5membermartinrj3013-Dec-11 15:22 
million times better than manually changing the option
GeneralAn alternative, better solutionmemberFtpDaemon23-Apr-11 11:30 
This did not work for me under WinXP, SP3, VS2010, .NET4.0, however, I found another, working solution at
 
C# Scheduler[^]
 
check the comment by corsairX. Worked like a charm!
GeneralUse using(RegistryKey cKey = ..) or explicit call cKey.close()memberKoprinkov3-Feb-11 2:13 
Use using(RegistryKey cKey = ..) or explicit call cKey.close()
QuestionWell Does this Work with Win7 + VS.Net 2010memberMarkWardell8-Jan-11 4:28 
When i use this code it :
1) Sets key to 272 as desired.
2) in OnStart I try to show a form but it just says it stops the service.
 
Any clues?
 
Mark
GeneralMy vote of 5memberBigdeak4-Oct-10 0:19 
Very good to know, thanks for this small article.
GeneralMy vote of 4membershanthiram710-Aug-10 10:24 
Excellent article. Looking allover for it.
GeneralMy vote of 5memberusmanbinasif28-Jun-10 1:50 
Awesome...
Generalsimplified VB.NET 2005memberMember 452036318-May-09 9:23 
Private Sub ServiceInstaller1_AfterInstall(ByVal sender As System.Object, ByVal e As System.Configuration.Install.InstallEventArgs) Handles ServiceInstaller1.AfterInstall
        'Set the option for "Allow service to interact with desktop"
        Dim ServiceKey As Microsoft.Win32.RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Services\" & ServiceInstaller1.ServiceName, True)
        If Not IsNothing(ServiceKey) Then
            If Not IsNothing(ServiceKey.GetValue("Type")) Then ServiceKey.SetValue("Type", (CType(ServiceKey.GetValue("Type"), Integer) Or 256))
        End If
End Sub

GeneralYet another way to do itmemberMember 96679523-Feb-09 1:13 
Hi , I do not want read meny posts below. What can I said
the code above was tested under Xp an 64bit 2003 Server and it works.
 
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
 
        protected override void OnAfterInstall(IDictionary savedState)
        {
            base.OnAfterInstall(savedState);
 
            // 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\" + this.serviceInstaller1.ServiceName, 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));
                }
            }
        }
.....

GeneralMoving code to OnCommited to make it work [modified]memberbergdoktor3-Feb-09 22:09 
In order to make the sample code work I had to move the modification of the Registry key to the overriden OnCommitted method.
        protected override void OnCommitted(IDictionary savedState)
        {
            // make sure to notify all subscribers to the event
            base.OnCommitted(savedState);
 
            // The following code sets the flag to allow desktop interaction for the service
            using (RegistryKey ckey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\My Service", true))
            {
                if (ckey != null)
                {
                    if (ckey.GetValue("Type") != null)
                    {
                        ckey.SetValue("Type", (((int)ckey.GetValue("Type")) | 256));
                    }
                }
            }
        }
        
modified on Thursday, February 18, 2010 12:26 PM

GeneralRe: Moving code to OnCommited to make it workmemberrexwang111-Nov-09 22:26 
This is the key information for me.
GeneralYou saved me a major panic just on the brink of a deadlinememberSimonWilliams8-Jul-08 7:43 
Thanks for sharing this, I am about to hit a deadline and I won't have to stay up all night now!
General'breadcrumb'memberizmoto8-Apr-08 1:52 
You are here: tested->working->love it!!! Laugh | :laugh:
 
void izmoto(char* szKwazi);

Questionthis.Installers.Clear(); ?memberkrishna_mag4-Jan-08 1:21 
I am getting error saying "From1 does not contain a definition for Installers" what should i do for it?
GeneralReally Awsome!memberSapphire043026-Dec-07 14:37 
you solve my problem!
thanks a lot
 
--
 
http://jjwang0430.blogspot.com
GeneralAbout Exceptions in Windows ServicememberPradeep Sattikar27-Nov-07 22:23 
hi,
thanks for giving such a wonderful code on internet.
the code works fine. Now I need to handle exceptions in windows service.Which is best way to inform user to inform about exception in windows service?
 
Thanks in advance

 
Regards
Pradeep Sattikar
GeneralAbout Exceptions in Windows ServicememberPradeep Sattikar27-Nov-07 22:22 
hi,
thanks for giving such a wonderful code on internet.
the code works fine. Now I need to handle exceptions in windows service.Which is best way to inform user to inform about exception in windows service?
 
Thanks in advance

 
Regards
Pradeep Sattikar

GeneralIntractive Servicemember73amit10-Oct-07 19:48 
Hi,
I have created one interactive service using a porperty "Allow service to intract with desktop"
And then while servie is running; in OnStart method one form is displayed.
My problem is that this form is loaded but not compeletly.
Service doesn't stops but i think it is in pause state at that moment.
I waited for some time but it dosent work out.
Plz help. ASAP
 

 
Amit
GeneralUse the name of the service programaticlymemberYitzhak Gootvilig14-Aug-07 3:58 
That's little better:
RegistryKey ckey = Registry.LocalMachine.OpenSubKey(
              string.Format(@"SYSTEM\CurrentControlSet\Services\{0}", si.ServiceName), true);

QuestionCheckbox has no effect ! [modified]memberfloydus2731-Jul-07 4: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
{
[RunInstaller(true)]
partial class ProjectInstaller : Installer
{
#region variables
private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller serviceInstaller1;
#endregion
 
private System.ComponentModel.IContainer components = null;
 
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
 
#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[] {
this.serviceProcessInstaller1,
this.serviceInstaller1});
 
}
 
#endregion
 
#region override methods
public override void Install(IDictionary mySavedState)
{
base.Install(mySavedState);
 
try
{
string SocketNumber = Context.Parameters["SocketNumber"];
string assemblypath = Context.Parameters["assemblypath"];
string appConfigPath = assemblypath + ".config";
XmlDocument doc = new XmlDocument();
doc.Load(appConfigPath);
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
doc.Save(appConfigPath);
}
}
}
}
catch (FormatException e)
{
string s = e.Message;
}
try
{
CreateEventLog();
}
catch (Exception)
{
}
}
 
protected override void OnAfterInstall(IDictionary mySavedState)
{
base.OnAfterInstall(mySavedState);
 
try
{
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.Start();
proc.StandardInput.WriteLine("net start \"DSD PROBE\"");
proc.StandardInput.Close();
 

}
catch (Exception)
{
}
}
 
protected override void OnBeforeUninstall(IDictionary mySavedState)
{
base.OnBeforeUninstall(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.Start();
proc.StandardInput.WriteLine("net stop \"DSD PROBE\"");
proc.StandardInput.Close();
}
catch (Exception)
{
}
}
 
public override void Rollback(IDictionary mySavedState)
{
.....
}
 
public override void Uninstall(IDictionary savedState)
{
..........
}
#endregion
}
}

 
"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]memberdmihailescu8-Oct-07 12:11 
The service controller is out of sync with the registry.
If you reboot, it will resync and work properly, but it sucks as a good solution.Mad | :mad:
 
As someone suggested below the 'right way is'
 
ConnectionOptions coOptions = new ConnectionOptions();
 
coOptions.Impersonation = ImpersonationLevel.Impersonate;
 
ManagementScope mgmtScope = new System.Management.ManagementScope(@"root\CIMV2", coOptions);
 
mgmtScope.Connect();
 
ManagementObject wmiService;
 
wmiService = new ManagementObject("Win32_Service.Name='" + ServiceMonitorInstaller.ServiceName + "'");
 
ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
 
InParam["DesktopInteract"] = true;
 
ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);
AnswerRe: Checkbox has no effect ! [modified]memberPouriya.GH28-Apr-12 23:46 
You can start and stop your services using this code:
 
    public static class ServiceHelper
    {
        public static void StartService(string serviceName, int timeoutMilliseconds)
        {
            ServiceController service = new ServiceController(serviceName);
 
            var servicesDependedOn = service.ServicesDependedOn;
            if (null != servicesDependedOn)
            {
                foreach (var svc in servicesDependedOn)
                {
                    StartService(svc.ServiceName, timeoutMilliseconds);
                }
            }
 
            try
            {
                TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
                service.Start();
                service.WaitForStatus(ServiceControllerStatus.Running, timeout);
                service.Refresh();
            }
            catch
            {
            }
            finally
            {
                service.Dispose();
            }
        }
 
        public static void StopService(string serviceName, int timeoutMilliseconds)
        {
            ServiceController service = new ServiceController(serviceName);
            var dependentServices = service.DependentServices;
            if (null != dependentServices)
            {
                foreach (var svc in dependentServices)
                {
                    StopService(svc.ServiceName, timeoutMilliseconds);
                }
            }
 
            try
            {
                TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
                service.Start();
                service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
                service.Refresh();
            }
            catch
            {
            }
            finally
            {
                service.Dispose();
            }
        }
 
        public static bool IsServiceAvailable(string serviceName)
        {
            var result = false;
            ServiceController service = null;
            try
            {
                service = new ServiceController(serviceName);
                result = service.Status == ServiceControllerStatus.Running;
                return result;
            }
            catch
            {
                return result;
            }
            finally
            {
                if (null != service)
                {
                    service.Dispose();
                }
            }
        }
    }

BugStart / Stop ServicesmemberPouriya.GH29-Apr-12 6:59 
The StopService method was wrong, i fixed that:
        public static void StartService(string serviceName, int timeoutMilliseconds)
        {
            ServiceController service = new ServiceController(serviceName);
 
            var servicesDependedOn = service.ServicesDependedOn;
            if (null != servicesDependedOn)
            {
                foreach (var svc in servicesDependedOn)
                {
                    StartService(svc.ServiceName, timeoutMilliseconds);
                }
            }
 
            try
            {
                TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
                if (service.Status != ServiceControllerStatus.Running)
                {
                    service.Start();
                    service.WaitForStatus(ServiceControllerStatus.Running, timeout);
                    service.Refresh();
                }
            }
            catch
            {
            }
            finally
            {
                service.Dispose();
            }
        }
 
        public static void StopService(string serviceName, int timeoutMilliseconds)
        {
            ServiceController service = new ServiceController(serviceName);
            var dependentServices = service.DependentServices;
            if (null != dependentServices)
            {
                foreach (var svc in dependentServices)
                {
                    StopService(svc.ServiceName, timeoutMilliseconds);
                }
            }
 
            try
            {
                TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
                if (service.Status != ServiceControllerStatus.Stopped)
                {
                    service.Stop();
                    service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
                    service.Refresh();
                }
            }
            catch
            {
            }
            finally
            {
                service.Dispose();
            }
        }
 
        public static bool IsServiceAvailable(string serviceName)
        {
            var result = false;
            ServiceController service = null;
            try
            {
                service = new ServiceController(serviceName);
                result = service.Status == ServiceControllerStatus.Running;
                return result;
            }
            catch
            {
                return result;
            }
            finally
            {
                if (null != service)
                {
                    service.Dispose();
                }
            }
        }
 
    }

GeneralHelp!!!memberCammail28-May-07 3:27 
Hi all,
 
I'm trying to use this code, and i'm having a problem, i can't make it to work... i have read all posts.
 
I'm using this code on (ProjectInstaller.VB).
 
#Region " Component Designer generated code "
 

 

Public Sub New()
MyBase.New()
 
'This call is required by the Component Designer.
InitializeComponent()
 

'Add any initialization after the InitializeComponent() call
 
'Add initialization code after the call to InitializeComponent
Dim si As New ServiceProcess.ServiceInstaller
si.ServiceName = "Myapp"
si.DisplayName = "Myapp"
si.StartType = ServiceProcess.ServiceStartMode.Manual
 
Me.Installers.Add(si)
Dim spi As New ServiceProcess.ServiceProcessInstaller
spi.Account = System.ServiceProcess.ServiceAccount.LocalSystem
spi.Password = Nothing
spi.Username = Nothing
Me.Installers.Add(spi)
 
Dim ckey As RegistryKey = Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Services\Myapp", True)
 
If ckey.GetValue("Type") <> Nothing Then
ckey.SetValue("Type", (ckey.GetValue("Type") Or 256))
End If
 

End Sub
 

But wend i try to install my service i get this error.
 
"Unable to create an instance of the "Myapp".ProjectInstaller installer type. --> Exception has been throw by the target of an invocation. --> Object reference not set to an instance of an object."
 

After this error my installation will rollback.
 

Cold some one gives a help....
 
Reagards,
 
mpires

GeneralRe: Help!!!memberCammail28-May-07 8:00 
Tks, i have found the solution....
 
I have set on after install:
 
Dim ckey As RegistryKey = Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Services\Myapp", True)
 
If ckey.GetValue("Type") <> Nothing Then
ckey.SetValue("Type", (ckey.GetValue("Type") Or 256))
End If
 

And it works....
 
Nice....
GeneralFYI: Platform SDK way of doing this...member-midiman-12-Dec-06 2:03 
This is a really good article for how to do this in C#.
 
If, however, you're not using C#, and are using the Platform SDK,
you can simply specify the DesktopInteract mode in the CreateService() API call e.g.:
 
SC_HANDLE schService = CreateService(schSCManager,
"MyService",
"MyServiceDisplayName",
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL,
"\"MyQuotedFullPathToExe\"",
NULL, NULL, NULL, NULL, NULL);
 
Note that in order for the interact mode to function, the service must use the LocalSystem account (second to last parameter = NULL).
Also note that if you set the 'Type' registry value directly to 0x00000110 or similar, this will not work on Windows 2003 server, so the programmatic way is definitely the better bet.
 
Hope this helps!

GeneralConvert to vb.netmemberalgel2-Jun-06 11:00 
hello All:
 
Can anyone please can convert the code to vb.net ?
 
Thanks in advance
Albert
GeneralRe: Convert to vb.netmemberICTech22-Jan-07 10:53 
Public Sub New()
InitializeComponent
Dim si As ServiceInstaller = New ServiceInstaller
si.ServiceName = "WindowsService1"
si.DisplayName = "WindowsService1"
si.StartType = ServiceStartMode.Manual
Me.Installers.Add(si)
Dim spi As ServiceProcessInstaller = New ServiceProcessInstaller
spi.Account = System.ServiceProcess.ServiceAccount.LocalSystem
spi.Password = Nothing
spi.Username = Nothing
Me.Installers.Add(spi)
Dim ckey As RegistryKey = Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Services\WindowsService1", True)
If Not (ckey Is Nothing) Then
If Not (ckey.GetValue("Type") Is Nothing) Then
ckey.SetValue("Type", (CType(ckey.GetValue("Type"), Integer) Or 256))
End If
End If
End Sub
Newshttp://united-arab-emirates-airline.tangoing.info/susshttp://united-arab-emirates-airline.tangoing.info/4-Dec-07 7:35 
http://united-arab-emirates-airline.tangoing.info/
http://united-arab-emirates-airline.tangoing.info/
GeneralIn my winXP System i have to add this code this.Installers.Clear()memberlkaou9-Nov-05 20:13 
this.Installers.Clear(); //add by kali, it work well Smile | :)
ServiceInstaller si = new ServiceInstaller();
si.ServiceName = "MonAgent";
si.DisplayName = "MonAgent";
si.StartType = ServiceStartMode.Automatic ;
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\MonAgent", 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));
}
}

GeneralIf that doesn't work...sussAnonymous1-Jan-05 6:59 
See the thread "The right way to do it".
 
This worked for me. The method in the original post (registry edit) didn't work on my Windows Server 2003 system. Looking at the service properties in the SCM, everything looked fine (the "Allow service to interact with desktop" flag was set), and there were no exceptions thrown in the GUI code, it just wouldn't show anything on the desktop (forms, notify icon, etc.)
 
Using this method, all worked great.
 
Regards (and thanks),
 
Robb

GeneralNot Able to run another applicationmemberchakkaradeepcc1-Dec-04 20:14 
hi,
i tried changing the Registry Key...but my service is not able to run a GUI Application...what is the mistake??.....it is LocalSystem Account and n the properties box , the Desktop Interaction is enabled...
i tried this code,
/***********************code starts here*************************/
Process proc= new Process();
proc.StartInfo.FileName="E:\\MyStarter.exe";
proc.Start();
/***********************code starts here*************************/
 
what is wrong in my code???
 

 
with regards,
C.C.Chakkaradeep
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 
This is not working for the application. There is uncheck option available after putting this code.
If any one got the demo application plz post here.
 


 
Regards,
 
Lalit Narayan Dubey
S/W Developer,Noida
GeneralRe: Not Able to run another applicationmemberLalit N Dubey9-May-06 23:11 
Hello Laurent,
 
I think this is not the proper solution. Because I felt one thing very interesting i have created on application ..
 
There this registry chnage us working fine the service is desktop interactive.
 
But in other application its failing to check the check box option. Event all the code is same.
 

So i think its better to find some thig good solution.
 


 
Regards,
 
Lalit Narayan Dubey
S/W Developer,Noida
GeneralAbsolutely delightful!sussAnonymous31-Aug-04 22:31 
Absolutely delightful! I had been looking for this piece of code or at least a direction towards doing it myself for sooo long, and finally got it. Though I was quite fine with the registry-'tampering' code, I impelemented it in VB.NET and though the checkbox was set to true for the service, it didn't quite work Cry | :(( I wonder why??
Eventually I used the 'right-way-to-do-it' piece of code and seriously it works like a dream. And moreover I got aware of a whole new concept of altering a service's properties on the fly!
All in all, loved it!

QuestionOnly works with LocalSystem account?sussAnonymous28-Jul-04 11:01 
I have a service that I install to run as the "NetworkService" user, instead of the "LocalSystem" user. I tried flipping that bit by hand in the registry and restarting the service, but my console window does not display, like it does when I run as LocalSystem. So I suspect that the bit (just like the dialog box checkbox) only applies if you are running as LocalSystem. Maybe not, but I though I would mention it.
AnswerRe: Only works with LocalSystem account?memberdaDude18-Oct-05 11:06 
So, is there any work around to enable "interact with desktop" for local user account?
GeneralThank you guysmemberisrael@jp-inc.com2-Jun-04 22:41 
Thank you guys: Robert for the initiative and knowledge, "Anonymous" for WMI suggestion and Richard for "the correct way" to get the thing done.
You don't need "Allow Interact with Desktop" for debugging with MessageBox.Show - ServiceNotification option works nicely.
Blake is right, of course, that interactive services are dangerous but
sometimes one needs a quick and dirty solution.
I really had to write a quick and dirty demo and Richard's suggestion to use ChangeServiceConfig was very helpful. It took a couple hours to declare and put in work the related P/Invoke functions (OpenSCManager, OpenService, ...) - the great PInvoke site (http://www.pinvoke.net) doesn't have them yet. Since I do the job on ProjectInstaller.Committed event I don't need to call LockServiceDatabase.
I'd like to attach here my class (about 150 lines, written carefully) but there is no place for attachments here, unfortunately.
 
Rose | [Rose] Rose | [Rose] Rose | [Rose]
 
Israel Shnaidman
GeneralRe: Thank you guysmemberDave Midgley17-Mar-06 0:45 
I am interested in why this is dangerous. You say "Blake is right of course . . ." but I couldn't find a message from anyone called Blake, or anything about the dangers. Why is this dangerous? Why should it only be used for "quick and dirty". I am writing a "proper" application that will go on sale top the public. Should I not use this method? If not, what is the recommended alternative?
 
Dave
GeneralRe: Thank you guysmember73amit10-Oct-07 22:34 
Hi Israel,
I am trying to load a form from OnStart method of windows service.
Plz help me by giving proper line of code or way to move.
 
Amit
GeneralThank you, Thank you, Thank you, Thank you,,Thank you.membernekane17-Feb-04 23:39 
Smile | :) Thank you,Thank you,Thank you,Thank you,Thank you,
Thank you,Thank you,Thank you,Thank you,Thank you,Thank you,Thank you,
Thank you,Thank you,Thank you,
Thank you,Thank you,Thank you,Thank you,Smile | :) Big Grin | :-D Big Grin | :-D
GeneralRe: Thank you, Thank you, Thank you, Thank you,,Thank you.sussAnonymous30-Jun-05 12:09 
Thank you, thank you, thank you, thank you.
 
Thank you.
GeneralRe: Thank you, Thank you, Thank you, Thank you,,Thank you.memberBested19-Jul-07 1:01 
Thank you, thank you, thank you, thank you.
Thank you, thank you, thank you, thank you.
Thank you, thank you, thank you, thank you.
Thank you, thank you, thank you, thank you.

GeneralInteract issuememberLord Fonty20-Jan-04 4:41 
I have written a Windows Service that monitors executing apps and if it isn't able to interact with the desktop the System.Diagnostic.Process.Responding always returns true even if the app has gone "Not Responding". This causes me another problem when the apps are running on another machine I have all sorts of security issues Frown | :(

 
Ian (Freebasing On Boredom.......)
 
BEING IN THERAPY
And yet, having therapy is very much like making love to a beautiful
woman. You... get on the couch, string 'em along with some half-lies and
evasions, probe some deep dark holes, and then hand over all your
money.
GeneralThe right way to do itsussAnonymous24-Nov-03 21:31 

private void ServiceInstaller_AfterInstall(object sender, System.Configuration.Install.InstallEventArgs e)
{
ConnectionOptions coOptions = new ConnectionOptions();
 
coOptions.Impersonation = ImpersonationLevel.Impersonate;
 
ManagementScope mgmtScope = new System.Management.ManagementScope(@"root\CIMV2", coOptions);
 
mgmtScope.Connect();
 
ManagementObject wmiService;
 
wmiService = new ManagementObject("Win32_Service.Name='" + ServiceController.ServiceName + "'");
 
ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
 
InParam["DesktopInteract"] = true;
 
ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);
 
ServiceController.Start();
}

 
this is the way to do this.
this code was provided by microsoft msdn support for the problem "interact with desktop".
 
after the installation, you will be able to run a service than can interact with the desktop.
 
greetings!
GeneralRe: The right way to do itmemberGennadiy Sterin15-Mar-04 4:19 
I have tried your suggestion.
It gives me an Exception during the installation, but if you do it on different
event - Commited, than it will work.
 
I absolutely agree that this is the right way of doing it.
Going through the registry -.... is a last reserve. Confused | :confused:
GeneralRe: The right way to do itmembercodeshasyou18-Oct-05 21:50 
Great Information THANKs for saving time

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.130617.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