1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
//go:build windows
// +build windows
package process
import (
"unsafe"
"golang.org/x/sys/windows"
)
// EnumProcesses returns a slice containing the process IDs of all processes
// currently running on the system.
func EnumProcesses() ([]uint32, error) {
count := 256
uint32Size := unsafe.Sizeof(uint32(0))
for {
buf := make([]uint32, count)
bufferSize := uint32(len(buf) * int(uint32Size))
retBufferSize := uint32(0)
if err := enumProcesses(&buf[0], bufferSize, &retBufferSize); err != nil {
return nil, err
}
if retBufferSize == bufferSize {
count = count * 2
continue
}
actualCount := retBufferSize / uint32(uint32Size)
return buf[:actualCount], nil
}
}
// ProcessMemoryCountersEx is the PROCESS_MEMORY_COUNTERS_EX struct from
// Windows:
// https://docs.microsoft.com/en-us/windows/win32/api/psapi/ns-psapi-process_memory_counters_ex
//
//nolint:revive // process.ProcessMemoryCountersEx stutters, too late to change it
type ProcessMemoryCountersEx struct {
Cb uint32
PageFaultCount uint32
PeakWorkingSetSize uint
WorkingSetSize uint
QuotaPeakPagedPoolUsage uint
QuotaPagedPoolUsage uint
QuotaPeakNonPagedPoolUsage uint
QuotaNonPagedPoolUsage uint
PagefileUsage uint
PeakPagefileUsage uint
PrivateUsage uint
}
// GetProcessMemoryInfo returns the memory usage information for the given
// process. The process handle must have the PROCESS_QUERY_INFORMATION or
// PROCESS_QUERY_LIMITED_INFORMATION, and the PROCESS_VM_READ access rights.
func GetProcessMemoryInfo(process windows.Handle) (*ProcessMemoryCountersEx, error) {
memCounters := &ProcessMemoryCountersEx{}
size := unsafe.Sizeof(*memCounters)
if err := getProcessMemoryInfo(process, memCounters, uint32(size)); err != nil {
return nil, err
}
return memCounters, nil
}
// These constants are used with QueryFullProcessImageName's flags.
const (
// ImageNameFormatWin32Path indicates to format the name as a Win32 path.
ImageNameFormatWin32Path = iota
// ImageNameFormatNTPath indicates to format the name as a NT path.
ImageNameFormatNTPath
)
// QueryFullProcessImageName returns the full process image name for the given
// process. The process handle must have the PROCESS_QUERY_INFORMATION or
// PROCESS_QUERY_LIMITED_INFORMATION access right. The flags can be either
// `ImageNameFormatWin32Path` or `ImageNameFormatNTPath`.
func QueryFullProcessImageName(process windows.Handle, flags uint32) (string, error) {
bufferSize := uint32(256)
for {
b := make([]uint16, bufferSize)
err := queryFullProcessImageName(process, flags, &b[0], &bufferSize)
if err == windows.ERROR_INSUFFICIENT_BUFFER { //nolint:errorlint // err is Errno
bufferSize = bufferSize * 2
continue
}
if err != nil {
return "", err
}
return windows.UTF16ToString(b[:bufferSize]), nil
}
}
|