AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Win32/Win64 API (native code) Delphi TerminateProcess not working correctly....

TerminateProcess not working correctly....

Ein Thema von FaNIX · begonnen am 22. Okt 2007 · letzter Beitrag vom 22. Okt 2007
Antwort Antwort
FaNIX

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

TerminateProcess not working correctly....

  Alt 22. Okt 2007, 08: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
Benutzerbild von sirius
sirius

Registriert seit: 3. Jan 2007
Ort: Dresden
3.443 Beiträge
 
Delphi 7 Enterprise
 
#2

Re: TerminateProcess not working correctly....

  Alt 22. Okt 2007, 09:02
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.
Dieser Beitrag ist für Jugendliche unter 18 Jahren nicht geeignet.
  Mit Zitat antworten Zitat
FaNIX

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

Re: TerminateProcess not working correctly....

  Alt 22. Okt 2007, 09:45
Zitat von sirius:
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.
I wish it was that, but it's not, even after 10s it still picks up the process, even though it's not running... Maybe it's because im using the same hProcess Handle? And the crap part of this is, is that the GetProcessID DOESN'T work in Windows 2000
  Mit Zitat antworten Zitat
Benutzerbild von Luckie
Luckie
(Moderator)

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#4

Re: TerminateProcess not working correctly....

  Alt 22. Okt 2007, 09:52
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;
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat
FaNIX

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

Re: TerminateProcess not working correctly....

  Alt 22. Okt 2007, 09:57
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...
  Mit Zitat antworten Zitat
FaNIX

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

Re: TerminateProcess not working correctly....

  Alt 22. Okt 2007, 10:00
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?
  Mit Zitat antworten Zitat
Benutzerbild von sirius
sirius

Registriert seit: 3. Jan 2007
Ort: Dresden
3.443 Beiträge
 
Delphi 7 Enterprise
 
#7

Re: TerminateProcess not working correctly....

  Alt 22. Okt 2007, 10:05
Just use MSDN-Library durchsuchenwaitforsingleobject with a time out intervall of 0 milliseconds
Dieser Beitrag ist für Jugendliche unter 18 Jahren nicht geeignet.
  Mit Zitat antworten Zitat
FaNIX

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

Re: TerminateProcess not working correctly....

  Alt 22. Okt 2007, 10:24
Zitat von sirius:
Just use MSDN-Library durchsuchenwaitforsingleobject with a time out intervall of 0 milliseconds
Ahhh genius!!! It work's perfectly... thanks mate!

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;
  Mit Zitat antworten Zitat
Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 17:50 Uhr.
Powered by vBulletin® Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2020 by Daniel R. Wolf