From da12f10f63237e264d76604ef5e6d181c0d44572 Mon Sep 17 00:00:00 2001 From: Lomanic Date: Sun, 12 Nov 2017 20:24:44 +0100 Subject: [PATCH] Use w32.EnumerateProcesses instead of slower wmi in windows process.Processes() --- process/process_windows.go | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/process/process_windows.go b/process/process_windows.go index a1c3373..d023030 100644 --- a/process/process_windows.go +++ b/process/process_windows.go @@ -94,7 +94,7 @@ func init() { func Pids() ([]int32, error) { // inspired by https://gist.github.com/henkman/3083408 - // and https://github.com/giampaolo/psutil/blob/1c3a15f637521ba5c0031283da39c733fda53e4c/psutil/arch/windows/process_info.c#L315-L329 + // and https://github.com/giampaolo/psutil/blob/1c3a15f637521ba5c0031283da39c733fda53e4c/psutil/arch/windows/process_info.c#L315-L329 var ret []int32 var read uint32 = 0 var psSize uint32 = 1024 @@ -434,19 +434,14 @@ func (p *Process) getFromSnapProcess(pid int32) (int32, int32, string, error) { // Get processes func Processes() ([]*Process, error) { - var dst []Win32_Process - q := wmi.CreateQuery(&dst, "") - err := wmi.Query(q, &dst) + pids, err := Pids() if err != nil { - return []*Process{}, err - } - if len(dst) == 0 { - return []*Process{}, fmt.Errorf("could not get Process") + return []*Process{}, fmt.Errorf("could not get Processes %s", err) } results := []*Process{} - for _, proc := range dst { - p, err := NewProcess(int32(proc.ProcessID)) + for _, pid := range pids { + p, err := NewProcess(int32(pid)) if err != nil { continue }