Delphi获取EXE的句柄 [英] Delphi Get the handle of a EXE

查看:510
本文介绍了Delphi获取EXE的句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var 
客户端:String;
句柄:整数;
begin
客户端:='窗口名称';
GetWindowThreadProcessId(FindWindow(nil,PAnsiChar(Client)),@ProcessId);
句柄:= OpenProcess(PROCESS_ALL_ACCESS,False,ProcessId);
结束

我宁愿用其exe名称来抓取进程的句柄...
这是可能这个?

解决方案

您可以使用 ProcessInfo

  var 
ProcessInfo:TProcessInfo;
进程:TProcessItem;
PID:红衣主教;
ProcessHandle:THandle;
begin
ProcessInfo:= TProcessInfo.Create(nil);
try
进程:= ProcessInfo.RunningProcesses.FindByName('Notepad.exe');
如果分配(进程)然后
开始
PID:= Process.ProcessID;
ProcessHandle:= OpenProcess(PROCESS_ALL_ACCESS,False,PID);
如果ProcessHandle> 0然后
尝试
{在此添加您的代码}
finally
CloseHandle(ProcessHandle);
结束
结束
finally
ProcessInfo.Free;
结束
结束

如果您不喜欢使用第三方组件,则可以学习ProcessInfo的源代码以查看它如何检索正在运行的进程的列表及其属性。基本上它在Windows工具帮助API中继承了大部分功能。


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?

解决方案

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.

这篇关于Delphi获取EXE的句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆