How to close another application (2) (Views: 29)
Problem/Question/Abstract: I'm using the command CreateProcess(nil, PChar('PKUNZIP ..."),nil,nil,false,0,nil,nil,SI,PI) to execute PKUNZIP inside my program. But after the execution of PKUNZIP the window remains opened. How do I detect if PKUNZIP finishes its execution and how do I close the window after that? Answer: Closing it gently : { ... } var Handle: THandle; begin Handle := FindWindow(classname, windowname); {Look this one up in the help file ...} if Handle <> 0 then if MessageBox(Handle, PChar('Do you really want me to try to kill this application ?'), 'Please Confirm', MB_YESNO) = mrYES then begin PostMessage(Handle, WM_QUIT, 0, 0); end; end; To close it with more brute force: procedure TggProcessViewer.KillProcess(hProcess: THandle); var PH: THandle; lpExitCode: DWord; begin PH := OpenProcess(PROCESS_TERMINATE or PROCESS_QUERY_INFORMATION, false, hProcess); if PH <> 0 then begin if GetExitCodeProcess(PH, lpExitCode) then begin if MessageBox(Handle, PChar('Do you really want me to try to kill this process ?'), 'Please Confirm', MB_YESNO) = mrYES then begin TerminateProcess(PH, lpExitCode); MessageBox(Handle, PChar('should be dead now...'), PChar('Check it out...'), MB_OK); end; end else MessageBox(Handle, PChar('Could not retreive the ExitCode for this process.' + #13 + #13 + SysErrorMessage(GetLastError)), PChar('Something went wrong...'), MB_OK); CloseHandle(PH); end else MessageBox(Handle, PChar('Could not get access to this process.' + #13 + #13 + SysErrorMessage(GetLastError)), PChar('Something went wrong...'), MB_OK); Refresh; end; |