469,909 Members | 1,562 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,909 developers. It's quick & easy.

Enabling Interaction of Desktop in C# Services

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
Nov 16 '05 #1
2 15604

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" <Ch**********@discussions.microsoft.com> wrote in message
news:9E**********************************@microsof t.com...
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

Nov 16 '05 #2
Hi,

the link to the codeproject article is as follows,
http://www.codeproject.com/csharp/Cs...iceDesktop.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 [MVP]" wrote:

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" <Ch**********@discussions.microsoft.com> wrote in message
news:9E**********************************@microsof t.com...
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


Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by richard | last post: by
reply views Thread by Josh Comley | last post: by
6 posts views Thread by Andy G | last post: by
8 posts views Thread by Rob R. Ainscough | last post: by
4 posts views Thread by vivek | last post: by
5 posts views Thread by sonali_reddy123 | last post: by
4 posts views Thread by Daniel.Benedek | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.