如何获取属于某个进程的已打开句柄的数量?

M S*_*kel 7 windows delphi winapi handle window-handles

您可以使用Process Explorer程序查看运行应用程序的句柄数.有没有办法用Delphi代码来获取这个数字?我有兴趣跟踪应用程序本身的编号; 不要像Process Explorer那样找到其他应用程序使用的句柄数.

我的目的是让应用程序跟踪/检测可能的资源泄漏.

TLa*_*ama 12

使用该GetProcessHandleCount功能.此API函数是在Winapi.Windows单元导入的Delphi的最新版本中(因此您可以省略所呈现的版本):

function GetProcessHandleCount(hProcess: THandle; var pdwHandleCount: DWORD): BOOL; stdcall;
  external 'kernel32.dll';

procedure TForm1.Button1Click(Sender: TObject);
var
  HandleCount: DWORD;
begin
  if GetProcessHandleCount(GetCurrentProcess, HandleCount) then
    ShowMessage('Handle count: ' + IntToStr(HandleCount));
end;
Run Code Online (Sandbox Code Playgroud)