Skip to content

Commit fdc3c05

Browse files
authored
Merge pull request #1712 from al-online/numfds_for_windows
implement process:NumFDs for Windows
2 parents 6c06ac9 + 462736c commit fdc3c05

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

process/process_windows.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ var (
4343
procGetPriorityClass = common.Modkernel32.NewProc("GetPriorityClass")
4444
procGetProcessIoCounters = common.Modkernel32.NewProc("GetProcessIoCounters")
4545
procGetNativeSystemInfo = common.Modkernel32.NewProc("GetNativeSystemInfo")
46+
procGetProcessHandleCount = common.Modkernel32.NewProc("GetProcessHandleCount")
4647

4748
processorArchitecture uint
4849
)
@@ -548,8 +549,21 @@ func (p *Process) NumCtxSwitchesWithContext(ctx context.Context) (*NumCtxSwitche
548549
return nil, common.ErrNotImplementedError
549550
}
550551

552+
// NumFDsWithContext returns the number of handles for a process on Windows,
553+
// not the number of file descriptors (FDs).
551554
func (p *Process) NumFDsWithContext(ctx context.Context) (int32, error) {
552-
return 0, common.ErrNotImplementedError
555+
handle, err := windows.OpenProcess(processQueryInformation, false, uint32(p.Pid))
556+
if err != nil {
557+
return 0, err
558+
}
559+
defer windows.CloseHandle(handle)
560+
561+
var handleCount uint32
562+
ret, _, err := procGetProcessHandleCount.Call(uintptr(handle), uintptr(unsafe.Pointer(&handleCount)))
563+
if ret == 0 {
564+
return 0, err
565+
}
566+
return int32(handleCount), nil
553567
}
554568

555569
func (p *Process) NumThreadsWithContext(ctx context.Context) (int32, error) {

0 commit comments

Comments
 (0)