this slowpoke moves

Get PID from any Process

uses TlHelp32, StdCtrls, psAPI

function GetParentProcessFileName(PID : DWORD): String;
var                              
  HandleSnapShot      : THandle;
  EntryParentProc     : TProcessEntry32;
  HandleParentProc    : THandle;
  ParentPID           : DWORD;
  ParentProcessFound  : Boolean;
  ParentProcPath      : PChar;
begin
  ParentProcessFound := False;
  HandleSnapShot     := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  GetMem(ParentProcPath, MAX_PATH);
  try
    if HandleSnapShot <> INVALID_HANDLE_VALUE then
    begin
      EntryParentProc.dwSize := SizeOf(EntryParentProc);
      if Process32First(HandleSnapShot, EntryParentProc) then
      begin
        repeat
          if EntryParentProc.th32ProcessID = PID then
          begin
            ParentPID  := EntryParentProc.th32ParentProcessID;
            HandleParentProc  := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, ParentPID);
            ParentProcessFound:= HandleParentProc <> 0;
            if ParentProcessFound then
            begin
                GetModuleFileNameEx(HandleParentProc, 0, PChar(ParentProcPath), MAX_PATH);
                ParentProcPath := PChar(ParentProcPath);
                CloseHandle(HandleParentProc);
            end;
            break;
          end;
        until not Process32Next(HandleSnapShot, EntryParentProc);
      end;
      CloseHandle(HandleSnapShot);
    end;
 
    if ParentProcessFound then
      Result := ParentProcPath
    else
      Result := '';
  finally
      FreeMem(ParentProcPath);
  end;
end;

function GetPIDbyProcessName(processName:String):integer;
var 
  GotProcess: Boolean;
  tempHandle: tHandle; 
  procE: tProcessEntry32;
begin
  tempHandle:=CreateToolHelp32SnapShot(TH32CS_SNAPALL, 0); 
  procE.dwSize:=SizeOf(procE); 
  GotProcess:=Process32First(tempHandle, procE);
  {$B-} 
    if GotProcess and not SameText(procE.szExeFile, processName) then 
      repeat GotProcess := Process32Next(tempHandle, procE); 
      until (not GotProcess) or SameText(procE.szExeFile,processName); 
  {$B+}

  if GotProcess then 
    result := procE.th32ProcessID 
  else
    result := 0; // process not found in running process list

  CloseHandle(tempHandle);
end;
Beispiel :
procedure TForm1.Button1Click(Sender: TObject);
var myPID, myProcessHandle: integer;
begin
  myPID := GetPIDbyProcessName('project1.exe');
  myProcessHandle := OpenProcess(PROCESS_ALL_ACCESS,False,myPID);
  Edit1.Text := IntToStr(myPID);
end;

// oder

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := IntToStr(GetPIDbyProcessName('Project1.exe'));
end;
Beispiel Programm von PID :
procedure TForm1.Button3Click(Sender: TObject);
var pid : DWORD;
    c : Cardinal;
begin
  c := StrToInt(Edit1.Text);
  PID := c;
  Label1.Caption := GetParentProcessFileName(PID);
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate