How to get count of opened handles that belongs to a certain process?

╄→гoц情女王★ 提交于 2019-12-08 21:08:43

问题


You can use the program Process Explorer to see how many handles running applications have. Is there a way with Delphi code to get this number? I am interested in tracking the number for the application itself; not to find the number of handles used by other applications as Process Explorer is doing.

My intention is for the application to track/detect possible resource leaks.


回答1:


Use the GetProcessHandleCount function. This API function is in recent versions of Delphi imported by the Winapi.Windows unit (so you can omit the presented one):

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;


来源:https://stackoverflow.com/questions/9018903/how-to-get-count-of-opened-handles-that-belongs-to-a-certain-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!