Thursday, July 17, 2008

Calling an exe from Windows Service

Some times we may need to call an exe from our windows service program. The following code can be used to call an exe from an appication. The sample code is for VB.NET applications. Here the calculator (calc.exe) is called.


Dim startInfo As System.Diagnostics.ProcessStartInfo
Dim pStart As New System.Diagnostics.Process

'Start the process

startInfo = New System.Diagnostics.ProcessStartInfo("calc.exe")

pStart.StartInfo = startInfo

pStart.Start()

pStart.WaitForExit()

pStart.Close()

Here
pStart.WaitForExit() makes your application wait till the called exe is closed.
pStart.Close() will free all the resources that are associated with this component.

This will work if you are using a windows Application. But in a windows service this will not work straight, you need to change some setting for the windows service. A service won't be able to show the GUI of an application, so if the application you are calling has any GUI you won't be able to see it. But the process will get kicked off and you can see the evidence in TaskManager. But we can enable the service to popup the UI by enabling this settings.
Allow Services to interact with desktop
  1. Start > Run > Services.msc
  2. Locate the services which you have created
  3. Right click and select properties.
  4. Select "Log On" tab on the top of the screen
  5. Select "Allow Services to interact with desktop" check box.
  6. Click on "Apply" and start the services.
If you want to kill an application during Windows Service Stop, try the following code
Dim myProcess As Process
Dim myProcesses As Process() = Process.GetProcessesByName("calc")
For Each myProcess In myProcesses
myProcess.Kill()
Next

7 comments:

Anonymous said...

hi,

Thank you very much sujith.you have solved my problem for which i was browsing the net for last 3 hours.

regards,
shiva kumar

Anonymous said...

hi,

Shiva again.Could you pls let me knw as to how to pass parameters to windows services through commandline arguments and through user defined main method.pls post your reply to shivaece_ksk@yahoo.co.in

regards,
shiva kumar.

Anonymous said...

are yar that setting is not working for me.
I tried that ,will u pls elaborate on it once again.
shrikant

Anonymous said...

hi sujith,
i done that settings(allow intrctns with desktop),
exe is also running but its not doing the intended task,
for eg. im running windowsinstaller(msiexec.exe) from my service to install the applications, it is running in task mgr but its not installing the setup of the application. would u pls help me on this.that'll b grtly apprciated.thanx.
shrikant

Anonymous said...

hi sujith,
i done that settings(allow intrctns with desktop),
exe is also running but its not doing the intended task,
for eg. im running windowsinstaller(msiexec.exe) from my service to install the applications, it is running in task mgr but its not installing the setup of the application. would u pls help me on this.that'll b grtly apprciated.thanx.
shrikant

my email id is shri.bhoyar@gmail.com

Anonymous said...

Hi
your code is working to call exe.but it is not doing intended task of my exe.And this code creates many processes in task manager that means it not stops exe.pls give me reply how i can get result of my exe on my mail sonal.2086@gmail.com


Thanks & Regards,
Sonal Patel

sonia said...

Nice Tutorial.U save my lot of time