Mostrando postagens com marcador Windows Service. Mostrar todas as postagens

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
Leia mais

Como chamar programa a partir de um serviço

Esta é uma dica de Delphi, mas deve ser o mesmo em outras linguagens
Digamos que você possua a seguinte estrutura de arquivos:

pasta
-->serviço.exe
-->arquivo.exe

Se você quiser executar o arquivo.exe a partir de uma função de serviço.exe como por exemplo:

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

Este código não irá funcionar mesmo ambos estando na mesma pasta, pois o serviço não executa do diretório onde está o arquivo serviço.exe.

Para funcionar é necessário o seguinte procedimento:

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

A procedure ChDir() serve para mudar o diretório atual onde o processo está ativo.
Outra opção é:

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

Desta maneira é executado o programa arquivo.exe utilizado o caminho completo do executável.
Mas neste caso arquivo.exe ainda é executado em outra pasta e não onde este se encontra, então a opção Chdir() ainda é mais recomendada.
Até a próxima dica :D
Leia mais
Blogger Template by Clairvo