Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 562
M Schenkel Avatar asked Jan 26 '12 13:01

M Schenkel


People also ask

What is a handle count?

The handle count you see in Task Manager is "the number of object handles in the process's object table". In effect, this is the sum of all handles that this process has open.

What are handles in Process Explorer?

Handle is a utility that displays information about open handles for any process in the system. You can use it to see the programs that have a file open, or to see the object types and names of all the handles of a program. You can also get a GUI-based version of this program, Process Explorer, here at Sysinternals.


1 Answers

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;
like image 177
TLama Avatar answered Nov 08 '22 16:11

TLama