segunda-feira, 29 de outubro de 2012

How to call another application from a service

This is a tip of Delphi, but should be the same in other languages

Say you have the following file structure:

folder
-> serviceapp.exe
-> application.exe

If you want to run the application.exe from a function in serviceapp.exe such as:

WinExec (PChar ('application.exe'), SW_MINIMIZE);

This code will not work even both being in the same folder as the service does not run from the directory where the file serviceapp.exe is.

To work you need the following:

ChDir (ExtractFilePath (Application.ExeName));
WinExec (PChar ('application.exe'), SW_MINIMIZE);

The procedure ChDir () change the current directory where the process is active.
Another option is:

WinExec (PChar (ExtractFilePath (Application.ExeName) + 'application.exe'), SW_MINIMIZE);

This way you will run the application.exe file using the full path to the executable.
But in this case the  application.exe still running from another folder, then the option Chdir () is better.

Until next tip: D

0 comentários:

Blogger Template by Clairvo