Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Aplicacion de consola (https://www.clubdelphi.com/foros/showthread.php?t=96748)

ArtPortEsp 18-06-2024 18:25:55

Aplicacion de consola
 
Hola amigos; tengo la necesidad de lanzar un proceso externo desde una aplicacion de consola.

La aplicacion de consola corre en Windows y en Linux.

Despues de lanzar el proceso tengo que "esperar" por el resultado.

Alguna sugerencia que sirva para ambas plataformas?

duilioisola 18-06-2024 20:14:50

RunAndWait para Windows.
Si avriguas como hacerlo en Linux puedes tener la misma función compilada según un {$IFDEF WIN32} ...{$ENDIF WIN32}, {$IFDEF LINUX}...{$ENDIF LINUX}.

Código Delphi [-]
function RunAndWait(Handle: THandle; Ejecutable, Argumentos: string; const RunDirectory: PChar = nil; const Visibilidad: integer = SW_SHOWNORMAL; MensajeSiCorrecto: boolean = True): DWORD;
var
  Info : TShellExecuteInfo;
  pInfo : PShellExecuteInfo;
  ExitCode : word;
  P : PChar;
begin
  { Puntero a Info }
  { Pointer to Info }
  pInfo := @Info;
  { Rellenamos Info }
  { Fill info }
  with Info do
  begin
     cbSize := SizeOf(Info);
     fMask := SEE_MASK_NOCLOSEPROCESS;
     wnd := Handle;
     lpVerb := nil;
     lpFile := PChar(Ejecutable);
     { Parametros al ejecutable }
     { Executable parameters }
     lpParameters := PChar(Argumentos + #0);
     if RunDirectory = '' then
        lpDirectory := nil
     else
        lpDirectory := PChar(RunDirectory + #0);
     nShow := Visibilidad;
     hInstApp := 0;
  end;
  { Ejecutamos }
  { Execute }
  if not ShellExecuteEx(pInfo) then
  begin
     Result := GetLastError;
     if FormatMessage(Format_Message_Allocate_Buffer + Format_Message_From_System,
        nil,
        Result,
        0, @P,
        0,
        nil) <> 0 then
     begin
        // Display the string.
        ShowMessage(P);
        // Free the buffer.
        LocalFree(integer(P));
     end;
  end
  else
     Result := 0; // Info.hInstApp;

  { Esperamos que termine }
  { Wait to finish }
  repeat
     ExitCode := WaitForSingleObject(Info.hProcess, 500);
     Application.ProcessMessages;
  until (ExitCode <> WAIT_TIMEOUT);
  ExitCode := GetLastError;

  GetExitCodeProcess(Info.hProcess, Result);
  if ((Result < 32) and (ExitCode = 0)) then
  begin
     ExitCode := GetLastError;
     if FormatMessage(Format_Message_Allocate_Buffer + Format_Message_From_System,
        nil,
        ExitCode,
        0, @P,
        0,
        nil) <> 0 then
     begin
        // Display the string.
        if MensajeSiCorrecto or (ExitCode <> 0) then
           ShowMessage(P);
        // Free the buffer.
        LocalFree(integer(P));
     end;
  end
  else
     Result := 0; // Info.hInstApp;
end;

Casimiro Notevi 18-06-2024 20:26:04

Hay varias formas, pero por ejemplo así:
Código:

./mi_aplicacion &
wait


ArtPortEsp 19-06-2024 01:43:55

Cita:

Empezado por duilioisola (Mensaje 556212)
RunAndWait para Windows.
Si avriguas como hacerlo en Linux puedes tener la misma función compilada según un {$IFDEF WIN32} ...{$ENDIF WIN32}, {$IFDEF LINUX}...{$ENDIF LINUX}.

Código Delphi [-]
function RunAndWait(Handle: THandle; Ejecutable, Argumentos: string; const RunDirectory: PChar = nil; const Visibilidad: integer = SW_SHOWNORMAL; MensajeSiCorrecto: boolean = True): DWORD;
var
  Info : TShellExecuteInfo;
  pInfo : PShellExecuteInfo;
  ExitCode : word;
  P : PChar;
begin
  { Puntero a Info }
  { Pointer to Info }
  pInfo := @Info;
  { Rellenamos Info }
  { Fill info }
  with Info do
  begin
     cbSize := SizeOf(Info);
     fMask := SEE_MASK_NOCLOSEPROCESS;
     wnd := Handle;
     lpVerb := nil;
     lpFile := PChar(Ejecutable);
     { Parametros al ejecutable }
     { Executable parameters }
     lpParameters := PChar(Argumentos + #0);
     if RunDirectory = '' then
        lpDirectory := nil
     else
        lpDirectory := PChar(RunDirectory + #0);
     nShow := Visibilidad;
     hInstApp := 0;
  end;
  { Ejecutamos }
  { Execute }
  if not ShellExecuteEx(pInfo) then
  begin
     Result := GetLastError;
     if FormatMessage(Format_Message_Allocate_Buffer + Format_Message_From_System,
        nil,
        Result,
        0, @P,
        0,
        nil) <> 0 then
     begin
        // Display the string.
        ShowMessage(P);
        // Free the buffer.
        LocalFree(integer(P));
     end;
  end
  else
     Result := 0; // Info.hInstApp;

  { Esperamos que termine }
  { Wait to finish }
  repeat
     ExitCode := WaitForSingleObject(Info.hProcess, 500);
     Application.ProcessMessages;
  until (ExitCode <> WAIT_TIMEOUT);
  ExitCode := GetLastError;

  GetExitCodeProcess(Info.hProcess, Result);
  if ((Result < 32) and (ExitCode = 0)) then
  begin
     ExitCode := GetLastError;
     if FormatMessage(Format_Message_Allocate_Buffer + Format_Message_From_System,
        nil,
        ExitCode,
        0, @P,
        0,
        nil) <> 0 then
     begin
        // Display the string.
        if MensajeSiCorrecto or (ExitCode <> 0) then
           ShowMessage(P);
        // Free the buffer.
        LocalFree(integer(P));
     end;
  end
  else
     Result := 0; // Info.hInstApp;
end;


Primero que nada, muchas gracias!; no estoy seguro, pero al ser una aplicación de consola, no podría usar ShowMessage y cosas que tengan que ver con la GUI de windows verdad?

Al estar Shellexecute en la librería WinApi, no estaría también ligado a windows?...

Entiendo lo de la compilación condicional, en su caso tambien deberia condicionar los elementos que son para interfaz grafica?

duilioisola 19-06-2024 08:17:11

Obviamente deberás modificar la función para adaptarse a lo que necesitas...
La compilación condicional deberá tener en cuenta las librerías, además del código.

Algo así:
Código Delphi [-]
uses {$IFDEF WIN32}WinApi{$ENDIF} 
     {$IFDEF LINUX}LinApi{$ENDIF} ; // Este último me lo he inventado

{$IFDEF WIN32}
function RunAndWait(Handle: THandle; Ejecutable, Argumentos: string; const RunDirectory: PChar = nil; const Visibilidad: integer = SW_SHOWNORMAL; MensajeSiCorrecto: boolean = True): DWORD;
...
{$ENDIF} 
{$IFDEF LINUX}
function RunAndWait(Handle: THandle; Ejecutable, Argumentos: string; const RunDirectory: PChar = nil; const Visibilidad: integer = SW_SHOWNORMAL; MensajeSiCorrecto: boolean = True): DWORD;
...
{$ENDIF} ;


La franja horaria es GMT +2. Ahora son las 00:03:49.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi