Posso accedere alla stringa restituita da un comando Delphi CreateProcess?
-
19-08-2019 - |
Domanda
Sto usando la funzione Win32 CreateProcess
per eseguire una chiamata a un eseguibile esterno. L'eseguibile restituisce una stringa.
Esiste un modo per acquisire e interrogare la stringa restituita dopo aver chiamato l'eseguibile? In caso contrario, potrei dover scrivere la stringa in un file nell'eseguibile e leggerlo nel programma chiamante al termine della chiamata. Sembra però zoppo.
Soluzione
Jedi Code Library contiene la funzione CreateDOSProcessRedirected che esegue un processo e fornisce file di input e output. Puoi inserire l'input richiesto (se presente) nel file di input e leggere l'output del processo (se presente) dal file di output (al termine del processo).
È così che viene implementato nella JCL :
function CreateDOSProcessRedirected(const CommandLine, InputFile, OutputFile: string): Boolean;
var
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
SecAtrrs: TSecurityAttributes;
hInputFile, hOutputFile: THandle;
begin
Result := False;
hInputFile := CreateFile(PChar(InputFile), GENERIC_READ, FILE_SHARE_READ,
CreateInheritable(SecAtrrs), OPEN_EXISTING, FILE_ATTRIBUTE_TEMPORARY, 0);
if hInputFile <> INVALID_HANDLE_VALUE then
begin
hOutputFile := CreateFile(PChar(OutPutFile), GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ, CreateInheritable(SecAtrrs), CREATE_ALWAYS,
FILE_ATTRIBUTE_TEMPORARY, 0);
if hOutputFile <> INVALID_HANDLE_VALUE then
begin
FillChar(StartupInfo, SizeOf(StartupInfo), #0);
StartupInfo.cb := SizeOf(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
StartupInfo.wShowWindow := SW_HIDE;
StartupInfo.hStdOutput := hOutputFile;
StartupInfo.hStdInput := hInputFile;
Result := CreateProcess(nil, PChar(CommandLine), nil, nil, True,
CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo,
ProcessInfo);
if Result then
begin
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end;
CloseHandle(hOutputFile);
end;
CloseHandle(hInputFile);
end;
end;
Altri suggerimenti
Supponendo che tu voglia catturare ciò che l'exe sta scrivendo nell'output standard, puoi avviarlo con
yourprog.exe > results.txt
Questo scriverà l'output in results.txt
, che potrai quindi leggere e valutare.
In alternativa, puoi provare il metodo spiegato in questa discussione : Crea una pipe per l'output dal processo generato e leggere da esso. Il post numero 7 ha un esempio di fonte su come utilizzare le funzioni WinApi rilevanti in C ++, che dovrebbero essere facilmente tradotte in Delphi.
In che modo l'eseguibile restituisce una stringa ? Il più che so che un eseguibile può restituire è solo un numero, il codice di uscita.
È possibile utilizzare la memoria condivisa per comunicare tra le due applicazioni.
Puoi usare la funzione Win32 CreateFileMapping e forse usare un mutex per sincronizzare le chiamate al file mappato in memoria.
Il seguente codice crea il file.
var
fMapping : THandle;
pMapData : Pointer;
fMapping := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE,
0, MAPFILESIZE, pchar('MAP NAME GOES HERE'));
PMapData := MapViewOfFile(fMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0);
il seguente codice chiude il mapfile
if PMapData <> nil then
UnMapViewOfFile(PMapData);
if fMapping <> 0 then
CloseHandle(fMapping);