Skip to content

Commit

Permalink
Merge pull request shirou#609 from Lomanic/windows-process-children
Browse files Browse the repository at this point in the history
[process][windows] Use win32 API in process.Children() instead of slow WMI call
  • Loading branch information
Lomanic authored Nov 24, 2018
2 parents 852c768 + 6b53905 commit 0f70a4a
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions process/process_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ func (p *Process) Status() (string, error) {
func (p *Process) StatusWithContext(ctx context.Context) (string, error) {
return "", common.ErrNotImplementedError
}

func (p *Process) Username() (string, error) {
return p.UsernameWithContext(context.Background())
}
Expand Down Expand Up @@ -456,22 +457,33 @@ func (p *Process) Children() ([]*Process, error) {
}

func (p *Process) ChildrenWithContext(ctx context.Context) ([]*Process, error) {
var dst []Win32_Process
query := wmi.CreateQuery(&dst, fmt.Sprintf("Where ParentProcessId = %d", p.Pid))
err := common.WMIQueryWithContext(ctx, query, &dst)
if err != nil {
return nil, err
out := []*Process{}
snap := w32.CreateToolhelp32Snapshot(w32.TH32CS_SNAPPROCESS, uint32(0))
if snap == 0 {
return out, windows.GetLastError()
}
defer w32.CloseHandle(snap)
var pe32 w32.PROCESSENTRY32
pe32.DwSize = uint32(unsafe.Sizeof(pe32))
if w32.Process32First(snap, &pe32) == false {
return out, windows.GetLastError()
}

out := []*Process{}
for _, proc := range dst {
p, err := NewProcess(int32(proc.ProcessID))
if err != nil {
continue
if pe32.Th32ParentProcessID == uint32(p.Pid) {
p, err := NewProcess(int32(pe32.Th32ProcessID))
if err == nil {
out = append(out, p)
}
out = append(out, p)
}

for w32.Process32Next(snap, &pe32) {
if pe32.Th32ParentProcessID == uint32(p.Pid) {
p, err := NewProcess(int32(pe32.Th32ProcessID))
if err == nil {
out = append(out, p)
}
}
}
return out, nil
}

Expand Down

0 comments on commit 0f70a4a

Please sign in to comment.