![]() |
TerminateProcess not working correctly....
Hi, i wonder if anyone can help me:
When i create a process, i use TerminateProcess to terminate the given process. This works fine, the process is killed correctly. Now i want to check to see if the process as killed, by using the OpenProcess API call, but for some FUNNY reason, OpenProcess does return a value, which should not happen, as the process does not exist.. Please can anyone help me with this?
Delphi-Quellcode:
procedure TForm1.btnCreateProcessClick(Sender: TObject);
var sinfo: TStartupInfo; pinfo: TProcessInformation; begin FillChar(sinfo, SizeOf(sinfo), 0); sinfo.cb := SizeOf(sinfo); //Run the Message Sending Application on the new Desktop CreateProcess(PChar('c:\windows\system32\notepad.exe'),nil, nil, nil, False, 0, nil, nil, sinfo, pinfo); pID := pinfo.dwProcessId; hProcess := pinfo.hProcess;//GetProcessHandle(pID); end; procedure TForm1.btnTerminateProcessClick(Sender: TObject); var NewHandle : THandle; i : integer; begin if TerminateProcess(hProcess,ExitCode) then ShowMessage('Terminated'); NewHandle := OpenProcess(SYNCHRONIZE, False, pID); if NewHandle = 0 then ShowMessage('Process not found') else ShowMessage('Process Found'); end; This is the results i get: If i click on the TerminateProcess button(btnTerminateProcessClick) before created the process with btnCreateProcessClick, then "Process not found" is returned, which is correct, because OpenProcess will return 0 if the process is not found. But now: When you first click on the btnCreateProcessClick button, then on the btnTerminateProcessClick button, then "Terminated" is displayed, indicating that the process was terminated, but THEN for some reason "Process FOUND" is displayed, where "Process not found" should actually be displayed since the process was already killed... Does anyone agree that this is strange, am i doing something wrong? All i want to do is check if the process exist by using a PID of Handle to check it... Thnx... |
Re: TerminateProcess not working correctly....
Could it be that you r a little bit to fast? You have to wait after terminating the process. Use waitforsingleobject or just sleep a few milliseconds.
|
Re: TerminateProcess not working correctly....
Zitat:
|
Re: TerminateProcess not working correctly....
TerminateProcess returns immediately no matter whether the process was killed or not. You have to verify the terminations of the process. And if you get a valid process handle the process is still alive.
Delphi-Quellcode:
{*
* Procedure : KillProcess * Author : Michael Puff * Date : 2006-09-15 * Terminates a process identified by its PID *} function KillProcess(dwProcID, Wait: DWORD): Integer; var hProcess : Cardinal; dw : DWORD; begin // open the process and store the process-handle hProcess := OpenProcess(SYNCHRONIZE or PROCESS_TERMINATE, False, dwProcID); // kill it if hProcess <> 0 then begin dw := Integer(TerminateProcess(hProcess, 1)); if dw <> 0 then begin // TerminateProcess returns immediately, so we have to verify the result via // WaitForSingleObject dw := WaitForSingleObject(hProcess, Wait); if dw = WAIT_FAILED then dw := GetLastError; end else // TerminateProcess = 0 dw := GetLastError; CloseHandle(hProcess); end else // hProcess = INVALID_HANDLE_VALUE dw := GetLastError; result := dw; end; |
Re: TerminateProcess not working correctly....
Thanks for your reply, but im not sure how to use this code?
What im trying to accomplish is this: I have 2 applications, Application 1 will create a process(Application 2). A timer inside application 1 will then check every 3seconds if application 2 is still running, thats basicly what i want to do, it's quite simple. So Application 1 does have the ProcessID and the Handle of Application 2, so i just want to check if application 2 is still running or not... |
Re: TerminateProcess not working correctly....
Let me just clear something:
I don't want to Terminate Application 2 inside Application 1, i basiclly just want to check inside application 1 if application 2 is still running or not? |
Re: TerminateProcess not working correctly....
Just use
![]() |
Re: TerminateProcess not working correctly....
Zitat:
Delphi-Quellcode:
try
//Create the Desktop Desktop.CreateDesktop(VIRTUAL_DESKTOP); FillChar(sinfo, SizeOf(sinfo), 0); sinfo.cb := SizeOf(sinfo); sinfo.lpDesktop := PChar(VIRTUAL_DESKTOP); sFileName := ExtractFilePath(Application.EXEName) + 'c_IPU_Sender_Appl.exe'; sFileName := sFileName +' '+ VIRTUAL_DESKTOP +' '+ Fs_LANUser; //Run the Message Sending Application on the new Desktop CreateProcess(nil,PChar(sFileName), nil, nil, False, 0, nil, nil, sinfo, pinfo); Fi_ProcessID := pinfo.dwProcessId; //Which to the new Desktop Desktop.SwitchToDesktop(VIRTUAL_DESKTOP); WaitForSingleObject(pinfo.hProcess, INFINITE); except on E: Exception do HandleError(E.Message); end; |
Alle Zeitangaben in WEZ +1. Es ist jetzt 21:17 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz