2

I want run an EXE file from a windows service using C# , I'm using this code in OnStart() service's method.

Process myProc = new Process();
myProc.StartInfo.FileName = "...\\MyExe.exe";
myProc.Start();

MyExe.exe is a simple console application that launch a console with some text. When I start my windows service I can see that MyExe.exe is in background processes, but there is no console shown on desktop.

What can be wrong ?

Litisqe Kumar
  • 2,512
  • 4
  • 26
  • 40
leonidas79
  • 437
  • 2
  • 7
  • 19
  • 1
    Services are isolated from users by design; you would need to perform some form of IPC to something running on a logged on users desktop. – Alex K. Jul 14 '15 at 11:53
  • possible duplicate of [How can I run an EXE program from a Windows Service using C#?](http://stackoverflow.com/questions/5307968/how-can-i-run-an-exe-program-from-a-windows-service-using-c) – Alex K. Jul 14 '15 at 11:57
  • my goal is to test launching an exe file that have a UI component from a windows service – leonidas79 Jul 14 '15 at 12:06
  • 1
    I understand that; as stated you cannot trivially do it. Go through the Google results for *run desktop application from service*. – Alex K. Jul 14 '15 at 12:08
  • 1
    "there is no console shown on desktop" - good, because services can run *before* any user logs in - there's no desktop to target. And windows allows *multiple* users to log in - there can be multiple desktops. As Alex has been saying, the correct fix is to separate anything UI related from the service. – Damien_The_Unbeliever Jul 14 '15 at 13:55

1 Answers1

0

Windows services won't display an interface by default, even if you launch an separate program. When you configure your service, you need to check the "Allow service to interact with desktop" checkbox on the Log On tab.

Opening a command line application is not the most elegant of solutions, and I would honestly avoid doing it if I could.

MichaelDotKnox
  • 1,312
  • 1
  • 14
  • 17
  • i'm using visual studio community 2013 , i can't find this option in settings . – leonidas79 Jul 14 '15 at 11:57
  • Not in Visual Studio, in the actual settings of the service in Control Panel. Go to Start->Run, type "services.msc" to open the services, right click on your service and choose properties – MichaelDotKnox Jul 14 '15 at 12:17
  • i tried with the option "Allow service to interact with desktop" but it's not working – leonidas79 Jul 14 '15 at 12:24
  • I think you really need to reconsider your design. What you are trying to do can cause all sorts of issues as @alex-k mentioned above. – MichaelDotKnox Jul 14 '15 at 12:34
  • is it poosible at least or not ? – leonidas79 Jul 14 '15 at 12:34
  • It is technically possible, but if you are trying to display a UI to a user, it is a bad idea and opens up all sorts of security holes. Just because you can do something doesn't mean that you should do it. – MichaelDotKnox Jul 14 '15 at 12:42