PID von Elternprozess ermitteln
Hallo,
ich hab hier einen Weg gefunden die PID des Parentprozesses zu ermitteln und habe das mal für mich nach Delphi übersetzt. Original: http://www.codeproject.com/Articles/...nt-Process-PID Meine Delphi Version:
Delphi-Quellcode:
Ich hab ein bisschen vom Original abgeschnitten, wie das Ermitteln des Prozessnamens, aber das kann jeder nach belieben dazumachen.
uses Windows, TlHelp32;
function GetParentPID:Cardinal; var crtpid : Cardinal; bContinue : Boolean; hSnapshot : THANDLE; procentry : PROCESSENTRY32A; begin hSnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0 ); bContinue := Process32FirstA( hSnapShot, procentry ) ; Result := 0; crtpid := GetCurrentProcessId(); while( bContinue ) do begin if(crtpid = procentry.th32ProcessID)then Result := procentry.th32ParentProcessID; procentry.dwSize := sizeof(PROCESSENTRY32) ; bContinue := (Result = 0) and Process32NextA( hSnapShot, procentry ); end; end; Da ich gesehen habe, dass die Frage nach dieser Funktion öfters auftaucht aber ich die Funktion selbst nicht gefunden habe, dachte ich ich teile das hier mal. Habt ihr noch Anregungen dazu? Wie ich euch kenne bestimmt ;) :angel: |
AW: PID von Elternprozess ermitteln
Warum setzt du in der Schleife jedes mal dwSize neu? Die Größe der Struktur ändert sich doch nicht. Und zu jedem CreateToolhelp32Snapshot gehört auch ein CloseHandle, siehe dazu die Dokumentation.
|
AW: PID von Elternprozess ermitteln
Das mit der Size hab ich mich auch grad beim nochmal durchlesen gefragt :gruebel:
Wie gesagt eins zu eins abgeschrieben und dann die gescheiften Klammern in begin und end gemacht und halöt noch die restliche Syntax angepasst aber mehr nicht :wall: Hier nochmal überarbeitet:
Delphi-Quellcode:
Edit: Vermutlich muss ich noch auf den Rückgabewert von CreateToolhelp32Snapshot eingehen, das muss ich mir noch durchlesen.
uses Windows, TlHelp32;
function GetParentPID:Cardinal; var crtpid : Cardinal; bContinue : Boolean; hSnapshot : THANDLE; procentry : PROCESSENTRY32A; begin Result := 0; hSnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0 ); bContinue := Process32FirstA( hSnapShot, procentry ) ; if bContinue then begin procentry.dwSize := sizeof(PROCESSENTRY32) ; crtpid := GetCurrentProcessId(); while( bContinue ) do begin if(crtpid = procentry.th32ProcessID)then Result := procentry.th32ParentProcessID; bContinue := (Result = 0) and Process32NextA( hSnapShot, procentry ); end; end; CloseHandle(hSnapshot); end; |
AW: PID von Elternprozess ermitteln
Und was passiert, wenn fehlschlägt, warum auch immer? Les dir doch bitte mal die Dokumentation durch.
|
AW: PID von Elternprozess ermitteln
Ohne es jetzt ausprobiert zu haben, wie wäre es denn so?
Delphi-Quellcode:
function GetParentPID:Cardinal;
var crtpid : Cardinal; bContinue : Boolean; hSnapshot : THANDLE; procentry : PROCESSENTRY32A; begin Result := 0; procentry.dwSize := sizeof(PROCESSENTRY32) ; hSnapShot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if hSnapshot <> INVALID_HANDLE_VALUE then try crtpid := GetCurrentProcessId(); bContinue := Process32FirstA(hSnapShot, procentry); while bContinue do begin if crtpid = procentry.th32ProcessID then Result := procentry.th32ParentProcessID; bContinue := (Result = 0) and Process32NextA(hSnapShot, procentry); end; finally CloseHandle(hSnapshot); end; end; |
AW: PID von Elternprozess ermitteln
Öhm ja so hatte ich das jetzt auch gemacht und eig auch gepostet :gruebel: warum hats das hier nich reingemacht? :shock: naja whatever es geht :mrgreen:
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 11:39 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz