Skip to content

Commit 1caf4d4

Browse files
committed
add processlist example
1 parent ed42477 commit 1caf4d4

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

examples/processlist/main.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"unsafe"
6+
7+
"github.com/hallazzang/go-windows-programming/pkg/win"
8+
)
9+
10+
type processInfo struct {
11+
id uint32
12+
name string
13+
sessionID uint32
14+
}
15+
16+
func processList() ([]processInfo, error) {
17+
var pProcessInfo *win.WTS_PROCESS_INFO
18+
var count uint32
19+
var ps []processInfo
20+
if win.WTSEnumerateProcesses(win.WTS_CURRENT_SERVER_HANDLE, 0, 1, &pProcessInfo, &count) == 0 {
21+
return nil, win.GetLastError()
22+
}
23+
defer win.WTSFreeMemory(unsafe.Pointer(pProcessInfo))
24+
size := unsafe.Sizeof(win.WTS_PROCESS_INFO{})
25+
for i := uint32(0); i < count; i++ {
26+
p := *(*win.WTS_PROCESS_INFO)(unsafe.Pointer(uintptr(unsafe.Pointer(pProcessInfo)) + uintptr(size)*uintptr(i)))
27+
ps = append(ps, processInfo{
28+
id: p.ProcessId,
29+
name: win.UTF16PtrToString(p.PProcessName),
30+
sessionID: p.SessionId,
31+
})
32+
}
33+
return ps, nil
34+
}
35+
36+
func main() {
37+
ps, err := processList()
38+
if err != nil {
39+
panic(err)
40+
}
41+
fmt.Printf("process list:\n%+v\n", ps)
42+
}

0 commit comments

Comments
 (0)