forked from auuunya/go-element
-
Notifications
You must be signed in to change notification settings - Fork 0
/
com.go
110 lines (98 loc) · 2.51 KB
/
com.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
package uiautomation
import (
"errors"
"log"
"syscall"
"unsafe"
)
var (
user32 = syscall.NewLazyDLL("user32.dll")
ole32 = syscall.NewLazyDLL("Ole32.dll")
procCoCreateInstance = ole32.NewProc("CoCreateInstance")
procCoInitialize = ole32.NewProc("CoInitialize")
procCoUninitialize = ole32.NewProc("CoUninitialize")
procFindWindowA = user32.NewProc("FindWindowA")
procFindWindowExA = user32.NewProc("FindWindowExA")
ErrorNotFoundWindow = errors.New("not found window")
)
func StringToCharPtr(str string) *uint8 {
return stringToCharPtr(str)
}
func GetWindowForString(classname, windowname string) uintptr {
find := findWindowA(classname, windowname)
if find == 0 {
log.Fatal(ErrorNotFoundWindow)
}
return find
}
func CoInitialize() error {
ret, _, _ := procCoInitialize.Call(
uintptr(0),
)
if ret != 0 {
return HResult(ret)
}
return nil
}
func CoUninitialize() {
procCoUninitialize.Call()
}
func CreateInstance(clsid, riid *syscall.GUID, clsctx CLSCTX) (unsafe.Pointer, error) {
return createInstance(clsid, riid, clsctx)
}
func FindWindowA(lpclass, lpwindow string) uintptr {
return findWindowA(lpclass, lpwindow)
}
func FindWindowExA(phwdn, chwdn uintptr, lpclass, lpwindow string) uintptr {
return findWindowExA(phwdn, chwdn, lpclass, lpwindow)
}
func createInstance(clsid, riid *syscall.GUID, clsctx CLSCTX) (unsafe.Pointer, error) {
var retVal unsafe.Pointer
ret, _, _ := procCoCreateInstance.Call(
uintptr(unsafe.Pointer(clsid)),
uintptr(0),
uintptr(clsctx),
uintptr(unsafe.Pointer(riid)),
uintptr(unsafe.Pointer(&retVal)),
)
if ret != 0 {
return nil, HResult(ret)
}
return retVal, nil
}
func stringToCharPtr(str string) *uint8 {
chars := append([]byte(str), 0) // null terminated
return &chars[0]
}
func findWindowA(lpclass, lpwindow string) uintptr {
lpclassname := uintptr(0)
lpwindowname := uintptr(0)
if lpclass != "" {
lpclassname = uintptr(unsafe.Pointer(stringToCharPtr(lpclass)))
}
if lpwindow != "" {
lpwindowname = uintptr(unsafe.Pointer(stringToCharPtr(lpwindow)))
}
ret, _, _ := procFindWindowA.Call(
lpclassname,
lpwindowname,
)
return ret
}
func findWindowExA(phwdn, chwdn uintptr, lpclass, lpwindow string) uintptr {
lpclassname := uintptr(0)
lpwindowname := uintptr(0)
if lpclass != "" {
lpclassname = uintptr(unsafe.Pointer(stringToCharPtr(lpclass)))
}
if lpwindow != "" {
lpwindowname = uintptr(unsafe.Pointer(stringToCharPtr(lpwindow)))
}
ret, _, _ := procFindWindowA.Call(
phwdn,
chwdn,
lpclassname,
lpwindowname,
)
return ret
}