Einzelnen Beitrag anzeigen

FaNIX

Registriert seit: 8. Okt 2007
36 Beiträge
 
#1

TerminateProcess not working correctly....

  Alt 22. Okt 2007, 07:30
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...
  Mit Zitat antworten Zitat