Author: Jonas Bilinkevicius
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 :
1 { ... }
2 var
3 Handle: THandle;
4 begin
5 Handle := FindWindow(classname, windowname); {Look this one up in the help file
6 ...}
7 if Handle <> 0 then
8 if MessageBox(Handle,
9 PChar('Do you really want me to try to kill this application ?'),
10 'Please Confirm', MB_YESNO) = mrYES then
11 begin
12 PostMessage(Handle, WM_QUIT, 0, 0);
13 end;
14 end;
15
16 //To close it with more brute force:
17
18 procedure TggProcessViewer.KillProcess(hProcess: THandle);
19 var
20 PH: THandle;
21 lpExitCode: DWord;
22 begin
23 PH := OpenProcess(PROCESS_TERMINATE or PROCESS_QUERY_INFORMATION, false,
24 hProcess);
25 if PH <> 0 then
26 begin
27 if GetExitCodeProcess(PH, lpExitCode) then
28 begin
29 if MessageBox(Handle,
30 PChar('Do you really want me to try to kill this process ?'),
31 'Please Confirm', MB_YESNO) = mrYES then
32 begin
33 TerminateProcess(PH, lpExitCode);
34 MessageBox(Handle, PChar('should be dead now...'), PChar('Check it out...'),
35 MB_OK);
36 end;
37 end
38 else
39 MessageBox(Handle, PChar('Could not retreive the ExitCode for this process.' +
40 #13 + #13 + SysErrorMessage(GetLastError)),
41 PChar('Something went wrong...'), MB_OK);
42 CloseHandle(PH);
43 end
44 else
45 MessageBox(Handle, PChar('Could not get access to this process.' + #13 + #13
46 + SysErrorMessage(GetLastError)), PChar('Something went wrong...'),
47 MB_OK);
48 Refresh;
49 end;
|