function WinExecAndWait32aFileName: string; Visibility: Integer): integer;
{ returns -1 if the Exec failed, otherwise returns the process' exit
code when the process terminates }
var
zAppName: array[0..512] of char;
lpCommandLine: array[0..512] of char;
zCurDir: array[0..255] of char;
WorkDir: string;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
StrPCopy(zAppName, '');
StrPCopy(lpCommandLine, FileName);
GetDir(0, WorkDir);
StrPCopy(zCurDir, WorkDir);
FillChar(StartupInfo, Sizeof(StartupInfo), #0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := Visibility;
if not CreateProcess(
nil, { pointer to command line string }
lpCommandLine,
nil, { pointer to process security attributes }
nil, { pointer to thread security attributes}
False, { handle inheritance flag }
CREATE_NEW_CONSOLE or { creation flags }
NORMAL_PRIORITY_CLASS,
nil, { pointer to new environment block}
nil, { pointer to current directory name }
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) then Result := -1 { pointer to PROCESS_INF }
else begin
WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess, Result); // <<---error occurs here
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end;
end;
procedure TForm1.Button8Click(Sender: TObject);
var
sBatchFileName : string;
begin
sBatchFileName:= 'c:\windows\batch\icp.bat';
WinExecAndWait32a(sBatchFileName, SW_HIDE);
end;
Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.
Our community of experts have been thoroughly vetted for their expertise and industry experience.