Is it possible to run an . EXE application from a Windows Service?

Asked

Viewed 582 times

2

This is an excerpt from the code:

protected override void OnStart(string[] args)
{
    Process.Start(@"C:\path\<file.exe>");
}

protected override void OnStop()
{
}

I wish I could run a . exe program inside the onStart(), however, using the Process.Start() does not work. Is there another way to do this? If yes, what would be?

  • I don’t understand what you mean by internal. You want your windows service to run/open another program or for it to interact with it?

2 answers

1


That is not possible. Windows services cannot run executable applications or invoke them, unlike Windows applications like Winforms, Console or WPF. Your code will work in any application other than a Windows service.

Windows has a security mechanism that causes services to run in sessions isolated from the user’s context, so there is no context where to start the executable.

Windows executables are only started in a context, be it administrator, system, or any user. Since a service is not in a specific context, but rather an isolated session, there is no place to run the application.

There is a way unsafe and not recommended to execute an executable from a service: by injecting the executable into an existing process. Therefore, this can make the system unstable and will become considered malware ever since. More details in this answer stack overflow.

  • 1

    Uhm, got it. Thanks friend, very enlightening. I’ll look at the answer you indicated.

0

Try this:

<<WARNING>> I do not know if it will work because Services have an isolation mechanism that cannot invoke windows. I don’t know if this applies to windowless processes. If a feedback works in comments

Establish the class Process before using the method Start and use the property StratInfo to configure the process startup.

   Process processo;

   protected override void OnStart(string[] args)
   {
        try
        {
            processo = new Process()

            // Não exibe o shell
            processo.StartInfo.UseShellExecute = false;

            // Carrega o caminho do seu arquivo
            processo.StartInfo.FileName = @"C:\path\<file.exe>";

            //Não criar janela de processo
            processo.StartInfo.CreateNoWindow = true;

            // Inicializar o processo
            processo.Start();

        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
   } 

If your process persists throughout your service:

   protected override void OnStop()
   {          
       processo.Kill();
   }
  • 1

    First, thank you for answering. About your reply, I had already tried this way but I did not succeed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.