From cbc32afb654d89fb1d3a39c18624ff37a775e86b Mon Sep 17 00:00:00 2001 From: huang <2971803929@qq.com> Date: Thu, 19 Sep 2024 23:59:11 +0800 Subject: [PATCH 1/2] implement NumFDs for Windows --- process/process_windows.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/process/process_windows.go b/process/process_windows.go index 52e1086f7..f8fa2f456 100644 --- a/process/process_windows.go +++ b/process/process_windows.go @@ -43,6 +43,7 @@ var ( procGetPriorityClass = common.Modkernel32.NewProc("GetPriorityClass") procGetProcessIoCounters = common.Modkernel32.NewProc("GetProcessIoCounters") procGetNativeSystemInfo = common.Modkernel32.NewProc("GetNativeSystemInfo") + procGetProcessHandleCount = common.Modkernel32.NewProc("GetProcessHandleCount") processorArchitecture uint ) @@ -549,7 +550,18 @@ func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitche } func (p *Process) NumFDsWithContext(ctx context.Context) (int32, error) { - return 0, common.ErrNotImplementedError + handle, err := windows.OpenProcess(processQueryInformation, false, uint32(p.Pid)) + if err != nil { + return 0, err + } + defer windows.CloseHandle(handle) + + var handleCount uint32 + ret, _, err := procGetProcessHandleCount.Call(uintptr(handle), uintptr(unsafe.Pointer(&handleCount))) + if ret == 0 { + return 0, err + } + return int32(handleCount), nil } func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) { From 462736cb8b6c3a0bce352908303593e649491993 Mon Sep 17 00:00:00 2001 From: huang <2971803929@qq.com> Date: Mon, 23 Sep 2024 22:05:49 +0800 Subject: [PATCH 2/2] add comment for NumFDsWithContext --- process/process_windows.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/process/process_windows.go b/process/process_windows.go index f8fa2f456..b00c671e9 100644 --- a/process/process_windows.go +++ b/process/process_windows.go @@ -549,6 +549,8 @@ func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitche return nil, common.ErrNotImplementedError } +// NumFDsWithContext returns the number of handles for a process on Windows, +// not the number of file descriptors (FDs). func (p *Process) NumFDsWithContext(ctx context.Context) (int32, error) { handle, err := windows.OpenProcess(processQueryInformation, false, uint32(p.Pid)) if err != nil {