go-tg-screenshot-bot

Форк
0
451 строка · 11.1 Кб
1
// Copyright 2010 The win Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4

5
// +build windows
6

7
package win
8

9
import (
10
	"golang.org/x/sys/windows"
11
	"syscall"
12
	"unsafe"
13
)
14

15
const MAX_PATH = 260
16

17
// Error codes
18
const (
19
	ERROR_SUCCESS             = 0
20
	ERROR_INVALID_FUNCTION    = 1
21
	ERROR_FILE_NOT_FOUND      = 2
22
	ERROR_INVALID_PARAMETER   = 87
23
	ERROR_INSUFFICIENT_BUFFER = 122
24
	ERROR_MORE_DATA           = 234
25
)
26

27
// GlobalAlloc flags
28
const (
29
	GHND          = 0x0042
30
	GMEM_FIXED    = 0x0000
31
	GMEM_MOVEABLE = 0x0002
32
	GMEM_ZEROINIT = 0x0040
33
	GPTR          = GMEM_FIXED | GMEM_ZEROINIT
34
)
35

36
// Predefined locale ids
37
const (
38
	LOCALE_CUSTOM_DEFAULT     LCID = 0x0c00
39
	LOCALE_CUSTOM_UI_DEFAULT  LCID = 0x1400
40
	LOCALE_CUSTOM_UNSPECIFIED LCID = 0x1000
41
	LOCALE_INVARIANT          LCID = 0x007f
42
	LOCALE_USER_DEFAULT       LCID = 0x0400
43
	LOCALE_SYSTEM_DEFAULT     LCID = 0x0800
44
)
45

46
// LCTYPE constants
47
const (
48
	LOCALE_SDECIMAL          LCTYPE = 14
49
	LOCALE_STHOUSAND         LCTYPE = 15
50
	LOCALE_SISO3166CTRYNAME  LCTYPE = 0x5a
51
	LOCALE_SISO3166CTRYNAME2 LCTYPE = 0x68
52
	LOCALE_SISO639LANGNAME   LCTYPE = 0x59
53
	LOCALE_SISO639LANGNAME2  LCTYPE = 0x67
54
)
55

56
var (
57
	// Library
58
	libkernel32 *windows.LazyDLL
59

60
	// Functions
61
	activateActCtx                     *windows.LazyProc
62
	closeHandle                        *windows.LazyProc
63
	createActCtx                       *windows.LazyProc
64
	fileTimeToSystemTime               *windows.LazyProc
65
	findResource                       *windows.LazyProc
66
	getConsoleTitle                    *windows.LazyProc
67
	getConsoleWindow                   *windows.LazyProc
68
	getCurrentThreadId                 *windows.LazyProc
69
	getLastError                       *windows.LazyProc
70
	getLocaleInfo                      *windows.LazyProc
71
	getLogicalDriveStrings             *windows.LazyProc
72
	getModuleHandle                    *windows.LazyProc
73
	getNumberFormat                    *windows.LazyProc
74
	getPhysicallyInstalledSystemMemory *windows.LazyProc
75
	getProfileString                   *windows.LazyProc
76
	getThreadLocale                    *windows.LazyProc
77
	getThreadUILanguage                *windows.LazyProc
78
	getVersion                         *windows.LazyProc
79
	globalAlloc                        *windows.LazyProc
80
	globalFree                         *windows.LazyProc
81
	globalLock                         *windows.LazyProc
82
	globalUnlock                       *windows.LazyProc
83
	moveMemory                         *windows.LazyProc
84
	mulDiv                             *windows.LazyProc
85
	loadResource                       *windows.LazyProc
86
	lockResource                       *windows.LazyProc
87
	setLastError                       *windows.LazyProc
88
	sizeofResource                     *windows.LazyProc
89
	systemTimeToFileTime               *windows.LazyProc
90
)
91

92
type (
93
	ATOM          uint16
94
	HANDLE        uintptr
95
	HGLOBAL       HANDLE
96
	HINSTANCE     HANDLE
97
	LCID          uint32
98
	LCTYPE        uint32
99
	LANGID        uint16
100
	HMODULE       uintptr
101
	HWINEVENTHOOK HANDLE
102
	HRSRC         uintptr
103
)
104

105
type FILETIME struct {
106
	DwLowDateTime  uint32
107
	DwHighDateTime uint32
108
}
109

110
type NUMBERFMT struct {
111
	NumDigits     uint32
112
	LeadingZero   uint32
113
	Grouping      uint32
114
	LpDecimalSep  *uint16
115
	LpThousandSep *uint16
116
	NegativeOrder uint32
117
}
118

119
type SYSTEMTIME struct {
120
	WYear         uint16
121
	WMonth        uint16
122
	WDayOfWeek    uint16
123
	WDay          uint16
124
	WHour         uint16
125
	WMinute       uint16
126
	WSecond       uint16
127
	WMilliseconds uint16
128
}
129

130
type ACTCTX struct {
131
	size                  uint32
132
	Flags                 uint32
133
	Source                *uint16 // UTF-16 string
134
	ProcessorArchitecture uint16
135
	LangID                uint16
136
	AssemblyDirectory     *uint16 // UTF-16 string
137
	ResourceName          *uint16 // UTF-16 string
138
	ApplicationName       *uint16 // UTF-16 string
139
	Module                HMODULE
140
}
141

142
func init() {
143
	// Library
144
	libkernel32 = windows.NewLazySystemDLL("kernel32.dll")
145

146
	// Functions
147
	activateActCtx = libkernel32.NewProc("ActivateActCtx")
148
	closeHandle = libkernel32.NewProc("CloseHandle")
149
	createActCtx = libkernel32.NewProc("CreateActCtxW")
150
	fileTimeToSystemTime = libkernel32.NewProc("FileTimeToSystemTime")
151
	findResource = libkernel32.NewProc("FindResourceW")
152
	getConsoleTitle = libkernel32.NewProc("GetConsoleTitleW")
153
	getConsoleWindow = libkernel32.NewProc("GetConsoleWindow")
154
	getCurrentThreadId = libkernel32.NewProc("GetCurrentThreadId")
155
	getLastError = libkernel32.NewProc("GetLastError")
156
	getLocaleInfo = libkernel32.NewProc("GetLocaleInfoW")
157
	getLogicalDriveStrings = libkernel32.NewProc("GetLogicalDriveStringsW")
158
	getModuleHandle = libkernel32.NewProc("GetModuleHandleW")
159
	getNumberFormat = libkernel32.NewProc("GetNumberFormatW")
160
	getPhysicallyInstalledSystemMemory = libkernel32.NewProc("GetPhysicallyInstalledSystemMemory")
161
	getProfileString = libkernel32.NewProc("GetProfileStringW")
162
	getThreadLocale = libkernel32.NewProc("GetThreadLocale")
163
	getThreadUILanguage = libkernel32.NewProc("GetThreadUILanguage")
164
	getVersion = libkernel32.NewProc("GetVersion")
165
	globalAlloc = libkernel32.NewProc("GlobalAlloc")
166
	globalFree = libkernel32.NewProc("GlobalFree")
167
	globalLock = libkernel32.NewProc("GlobalLock")
168
	globalUnlock = libkernel32.NewProc("GlobalUnlock")
169
	moveMemory = libkernel32.NewProc("RtlMoveMemory")
170
	mulDiv = libkernel32.NewProc("MulDiv")
171
	loadResource = libkernel32.NewProc("LoadResource")
172
	lockResource = libkernel32.NewProc("LockResource")
173
	setLastError = libkernel32.NewProc("SetLastError")
174
	sizeofResource = libkernel32.NewProc("SizeofResource")
175
	systemTimeToFileTime = libkernel32.NewProc("SystemTimeToFileTime")
176
}
177

178
func ActivateActCtx(ctx HANDLE) (uintptr, bool) {
179
	var cookie uintptr
180
	ret, _, _ := syscall.Syscall(activateActCtx.Addr(), 2,
181
		uintptr(ctx),
182
		uintptr(unsafe.Pointer(&cookie)),
183
		0)
184
	return cookie, ret != 0
185
}
186

187
func CloseHandle(hObject HANDLE) bool {
188
	ret, _, _ := syscall.Syscall(closeHandle.Addr(), 1,
189
		uintptr(hObject),
190
		0,
191
		0)
192

193
	return ret != 0
194
}
195

196
func CreateActCtx(ctx *ACTCTX) HANDLE {
197
	if ctx != nil {
198
		ctx.size = uint32(unsafe.Sizeof(*ctx))
199
	}
200
	ret, _, _ := syscall.Syscall(
201
		createActCtx.Addr(),
202
		1,
203
		uintptr(unsafe.Pointer(ctx)),
204
		0,
205
		0)
206
	return HANDLE(ret)
207
}
208

209
func FileTimeToSystemTime(lpFileTime *FILETIME, lpSystemTime *SYSTEMTIME) bool {
210
	ret, _, _ := syscall.Syscall(fileTimeToSystemTime.Addr(), 2,
211
		uintptr(unsafe.Pointer(lpFileTime)),
212
		uintptr(unsafe.Pointer(lpSystemTime)),
213
		0)
214

215
	return ret != 0
216
}
217

218
func FindResource(hModule HMODULE, lpName, lpType *uint16) HRSRC {
219
	ret, _, _ := syscall.Syscall(findResource.Addr(), 3,
220
		uintptr(hModule),
221
		uintptr(unsafe.Pointer(lpName)),
222
		uintptr(unsafe.Pointer(lpType)))
223

224
	return HRSRC(ret)
225
}
226

227
func GetConsoleTitle(lpConsoleTitle *uint16, nSize uint32) uint32 {
228
	ret, _, _ := syscall.Syscall(getConsoleTitle.Addr(), 2,
229
		uintptr(unsafe.Pointer(lpConsoleTitle)),
230
		uintptr(nSize),
231
		0)
232

233
	return uint32(ret)
234
}
235

236
func GetConsoleWindow() HWND {
237
	ret, _, _ := syscall.Syscall(getConsoleWindow.Addr(), 0,
238
		0,
239
		0,
240
		0)
241

242
	return HWND(ret)
243
}
244

245
func GetCurrentThreadId() uint32 {
246
	ret, _, _ := syscall.Syscall(getCurrentThreadId.Addr(), 0,
247
		0,
248
		0,
249
		0)
250

251
	return uint32(ret)
252
}
253

254
func GetLastError() uint32 {
255
	ret, _, _ := syscall.Syscall(getLastError.Addr(), 0,
256
		0,
257
		0,
258
		0)
259

260
	return uint32(ret)
261
}
262

263
func GetLocaleInfo(Locale LCID, LCType LCTYPE, lpLCData *uint16, cchData int32) int32 {
264
	ret, _, _ := syscall.Syscall6(getLocaleInfo.Addr(), 4,
265
		uintptr(Locale),
266
		uintptr(LCType),
267
		uintptr(unsafe.Pointer(lpLCData)),
268
		uintptr(cchData),
269
		0,
270
		0)
271

272
	return int32(ret)
273
}
274

275
func GetLogicalDriveStrings(nBufferLength uint32, lpBuffer *uint16) uint32 {
276
	ret, _, _ := syscall.Syscall(getLogicalDriveStrings.Addr(), 2,
277
		uintptr(nBufferLength),
278
		uintptr(unsafe.Pointer(lpBuffer)),
279
		0)
280

281
	return uint32(ret)
282
}
283

284
func GetModuleHandle(lpModuleName *uint16) HINSTANCE {
285
	ret, _, _ := syscall.Syscall(getModuleHandle.Addr(), 1,
286
		uintptr(unsafe.Pointer(lpModuleName)),
287
		0,
288
		0)
289

290
	return HINSTANCE(ret)
291
}
292

293
func GetNumberFormat(Locale LCID, dwFlags uint32, lpValue *uint16, lpFormat *NUMBERFMT, lpNumberStr *uint16, cchNumber int32) int32 {
294
	ret, _, _ := syscall.Syscall6(getNumberFormat.Addr(), 6,
295
		uintptr(Locale),
296
		uintptr(dwFlags),
297
		uintptr(unsafe.Pointer(lpValue)),
298
		uintptr(unsafe.Pointer(lpFormat)),
299
		uintptr(unsafe.Pointer(lpNumberStr)),
300
		uintptr(cchNumber))
301

302
	return int32(ret)
303
}
304

305
func GetPhysicallyInstalledSystemMemory(totalMemoryInKilobytes *uint64) bool {
306
	if getPhysicallyInstalledSystemMemory.Find() != nil {
307
		return false
308
	}
309
	ret, _, _ := syscall.Syscall(getPhysicallyInstalledSystemMemory.Addr(), 1,
310
		uintptr(unsafe.Pointer(totalMemoryInKilobytes)),
311
		0,
312
		0)
313

314
	return ret != 0
315
}
316

317
func GetProfileString(lpAppName, lpKeyName, lpDefault *uint16, lpReturnedString uintptr, nSize uint32) bool {
318
	ret, _, _ := syscall.Syscall6(getProfileString.Addr(), 5,
319
		uintptr(unsafe.Pointer(lpAppName)),
320
		uintptr(unsafe.Pointer(lpKeyName)),
321
		uintptr(unsafe.Pointer(lpDefault)),
322
		lpReturnedString,
323
		uintptr(nSize),
324
		0)
325
	return ret != 0
326
}
327

328
func GetThreadLocale() LCID {
329
	ret, _, _ := syscall.Syscall(getThreadLocale.Addr(), 0,
330
		0,
331
		0,
332
		0)
333

334
	return LCID(ret)
335
}
336

337
func GetThreadUILanguage() LANGID {
338
	if getThreadUILanguage.Find() != nil {
339
		return 0
340
	}
341

342
	ret, _, _ := syscall.Syscall(getThreadUILanguage.Addr(), 0,
343
		0,
344
		0,
345
		0)
346

347
	return LANGID(ret)
348
}
349

350
func GetVersion() uint32 {
351
	ret, _, _ := syscall.Syscall(getVersion.Addr(), 0,
352
		0,
353
		0,
354
		0)
355
	return uint32(ret)
356
}
357

358
func GlobalAlloc(uFlags uint32, dwBytes uintptr) HGLOBAL {
359
	ret, _, _ := syscall.Syscall(globalAlloc.Addr(), 2,
360
		uintptr(uFlags),
361
		dwBytes,
362
		0)
363

364
	return HGLOBAL(ret)
365
}
366

367
func GlobalFree(hMem HGLOBAL) HGLOBAL {
368
	ret, _, _ := syscall.Syscall(globalFree.Addr(), 1,
369
		uintptr(hMem),
370
		0,
371
		0)
372

373
	return HGLOBAL(ret)
374
}
375

376
func GlobalLock(hMem HGLOBAL) unsafe.Pointer {
377
	ret, _, _ := syscall.Syscall(globalLock.Addr(), 1,
378
		uintptr(hMem),
379
		0,
380
		0)
381

382
	return unsafe.Pointer(ret)
383
}
384

385
func GlobalUnlock(hMem HGLOBAL) bool {
386
	ret, _, _ := syscall.Syscall(globalUnlock.Addr(), 1,
387
		uintptr(hMem),
388
		0,
389
		0)
390

391
	return ret != 0
392
}
393

394
func MoveMemory(destination, source unsafe.Pointer, length uintptr) {
395
	syscall.Syscall(moveMemory.Addr(), 3,
396
		uintptr(unsafe.Pointer(destination)),
397
		uintptr(source),
398
		uintptr(length))
399
}
400

401
func MulDiv(nNumber, nNumerator, nDenominator int32) int32 {
402
	ret, _, _ := syscall.Syscall(mulDiv.Addr(), 3,
403
		uintptr(nNumber),
404
		uintptr(nNumerator),
405
		uintptr(nDenominator))
406

407
	return int32(ret)
408
}
409

410
func LoadResource(hModule HMODULE, hResInfo HRSRC) HGLOBAL {
411
	ret, _, _ := syscall.Syscall(loadResource.Addr(), 2,
412
		uintptr(hModule),
413
		uintptr(hResInfo),
414
		0)
415

416
	return HGLOBAL(ret)
417
}
418

419
func LockResource(hResData HGLOBAL) uintptr {
420
	ret, _, _ := syscall.Syscall(lockResource.Addr(), 1,
421
		uintptr(hResData),
422
		0,
423
		0)
424

425
	return ret
426
}
427

428
func SetLastError(dwErrorCode uint32) {
429
	syscall.Syscall(setLastError.Addr(), 1,
430
		uintptr(dwErrorCode),
431
		0,
432
		0)
433
}
434

435
func SizeofResource(hModule HMODULE, hResInfo HRSRC) uint32 {
436
	ret, _, _ := syscall.Syscall(sizeofResource.Addr(), 2,
437
		uintptr(hModule),
438
		uintptr(hResInfo),
439
		0)
440

441
	return uint32(ret)
442
}
443

444
func SystemTimeToFileTime(lpSystemTime *SYSTEMTIME, lpFileTime *FILETIME) bool {
445
	ret, _, _ := syscall.Syscall(systemTimeToFileTime.Addr(), 2,
446
		uintptr(unsafe.Pointer(lpSystemTime)),
447
		uintptr(unsafe.Pointer(lpFileTime)),
448
		0)
449

450
	return ret != 0
451
}
452

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.