我正在使用Win32 CreateProcess
函数来执行对外部可执行文件的调用.可执行文件返回一个字符串
有没有办法在调用可执行文件后捕获并查询返回的字符串?如果不这样做,我可能必须将字符串写入可执行文件中的文件,并在调用完成后在调用程序中读取该字符串.这看起来很蹩脚.
Jedi代码库包含函数CreateDOSProcessRedirected,它运行一个进程并为其提供输入和输出文件.您可以将所需的输入(如果有)放入输入文件,并从输出文件中读取过程输出(如果有)(在过程完成后).
这是在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;