Use w32.EnumerateProcesses instead of slower wmi in windows process.Processes()

This commit is contained in:
Lomanic 2017-11-12 20:24:44 +01:00
parent c9a24cf2d0
commit da12f10f63
1 changed files with 5 additions and 10 deletions

View File

@ -94,7 +94,7 @@ func init() {
func Pids() ([]int32, error) { func Pids() ([]int32, error) {
// inspired by https://gist.github.com/henkman/3083408 // 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 ret []int32
var read uint32 = 0 var read uint32 = 0
var psSize uint32 = 1024 var psSize uint32 = 1024
@ -434,19 +434,14 @@ func (p *Process) getFromSnapProcess(pid int32) (int32, int32, string, error) {
// Get processes // Get processes
func Processes() ([]*Process, error) { func Processes() ([]*Process, error) {
var dst []Win32_Process pids, err := Pids()
q := wmi.CreateQuery(&dst, "")
err := wmi.Query(q, &dst)
if err != nil { if err != nil {
return []*Process{}, err return []*Process{}, fmt.Errorf("could not get Processes %s", err)
}
if len(dst) == 0 {
return []*Process{}, fmt.Errorf("could not get Process")
} }
results := []*Process{} results := []*Process{}
for _, proc := range dst { for _, pid := range pids {
p, err := NewProcess(int32(proc.ProcessID)) p, err := NewProcess(int32(pid))
if err != nil { if err != nil {
continue continue
} }