-
Notifications
You must be signed in to change notification settings - Fork 2
/
hinstance.go
229 lines (207 loc) Β· 7.03 KB
/
hinstance.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package wingo
import (
proc2 "github.com/rogeecn/wingo/proc"
"runtime"
"syscall"
"unsafe"
"github.com/rogeecn/wingo/co"
"github.com/rogeecn/wingo/errco"
)
// A handle to an instance. This is the base address of the module in memory.
//
// π https://docs.microsoft.com/en-us/windows/win32/winprog/windows-data-types#hinstance
type HINSTANCE HANDLE
// If moduleName is nil, returns a handle to the file used to create the calling
// process (.exe file).
//
// π https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulehandlew
func GetModuleHandle(moduleName StrOrNil) HINSTANCE {
ret, _, err := syscall.Syscall(proc2.GetModuleHandle.Addr(), 1,
uintptr(variantStrOrNil(moduleName)), 0, 0)
if ret == 0 {
panic(errco.ERROR(err))
}
return HINSTANCE(ret)
}
// β οΈ You must defer HINSTANCE.FreeLibrary().
//
// π https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryw
func LoadLibrary(libFileName string) HINSTANCE {
ret, _, err := syscall.Syscall(proc2.LoadLibrary.Addr(), 1,
uintptr(unsafe.Pointer(Str.ToNativePtr(libFileName))),
0, 0)
if ret == 0 {
panic(errco.ERROR(err))
}
return HINSTANCE(ret)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-createdialogparamw
func (hInst HINSTANCE) CreateDialogParam(
templateName ResId, hwndParent HWND,
dialogFunc uintptr, dwInitParam LPARAM) HWND {
templateNameVal, templateNameBuf := variantResId(templateName)
ret, _, err := syscall.Syscall6(proc2.CreateDialogParam.Addr(), 5,
uintptr(hInst), templateNameVal,
uintptr(hwndParent), dialogFunc, uintptr(dwInitParam), 0)
runtime.KeepAlive(templateNameBuf)
if ret == 0 {
panic(errco.ERROR(err))
}
return HWND(ret)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-dialogboxparamw
func (hInst HINSTANCE) DialogBoxParam(
templateName ResId, hwndParent HWND,
dialogFunc uintptr, dwInitParam LPARAM) uintptr {
templateNameVal, templateNameBuf := variantResId(templateName)
ret, _, err := syscall.Syscall6(proc2.DialogBoxParam.Addr(), 5,
uintptr(hInst), templateNameVal,
uintptr(hwndParent), dialogFunc, uintptr(dwInitParam), 0)
runtime.KeepAlive(templateNameBuf)
if int(ret) == -1 && errco.ERROR(err) != errco.SUCCESS {
panic(errco.ERROR(err))
}
return ret
}
// π https://docs.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-duplicateicon
func (hInst HINSTANCE) DuplicateIcon(hIcon HICON) HICON {
ret, _, err := syscall.Syscall(proc2.DuplicateIcon.Addr(), 2,
uintptr(hInst), uintptr(hIcon), 0)
if ret == 0 {
panic(errco.ERROR(err))
}
return HICON(ret)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-freelibrary
func (hInst HINSTANCE) FreeLibrary() {
ret, _, err := syscall.Syscall(proc2.FreeLibrary.Addr(), 1,
uintptr(hInst), 0, 0)
if ret == 0 {
panic(errco.ERROR(err))
}
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getclassinfoexw
func (hInst HINSTANCE) GetClassInfoEx(
className *uint16, destBuf *WNDCLASSEX) (ATOM, error) {
ret, _, err := syscall.Syscall(proc2.GetClassInfoEx.Addr(), 3,
uintptr(hInst),
uintptr(unsafe.Pointer(className)),
uintptr(unsafe.Pointer(destBuf)))
if ret == 0 {
return ATOM(0), errco.ERROR(err)
}
return ATOM(ret), nil
}
// Example retrieving own .exe path:
//
// exePath := win.HINSTANCE(0).GetModuleFileName()
// fmt.Printf("Current .exe path: %s\n", exePath)
//
// π https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getmodulefilenamew
func (hInst HINSTANCE) GetModuleFileName() string {
var buf [_MAX_PATH + 1]uint16
ret, _, err := syscall.Syscall(proc2.GetModuleFileName.Addr(), 3,
uintptr(hInst), uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)))
if ret == 0 {
panic(errco.ERROR(err))
}
return Str.FromNativeSlice(buf[:])
}
// π https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getprocaddress
func (hInst HINSTANCE) GetProcAddress(procName string) uintptr {
ascii := []byte(procName)
ascii = append(ascii, 0x00) // terminating null
ret, _, err := syscall.Syscall(proc2.GetProcAddress.Addr(), 2,
uintptr(hInst), uintptr(unsafe.Pointer(&ascii[0])), 0)
if ret == 0 {
panic(errco.ERROR(err))
}
return ret
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-loadacceleratorsw
func (hInst HINSTANCE) LoadAccelerators(tableName ResId) HACCEL {
tableNameVal, tableNameBuf := variantResId(tableName)
ret, _, err := syscall.Syscall(proc2.LoadAccelerators.Addr(), 2,
uintptr(hInst), tableNameVal, 0)
runtime.KeepAlive(tableNameBuf)
if ret == 0 {
panic(errco.ERROR(err))
}
return HACCEL(ret)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-loadcursorw
func (hInst HINSTANCE) LoadCursor(cursorName CursorRes) HCURSOR {
cursorNameVal, cursorNameBuf := variantCursorResId(cursorName)
ret, _, err := syscall.Syscall(proc2.LoadCursor.Addr(), 2,
uintptr(hInst), cursorNameVal, 0)
runtime.KeepAlive(cursorNameBuf)
if ret == 0 {
panic(errco.ERROR(err))
}
return HCURSOR(ret)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-loadiconw
func (hInst HINSTANCE) LoadIcon(iconName IconRes) HICON {
iconNameVal, iconNameBuf := variantIconResId(iconName)
ret, _, err := syscall.Syscall(proc2.LoadIcon.Addr(), 2,
uintptr(hInst), iconNameVal, 0)
runtime.KeepAlive(iconNameBuf)
if ret == 0 {
panic(errco.ERROR(err))
}
return HICON(ret)
}
// Returned HGDIOBJ must be cast into HBITMAP, HCURSOR or HICON.
//
// β οΈ If the object is not being loaded from the application resources, you must
// defer its respective DeleteObject().
//
// Example loading a 16x16 icon resource:
//
// const MY_ICON_ID int = 101
//
// hIcon := win.HICON(
// win.GetModuleHandle(nil).LoadImage(
// win.ResIdInt(MY_ICON_ID),
// co.IMAGE_ICON,
// 16, 16,
// co.LR_DEFAULTCOLOR,
// ),
// )
//
// Example loading a bitmap from file:
//
// hBmp := win.HBITMAP(
// win.HINSTANCE(0).LoadImage(
// win.ResIdStr("C:\\Temp\\image.bmp"),
// co.IMAGE_BITMAP,
// 0, 0,
// co.LR_LOADFROMFILE,
// ),
// )
// defer hBmp.DeleteObject()
//
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-loadimagew
func (hInst HINSTANCE) LoadImage(
name ResId, imgType co.IMAGE, cx, cy int32, fuLoad co.LR) HGDIOBJ {
nameVal, nameBuf := variantResId(name)
ret, _, err := syscall.Syscall6(proc2.LoadImage.Addr(), 6,
uintptr(hInst), nameVal, uintptr(imgType),
uintptr(cx), uintptr(cy), uintptr(fuLoad))
runtime.KeepAlive(nameBuf)
if ret == 0 {
panic(errco.ERROR(err))
}
return HGDIOBJ(ret)
}
// π https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-loadmenuw
func (hInst HINSTANCE) LoadMenu(menuName ResId) HMENU {
menuNameVal, menuNameBuf := variantResId(menuName)
ret, _, err := syscall.Syscall(proc2.LoadMenu.Addr(), 2,
uintptr(hInst), menuNameVal, 0)
runtime.KeepAlive(menuNameBuf)
if ret == 0 {
panic(errco.ERROR(err))
}
return HMENU(ret)
}