Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if CreateProcess( nil , PChar( FProcess.Command ) , nil , nil , False ,
- NORMAL_PRIORITY_CLASS , nil , Directory ,
- StartupInfo , ProcessInfo ) then
- begin
- if FProcess.Wait then
- begin
- WaitForSingleObject( ProcessInfo.hProcess , Infinite );
- GetExitCodeProcess( ProcessInfo.hProcess , ExitCode );
- if Assigned( FProcess.FOnFinished ) then
- FProcess.FOnFinished( FProcess , ExitCode );
- end;
- CloseHandle( ProcessInfo.hProcess );
- CloseHandle( ProcessInfo.hThread );
- end;
- uses Windows;
- interface
- function MyTerminateAppEnum(hHwnd:HWND; dwData:LPARAM):Boolean; stdcall;
- implementation
- function MyTerminateAppEnum(hHwnd:HWND; dwData:LPARAM):Boolean;
- var
- vID:DWORD;
- begin
- GetWindowThreadProcessID(hHwnd, @vID);
- if vID = dwData then
- begin
- PostMessage(hHwnd, WM_CLOSE, 0, 0); //tell window to close gracefully
- Result := False; //can stop enumerating
- end
- else
- begin
- Result := TRUE; //keep enumerating until you find your id
- end;
- end;
- Procedure TerminateMe(YourSavedProcessInfo:TProcessInformation);
- var
- vExitCode:UINT;
- begin
- GetExitCodeProcess(YourSavedProcessInfo.hProcess, vExitCode);
- if (vExitCode = STILL_ACTIVE) then //launched app still running..
- begin
- //tell it to close
- EnumWindows(@MyTerminateAppEnum, YourSavedProcessInfo.dwProcessId);
- if WaitForSingleObject(YourSavedProcessInfo.hProcess, TIMEOUT_VAL) <> WAIT_OBJECT_0 then
- begin
- if not TerminateProcess(YourSavedProcessInfo.hProcess, 0) then //didn't close, try to terminate
- begin
- //uh-oh Didn't close, didn't terminate..
- end;
- end;
- end;
- CloseHandle(YourSavedProcessInfo.hProcess);
- CloseHandle(YourSavedProcessInfo.hThread);
- end;
Add Comment
Please, Sign In to add comment