-
Notifications
You must be signed in to change notification settings - Fork 0
/
windows.go
116 lines (103 loc) · 2.92 KB
/
windows.go
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package win
import (
"fmt"
"github.com/gonutz/w32/v2"
"github.com/lxn/win"
"golang.org/x/sys/windows"
"path/filepath"
"syscall"
"unsafe"
)
func StringToCharPtr(str string) *uint8 {
chars := append([]byte(str), 0)
return &chars[0]
}
// 回调函数,用于EnumWindows中的回调函数,第一个参数是hWnd,第二个是自定义穿的参数
func AddElementFunc(hWnd win.HWND, hWndList *[]win.HWND) uintptr {
*hWndList = append(*hWndList, hWnd)
return 1
}
// 获取桌面下的所有窗口句柄,包括没有Windows标题的或者是窗口的。
func GetDesktopWindowHWND() []win.HWND {
var hWndList []win.HWND
hL := &hWndList
_, _, err := syscall.Syscall(procEnumWindows.Addr(), 2, syscall.NewCallback(AddElementFunc), uintptr(unsafe.Pointer(hL)), 0)
if err != 0 {
fmt.Println(err)
}
return hWndList
}
func FindWindow(title, processName string) win.HWND {
hwnd, _, _ := findWindow.Call(0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(title))))
if hwnd == 0 {
handle, _ := syscall.CreateToolhelp32Snapshot(syscall.TH32CS_SNAPPROCESS, 0)
if handle == syscall.InvalidHandle {
return 0
}
defer syscall.CloseHandle(handle)
var processEntry syscall.ProcessEntry32
processEntry.Size = uint32(unsafe.Sizeof(processEntry))
for syscall.Process32Next(handle, &processEntry) == nil {
if syscall.UTF16ToString(processEntry.ExeFile[:]) == processName {
handle, _ := syscall.OpenProcess(syscall.PROCESS_QUERY_INFORMATION, false, processEntry.ProcessID)
if handle == 0 {
return 0
}
defer syscall.CloseHandle(handle)
hwnd, _, _ = findWindow.Call(0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(processName))))
break
}
}
}
return win.HWND(hwnd)
}
func findProcess(name string) uint32 {
processIDs, ok := w32.EnumProcesses(make([]uint32, 256))
if !ok {
return 0
}
for i := 0; i < len(processIDs); i++ {
if processIDs[i] != 0 {
if name == GetProcName(processIDs[i]) {
return processIDs[i]
}
}
}
return 0
}
func GetProcName(pid uint32) string {
if pid == 0 {
return "System Idle Process"
}
if pid == 4 {
return "System"
}
hProcess, err := windows.OpenProcess(
windows.PROCESS_VM_READ|windows.PROCESS_QUERY_INFORMATION,
false,
pid,
)
if err != nil {
fmt.Println(err)
}
defer windows.CloseHandle(hProcess)
buf := make([]uint16, syscall.MAX_LONG_PATH)
size := uint32(syscall.MAX_LONG_PATH)
if err := procQueryFullProcessImageNameW.Find(); err == nil { // Vista+
ret, _, err := procQueryFullProcessImageNameW.Call(
uintptr(hProcess),
uintptr(0),
uintptr(unsafe.Pointer(&buf[0])),
uintptr(unsafe.Pointer(&size)))
if ret == 0 {
fmt.Println(err)
}
return filepath.Base(windows.UTF16ToString(buf[:]))
}
// XP fallback
ret, _, err := procGetProcessImageFileNameW.Call(uintptr(hProcess), uintptr(unsafe.Pointer(&buf[0])), uintptr(size))
if ret == 0 {
fmt.Println(err)
}
return filepath.Base(ConvertDOSPath(windows.UTF16ToString(buf[:])))
}