// based on [url]http://www.swissdelphicenter.com/de/showcode.php?id=990[/url]
function RunCaptured(
const DirName, ExeName, CmdLine:
string;
OutPut: TStrings;
var ReturnCode: Cardinal): Boolean;
var
StartupInfo: TStartupInfo;
ProcInfo: TProcessInformation;
FileName:
string;
FileHandle: THandle;
SecurityAttributes: TSecurityAttributes;
begin
Result := False;
try
// set a temporary file
FileName := '
Test.tmp';
FillChar(SecurityAttributes, SizeOf(SecurityAttributes), #0);
SecurityAttributes.nLength := SizeOf(SecurityAttributes);
SecurityAttributes.bInheritHandle := True;
FileHandle := Windows.CreateFile(PChar(FileName),
GENERIC_WRITE, FILE_SHARE_WRITE, @SecurityAttributes, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, 0);
try
FillChar(StartupInfo, SizeOf(StartupInfo), #0);
StartupInfo.cb := SizeOf(StartupInfo);
StartupInfo.hStdOutput := FileHandle;
StartupInfo.dwFlags := STARTF_USESTDHANDLES
or STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := SW_HIDE;
// start the program
if CreateProcess(
nil, PChar(ExeName + '
' + CmdLine),
nil,
nil, True,
0,
nil, PChar(DirName), StartupInfo, ProcInfo)
then
begin
SetPriorityClass(ProcInfo.hProcess, IDLE_PRIORITY_CLASS);
WaitForSingleObject(ProcInfo.hProcess, Infinite);
GetExitCodeProcess(ProcInfo.hProcess, ReturnCode);
// PHP.exe returns 255 on parse errors!
Result := True;
CloseHandle(ProcInfo.hThread);
CloseHandle(ProcInfo.hProcess);
Windows.CloseHandle(FileHandle);
// Add the output
if (OutPut <>
nil)
then
OutPut.LoadFromFile(FileName);
end;
Windows.DeleteFile(PChar(FileName));
except
Windows.CloseHandle(FileHandle);
Windows.DeleteFile(PChar(FileName));
Result := False;
end;
finally
end;
end;