7

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.

TLama
  • 71,521
  • 15
  • 192
  • 348
M Schenkel
  • 6,001
  • 10
  • 56
  • 102

1 Answers1

12

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;
Victoria
  • 7,477
  • 2
  • 18
  • 40
TLama
  • 71,521
  • 15
  • 192
  • 348
  • 1
    Works well. I used this to detect resource leaks caused by not calling `Socket.Close()` when a network connection error occurs. – AlainD Nov 08 '17 at 16:33