Delphi Get the handle of a EXE

29,373

Solution 1

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.

Solution 2

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)

Solution 3

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

Share:
29,373
Bryan
Author by

Bryan

Updated on July 05, 2022

Comments

  • Bryan
    Bryan almost 2 years

    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?

  • Andreas Rejbrand
    Andreas Rejbrand about 13 years
    I got the feeling the OP isn't looking for the handle of the own application.
  • Abelisto
    Abelisto about 13 years
    No. Unfortunately I haven't installed WinAPI help for now. But You can try to get help starting from API function EnumProcesses for example
  • Abelisto
    Abelisto about 13 years
    @Andreas Rejbrand Its look like that @Bryan trying to write virus on Delphi. Just a joke. Sorry for offtopic.
  • David Heffernan
    David Heffernan about 13 years
    @Abelisto @Bryan is attempting to write an online poker bot using SendKeys
  • Cosmin Prund
    Cosmin Prund about 13 years
    @Abelisto, you should edit or remove your answer, because in the new light it has nothing to do with the question.
  • Andreas Rejbrand
    Andreas Rejbrand about 13 years
    @Abelisto: "installed" WinAPI help? What is wrong with MSDN? msdn.microsoft.com/en-us/library/ms682629(v=vs.85).aspx
  • Bryan
    Bryan about 13 years
    @David I'm not attempting I have and sendkeys is only for bet ammounts >.>
  • Bryan
    Bryan about 13 years
    @David posting a image in a sec
  • Abelisto
    Abelisto about 13 years
    Comment deleted. @Andreas Rejbrand I using WinAPI help from Delphi5 at work. At home I using Delphi 2011 and I haven't any help files installed for it (for technical reasons). I know what is MSDN and I know where I can find it ))
  • David Heffernan
    David Heffernan about 13 years
    @bryan i still hope you get your just desserts and lose all your money when your bot bets on the wrong hand!!
  • Abelisto
    Abelisto about 13 years
    I think it's not the words that you have to say. In Russian community exists perfect site deplphikingdom.ru I hope, in English community exists site like this with section for novices.
  • David Heffernan
    David Heffernan about 13 years
    @Bryan That image you posted shows that your code still does not have any indentation. Are you aware of the fact that you are lacking in almost all of the fundamental skills of a programmer? If you were to listen to some of the good advice you have been offered here by any number of expert programmers, you would improve. You have enormous room for improvement. Do you even want to improve?
  • Bryan
    Bryan about 13 years
    how does it not have indentation explain?if there was a word wrap type thing in delphi where you could make it all pretty like id use it...
  • Bryan
    Bryan about 13 years
    also whats the point of flaming someone that is new to coding..this is one of my first major projects... and it doesn't help that the last project i was working on was over a year ago..I only got back into coding because a friend of mine that uses delphi alot wanted me to get back into it so we could work on stuff...
  • Bryan
    Bryan about 13 years
    and I'm sorry I'm not ocd about how my code looks ... all i do is comment it really...
  • David Heffernan
    David Heffernan about 13 years
    @bryan I'm not flaming. I'm talking about indentation not wrapping. Do a websearch to learn about it. Look at my answer to your question about the if statement. You would be far less likely to make such mistakes if you indented your code. Look at other people's code. Try to learn better ways of doing it.