Enabling Interaction of Desktop in C# Services

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi all,

as it was discussed earlier in this forum titled,"Execution of Another
Application in Services", i got the answer that Services are not meant for
executing another applications, but Microsoft has given the options to
Interact with Desktop for the Services which need to...

I got many C Service Programs wherein they execute another application
depending upon the need...when i asked how can it be done in C#, many said it
should not be done but didnt give any suggestions on how to implement it...

When i was searching for this stuff, i found that enabling Interaction of
Desktop was a common thing and we can use it whenever we need to..and i got
this code from www.codeproject.com which is working fine...

the code s as follows,
/*********************code starts here**************************/
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='Service1'");//
+ServiceController.ServiceName + "'");
ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
InParam["DesktopInteract"] = true;
ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam,
null);
}
/*********************code ends here**************************/

am not telling that what others told me is wrong, but we have to use
Interact With Desktop option when it is needed and many of the services
running are using it....

so enjoy this stuff...

with regards,
C.C.Chakkaradeep
 
Sorry, I've (mistakenly) replied [1] to your original post titled "Windows
Services" in which I explained how to do it from code instead of replying to
: "Executing an application inside a Service".
Now you are starting another (third) thread about exactly the same subject,
please in order to prevent such mistakes don't start another thread for the
same issue.
Also you post a link to "codeproject" home page, I suggest you post the
exact URL to the article, so others can find it too (I didn't find it).

[1] RE: Windows Services
Yes, there is.

Add following class to the cs file that contains the Installer class (class
derived from System.Configuration.Install.Installer).
Change the class ServiceInstaller into ServiceInstallerEx and create an
instance of ServiceInstallerEx instead of ServiceInstaller .
Finally add - using System.Management;

class ServiceInstallerEx : System.ServiceProcess.ServiceInstaller
{
public ServiceInstallerEx() : base()
{
// Set eventhandler to call UpdateServiceConfig when install commited
base.Committed += new InstallEventHandler( this.UpdateServiceConfig );
}
// Eventhandler, called after service installer committed.
// This method uses System.Management classes to change service config.
// Note that enabling "DesktopInteract" will fail if Service account is not
LocalSystem !!
void UpdateServiceConfig(object sender, InstallEventArgs e)
{
int ret;
ManagementBaseObject inParams = null;
ManagementObject srvc = new ManagementObject("Win32_Service=" + "\"" +
this.ServiceName + "\"");
inParams = srvc.GetMethodParameters("Change");
inParams["DesktopInteract"] = true; // Enable interactive mode (Interact
With Desktop)
ManagementBaseObject outParams = srvc.InvokeMethod("Change", inParams,
null);
if((ret = Convert.ToInt32(outParams.Properties["ReturnValue"].Value)) !=
0)
Console.WriteLine("Failed to set option: {0}", ret);
else
Console.WriteLine("Option set");
}
}

Willy.


Chakkaradeep said:
hi all,

as it was discussed earlier in this forum titled,"Execution of Another
Application in Services", i got the answer that Services are not meant for
executing another applications, but Microsoft has given the options to
Interact with Desktop for the Services which need to...

I got many C Service Programs wherein they execute another application
depending upon the need...when i asked how can it be done in C#, many said
it
should not be done but didnt give any suggestions on how to implement
it...

When i was searching for this stuff, i found that enabling Interaction of
Desktop was a common thing and we can use it whenever we need to..and i
got
this code from www.codeproject.com which is working fine...

the code s as follows,
/*********************code starts here**************************/
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='Service1'");//
+ServiceController.ServiceName + "'");
ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
InParam["DesktopInteract"] = true;
ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam,
null);
}
/*********************code ends here**************************/

am not telling that what others told me is wrong, but we have to use
Interact With Desktop option when it is needed and many of the services
running are using it....

so enjoy this stuff...

with regards,
C.C.Chakkaradeep
 
Hi,

the link to the codeproject article is as follows,
http://www.codeproject.com/csharp/CsWindowsServiceDesktop.asp

just go thru the FAQ Section and u will find a thread "The right way to do
it"....thats the answer..

with regards,
C.C.Chakkaradeep

Willy Denoyette said:
Sorry, I've (mistakenly) replied [1] to your original post titled "Windows
Services" in which I explained how to do it from code instead of replying to
: "Executing an application inside a Service".
Now you are starting another (third) thread about exactly the same subject,
please in order to prevent such mistakes don't start another thread for the
same issue.
Also you post a link to "codeproject" home page, I suggest you post the
exact URL to the article, so others can find it too (I didn't find it).

[1] RE: Windows Services
Yes, there is.

Add following class to the cs file that contains the Installer class (class
derived from System.Configuration.Install.Installer).
Change the class ServiceInstaller into ServiceInstallerEx and create an
instance of ServiceInstallerEx instead of ServiceInstaller .
Finally add - using System.Management;

class ServiceInstallerEx : System.ServiceProcess.ServiceInstaller
{
public ServiceInstallerEx() : base()
{
// Set eventhandler to call UpdateServiceConfig when install commited
base.Committed += new InstallEventHandler( this.UpdateServiceConfig );
}
// Eventhandler, called after service installer committed.
// This method uses System.Management classes to change service config.
// Note that enabling "DesktopInteract" will fail if Service account is not
LocalSystem !!
void UpdateServiceConfig(object sender, InstallEventArgs e)
{
int ret;
ManagementBaseObject inParams = null;
ManagementObject srvc = new ManagementObject("Win32_Service=" + "\"" +
this.ServiceName + "\"");
inParams = srvc.GetMethodParameters("Change");
inParams["DesktopInteract"] = true; // Enable interactive mode (Interact
With Desktop)
ManagementBaseObject outParams = srvc.InvokeMethod("Change", inParams,
null);
if((ret = Convert.ToInt32(outParams.Properties["ReturnValue"].Value)) !=
0)
Console.WriteLine("Failed to set option: {0}", ret);
else
Console.WriteLine("Option set");
}
}

Willy.


Chakkaradeep said:
hi all,

as it was discussed earlier in this forum titled,"Execution of Another
Application in Services", i got the answer that Services are not meant for
executing another applications, but Microsoft has given the options to
Interact with Desktop for the Services which need to...

I got many C Service Programs wherein they execute another application
depending upon the need...when i asked how can it be done in C#, many said
it
should not be done but didnt give any suggestions on how to implement
it...

When i was searching for this stuff, i found that enabling Interaction of
Desktop was a common thing and we can use it whenever we need to..and i
got
this code from www.codeproject.com which is working fine...

the code s as follows,
/*********************code starts here**************************/
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='Service1'");//
+ServiceController.ServiceName + "'");
ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
InParam["DesktopInteract"] = true;
ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam,
null);
}
/*********************code ends here**************************/

am not telling that what others told me is wrong, but we have to use
Interact With Desktop option when it is needed and many of the services
running are using it....

so enjoy this stuff...

with regards,
C.C.Chakkaradeep
 
Back
Top