Hej Carsten
Værs'go
Function PFURunExecutable(FileName :String; Visibility: integer;
TimeOut:LongWord): DWORD;
var MsgWaitForMultipleObjects_TimeOut:LongWord;
Procedure WaitFor( processHandle: THandle );
Var
ret: DWORD;
Begin
MsgWaitForMultipleObjects_TimeOut:=TimeOut;
Repeat
ret := MsgWaitForMultipleObjects(
1, { 1 handle to wait on }
processHandle, { the handle }
False, { wake on any event }
MsgWaitForMultipleObjects_TimeOut,
// TimeOut,
// INFINITE, { wait without timeout }
QS_PAINT or { wake on paint messages }
QS_SENDMESSAGE { or messages from other threads }
);
If ret = WAIT_FAILED Then Exit; { can do little here }
If ret = (WAIT_OBJECT_0 + 1) Then Begin
{ Woke on a message, process paint messages only. Calling
PeekMessage gets messages send from other threads processed.
}
{ While PeekMessage( msg, 0, WM_PAINT, WM_PAINT, PM_REMOVE ) Do
DispatchMessage( msg );}
End;
Until ret = WAIT_OBJECT_0;
End; { Waitfor }
Var { V1 by Pat Ritchey, V2 by P.Below }
zAppName:array[0..512] of char;
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
Begin { WinExecAndWait32V2 }
StrPCopy(zAppName,FileName);
FillChar(StartupInfo,Sizeof(StartupInfo),#0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
// StartupInfo.dwFlags :=STARTF_USESTDHANDLES;
StartupInfo.wShowWindow := Visibility;
If not CreateProcess(nil,
zAppName, { pointer to command line string }
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) { pointer to PROCESS_INF }
Then
Result := DWORD(-1) { failed, GetLastError has error code }
Else Begin
// WaitForSingleObject(ProcessInfo.hProcess, TimeOut);
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
Waitfor(ProcessInfo.hProcess);
GetExitCodeProcess(ProcessInfo.hProcess, Result);
CloseHandle( ProcessInfo.hProcess );
CloseHandle( ProcessInfo.hThread );
End; { Else }
End; { WinExecAndWait32V2 }
|