Bryan
Bryan

Reputation: 85

Delphi Get the handle of a EXE

Heres an example of how I'm doing it right now :

var
Client : String;
Handle : Integer;
begin
Client := 'Window Name';
GetWindowThreadProcessId(FindWindow(nil, PAnsiChar(Client)), @ProcessId);
Handle := OpenProcess(PROCESS_ALL_ACCESS, False, ProcessId);
end;

I'd rather grab the Process's handle with its exe name... Is this possible this?

Upvotes: 4

Views: 34571

Answers (3)

That Marc
That Marc

Reputation: 1224

Since the link provided by vcldeveloper is broken, here's the full function code that works without 3rd party components.

First we will find process id (PID), and then we'll get process handle by opening all access (since the OP mentioned in the comments he will need this for ReadProcessMemory functionality).

If the function for PID returns 0, it means that the process is most likely not running (or just not found in the running process list)

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;

Next, we will get/open Process handle from the PID we got. The whole code/usage is as follows:

var myPID, myProcessHandle: integer;
begin
  myPID:=GetPIDbyProcessName('someExeName.exe');
  myProcessHandle:=OpenProcess(PROCESS_ALL_ACCESS,False,myPID);
end;

You should store the myProcessHandle in such way that it is accessable for
ReadProcessMemory(myProcessHandle...) as first parameter.

Also, add these to your global uses clauses:
Winapi.Windows (for ReadProcessMemory and OpenProcess)
Winapi.tlHelp32(for getting PID tProcessEntry32 variable)

Upvotes: 6

vcldeveloper
vcldeveloper

Reputation: 7489

You can use ProcessInfo:

var
  ProcessInfo : TProcessInfo;
  Process : TProcessItem;
  PID: Cardinal;
  ProcessHandle : THandle;
begin
  ProcessInfo := TProcessInfo.Create(nil);
  try
    Process := ProcessInfo.RunningProcesses.FindByName('Notepad.exe');
    if Assigned(Process) then
    begin
      PID := Process.ProcessID;
      ProcessHandle := OpenProcess(PROCESS_ALL_ACCESS,False,PID);
      if ProcessHandle > 0 then
      try
        {Add your code here}
      finally
        CloseHandle(ProcessHandle);
      end;
  end;
  finally
    ProcessInfo.Free;
  end;
end;

If you do not like using a third-party component, you can study source code of ProcessInfo to see how it retrieves list of running processes, and their properties. Basically it relays on Windows Tool Help API for most of its features.

Upvotes: 4

Abelisto
Abelisto

Reputation: 15614

Application.Handle?
Looks like you're trying to program in Delphi by WinAPI. In the vast majority do not need it, VCL provides appropriate object-oriented wrappers.
May be You will find something in this component pack: GLibWMI

Upvotes: 5

Related Questions