go-tg-screenshot-bot
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
7package win8
9import (10"golang.org/x/sys/windows"11"syscall"12"unsafe"13)
14
15const MAX_PATH = 26016
17// Error codes
18const (19ERROR_SUCCESS = 020ERROR_INVALID_FUNCTION = 121ERROR_FILE_NOT_FOUND = 222ERROR_INVALID_PARAMETER = 8723ERROR_INSUFFICIENT_BUFFER = 12224ERROR_MORE_DATA = 23425)
26
27// GlobalAlloc flags
28const (29GHND = 0x004230GMEM_FIXED = 0x000031GMEM_MOVEABLE = 0x000232GMEM_ZEROINIT = 0x004033GPTR = GMEM_FIXED | GMEM_ZEROINIT34)
35
36// Predefined locale ids
37const (38LOCALE_CUSTOM_DEFAULT LCID = 0x0c0039LOCALE_CUSTOM_UI_DEFAULT LCID = 0x140040LOCALE_CUSTOM_UNSPECIFIED LCID = 0x100041LOCALE_INVARIANT LCID = 0x007f42LOCALE_USER_DEFAULT LCID = 0x040043LOCALE_SYSTEM_DEFAULT LCID = 0x080044)
45
46// LCTYPE constants
47const (48LOCALE_SDECIMAL LCTYPE = 1449LOCALE_STHOUSAND LCTYPE = 1550LOCALE_SISO3166CTRYNAME LCTYPE = 0x5a51LOCALE_SISO3166CTRYNAME2 LCTYPE = 0x6852LOCALE_SISO639LANGNAME LCTYPE = 0x5953LOCALE_SISO639LANGNAME2 LCTYPE = 0x6754)
55
56var (57// Library58libkernel32 *windows.LazyDLL59
60// Functions61activateActCtx *windows.LazyProc62closeHandle *windows.LazyProc63createActCtx *windows.LazyProc64fileTimeToSystemTime *windows.LazyProc65findResource *windows.LazyProc66getConsoleTitle *windows.LazyProc67getConsoleWindow *windows.LazyProc68getCurrentThreadId *windows.LazyProc69getLastError *windows.LazyProc70getLocaleInfo *windows.LazyProc71getLogicalDriveStrings *windows.LazyProc72getModuleHandle *windows.LazyProc73getNumberFormat *windows.LazyProc74getPhysicallyInstalledSystemMemory *windows.LazyProc75getProfileString *windows.LazyProc76getThreadLocale *windows.LazyProc77getThreadUILanguage *windows.LazyProc78getVersion *windows.LazyProc79globalAlloc *windows.LazyProc80globalFree *windows.LazyProc81globalLock *windows.LazyProc82globalUnlock *windows.LazyProc83moveMemory *windows.LazyProc84mulDiv *windows.LazyProc85loadResource *windows.LazyProc86lockResource *windows.LazyProc87setLastError *windows.LazyProc88sizeofResource *windows.LazyProc89systemTimeToFileTime *windows.LazyProc90)
91
92type (93ATOM uint1694HANDLE uintptr95HGLOBAL HANDLE
96HINSTANCE HANDLE
97LCID uint3298LCTYPE uint3299LANGID uint16100HMODULE uintptr101HWINEVENTHOOK HANDLE
102HRSRC uintptr103)
104
105type FILETIME struct {106DwLowDateTime uint32107DwHighDateTime uint32108}
109
110type NUMBERFMT struct {111NumDigits uint32112LeadingZero uint32113Grouping uint32114LpDecimalSep *uint16115LpThousandSep *uint16116NegativeOrder uint32117}
118
119type SYSTEMTIME struct {120WYear uint16121WMonth uint16122WDayOfWeek uint16123WDay uint16124WHour uint16125WMinute uint16126WSecond uint16127WMilliseconds uint16128}
129
130type ACTCTX struct {131size uint32132Flags uint32133Source *uint16 // UTF-16 string134ProcessorArchitecture uint16135LangID uint16136AssemblyDirectory *uint16 // UTF-16 string137ResourceName *uint16 // UTF-16 string138ApplicationName *uint16 // UTF-16 string139Module HMODULE
140}
141
142func init() {143// Library144libkernel32 = windows.NewLazySystemDLL("kernel32.dll")145
146// Functions147activateActCtx = libkernel32.NewProc("ActivateActCtx")148closeHandle = libkernel32.NewProc("CloseHandle")149createActCtx = libkernel32.NewProc("CreateActCtxW")150fileTimeToSystemTime = libkernel32.NewProc("FileTimeToSystemTime")151findResource = libkernel32.NewProc("FindResourceW")152getConsoleTitle = libkernel32.NewProc("GetConsoleTitleW")153getConsoleWindow = libkernel32.NewProc("GetConsoleWindow")154getCurrentThreadId = libkernel32.NewProc("GetCurrentThreadId")155getLastError = libkernel32.NewProc("GetLastError")156getLocaleInfo = libkernel32.NewProc("GetLocaleInfoW")157getLogicalDriveStrings = libkernel32.NewProc("GetLogicalDriveStringsW")158getModuleHandle = libkernel32.NewProc("GetModuleHandleW")159getNumberFormat = libkernel32.NewProc("GetNumberFormatW")160getPhysicallyInstalledSystemMemory = libkernel32.NewProc("GetPhysicallyInstalledSystemMemory")161getProfileString = libkernel32.NewProc("GetProfileStringW")162getThreadLocale = libkernel32.NewProc("GetThreadLocale")163getThreadUILanguage = libkernel32.NewProc("GetThreadUILanguage")164getVersion = libkernel32.NewProc("GetVersion")165globalAlloc = libkernel32.NewProc("GlobalAlloc")166globalFree = libkernel32.NewProc("GlobalFree")167globalLock = libkernel32.NewProc("GlobalLock")168globalUnlock = libkernel32.NewProc("GlobalUnlock")169moveMemory = libkernel32.NewProc("RtlMoveMemory")170mulDiv = libkernel32.NewProc("MulDiv")171loadResource = libkernel32.NewProc("LoadResource")172lockResource = libkernel32.NewProc("LockResource")173setLastError = libkernel32.NewProc("SetLastError")174sizeofResource = libkernel32.NewProc("SizeofResource")175systemTimeToFileTime = libkernel32.NewProc("SystemTimeToFileTime")176}
177
178func ActivateActCtx(ctx HANDLE) (uintptr, bool) {179var cookie uintptr180ret, _, _ := syscall.Syscall(activateActCtx.Addr(), 2,181uintptr(ctx),182uintptr(unsafe.Pointer(&cookie)),1830)184return cookie, ret != 0185}
186
187func CloseHandle(hObject HANDLE) bool {188ret, _, _ := syscall.Syscall(closeHandle.Addr(), 1,189uintptr(hObject),1900,1910)192
193return ret != 0194}
195
196func CreateActCtx(ctx *ACTCTX) HANDLE {197if ctx != nil {198ctx.size = uint32(unsafe.Sizeof(*ctx))199}200ret, _, _ := syscall.Syscall(201createActCtx.Addr(),2021,203uintptr(unsafe.Pointer(ctx)),2040,2050)206return HANDLE(ret)207}
208
209func FileTimeToSystemTime(lpFileTime *FILETIME, lpSystemTime *SYSTEMTIME) bool {210ret, _, _ := syscall.Syscall(fileTimeToSystemTime.Addr(), 2,211uintptr(unsafe.Pointer(lpFileTime)),212uintptr(unsafe.Pointer(lpSystemTime)),2130)214
215return ret != 0216}
217
218func FindResource(hModule HMODULE, lpName, lpType *uint16) HRSRC {219ret, _, _ := syscall.Syscall(findResource.Addr(), 3,220uintptr(hModule),221uintptr(unsafe.Pointer(lpName)),222uintptr(unsafe.Pointer(lpType)))223
224return HRSRC(ret)225}
226
227func GetConsoleTitle(lpConsoleTitle *uint16, nSize uint32) uint32 {228ret, _, _ := syscall.Syscall(getConsoleTitle.Addr(), 2,229uintptr(unsafe.Pointer(lpConsoleTitle)),230uintptr(nSize),2310)232
233return uint32(ret)234}
235
236func GetConsoleWindow() HWND {237ret, _, _ := syscall.Syscall(getConsoleWindow.Addr(), 0,2380,2390,2400)241
242return HWND(ret)243}
244
245func GetCurrentThreadId() uint32 {246ret, _, _ := syscall.Syscall(getCurrentThreadId.Addr(), 0,2470,2480,2490)250
251return uint32(ret)252}
253
254func GetLastError() uint32 {255ret, _, _ := syscall.Syscall(getLastError.Addr(), 0,2560,2570,2580)259
260return uint32(ret)261}
262
263func GetLocaleInfo(Locale LCID, LCType LCTYPE, lpLCData *uint16, cchData int32) int32 {264ret, _, _ := syscall.Syscall6(getLocaleInfo.Addr(), 4,265uintptr(Locale),266uintptr(LCType),267uintptr(unsafe.Pointer(lpLCData)),268uintptr(cchData),2690,2700)271
272return int32(ret)273}
274
275func GetLogicalDriveStrings(nBufferLength uint32, lpBuffer *uint16) uint32 {276ret, _, _ := syscall.Syscall(getLogicalDriveStrings.Addr(), 2,277uintptr(nBufferLength),278uintptr(unsafe.Pointer(lpBuffer)),2790)280
281return uint32(ret)282}
283
284func GetModuleHandle(lpModuleName *uint16) HINSTANCE {285ret, _, _ := syscall.Syscall(getModuleHandle.Addr(), 1,286uintptr(unsafe.Pointer(lpModuleName)),2870,2880)289
290return HINSTANCE(ret)291}
292
293func GetNumberFormat(Locale LCID, dwFlags uint32, lpValue *uint16, lpFormat *NUMBERFMT, lpNumberStr *uint16, cchNumber int32) int32 {294ret, _, _ := syscall.Syscall6(getNumberFormat.Addr(), 6,295uintptr(Locale),296uintptr(dwFlags),297uintptr(unsafe.Pointer(lpValue)),298uintptr(unsafe.Pointer(lpFormat)),299uintptr(unsafe.Pointer(lpNumberStr)),300uintptr(cchNumber))301
302return int32(ret)303}
304
305func GetPhysicallyInstalledSystemMemory(totalMemoryInKilobytes *uint64) bool {306if getPhysicallyInstalledSystemMemory.Find() != nil {307return false308}309ret, _, _ := syscall.Syscall(getPhysicallyInstalledSystemMemory.Addr(), 1,310uintptr(unsafe.Pointer(totalMemoryInKilobytes)),3110,3120)313
314return ret != 0315}
316
317func GetProfileString(lpAppName, lpKeyName, lpDefault *uint16, lpReturnedString uintptr, nSize uint32) bool {318ret, _, _ := syscall.Syscall6(getProfileString.Addr(), 5,319uintptr(unsafe.Pointer(lpAppName)),320uintptr(unsafe.Pointer(lpKeyName)),321uintptr(unsafe.Pointer(lpDefault)),322lpReturnedString,323uintptr(nSize),3240)325return ret != 0326}
327
328func GetThreadLocale() LCID {329ret, _, _ := syscall.Syscall(getThreadLocale.Addr(), 0,3300,3310,3320)333
334return LCID(ret)335}
336
337func GetThreadUILanguage() LANGID {338if getThreadUILanguage.Find() != nil {339return 0340}341
342ret, _, _ := syscall.Syscall(getThreadUILanguage.Addr(), 0,3430,3440,3450)346
347return LANGID(ret)348}
349
350func GetVersion() uint32 {351ret, _, _ := syscall.Syscall(getVersion.Addr(), 0,3520,3530,3540)355return uint32(ret)356}
357
358func GlobalAlloc(uFlags uint32, dwBytes uintptr) HGLOBAL {359ret, _, _ := syscall.Syscall(globalAlloc.Addr(), 2,360uintptr(uFlags),361dwBytes,3620)363
364return HGLOBAL(ret)365}
366
367func GlobalFree(hMem HGLOBAL) HGLOBAL {368ret, _, _ := syscall.Syscall(globalFree.Addr(), 1,369uintptr(hMem),3700,3710)372
373return HGLOBAL(ret)374}
375
376func GlobalLock(hMem HGLOBAL) unsafe.Pointer {377ret, _, _ := syscall.Syscall(globalLock.Addr(), 1,378uintptr(hMem),3790,3800)381
382return unsafe.Pointer(ret)383}
384
385func GlobalUnlock(hMem HGLOBAL) bool {386ret, _, _ := syscall.Syscall(globalUnlock.Addr(), 1,387uintptr(hMem),3880,3890)390
391return ret != 0392}
393
394func MoveMemory(destination, source unsafe.Pointer, length uintptr) {395syscall.Syscall(moveMemory.Addr(), 3,396uintptr(unsafe.Pointer(destination)),397uintptr(source),398uintptr(length))399}
400
401func MulDiv(nNumber, nNumerator, nDenominator int32) int32 {402ret, _, _ := syscall.Syscall(mulDiv.Addr(), 3,403uintptr(nNumber),404uintptr(nNumerator),405uintptr(nDenominator))406
407return int32(ret)408}
409
410func LoadResource(hModule HMODULE, hResInfo HRSRC) HGLOBAL {411ret, _, _ := syscall.Syscall(loadResource.Addr(), 2,412uintptr(hModule),413uintptr(hResInfo),4140)415
416return HGLOBAL(ret)417}
418
419func LockResource(hResData HGLOBAL) uintptr {420ret, _, _ := syscall.Syscall(lockResource.Addr(), 1,421uintptr(hResData),4220,4230)424
425return ret426}
427
428func SetLastError(dwErrorCode uint32) {429syscall.Syscall(setLastError.Addr(), 1,430uintptr(dwErrorCode),4310,4320)433}
434
435func SizeofResource(hModule HMODULE, hResInfo HRSRC) uint32 {436ret, _, _ := syscall.Syscall(sizeofResource.Addr(), 2,437uintptr(hModule),438uintptr(hResInfo),4390)440
441return uint32(ret)442}
443
444func SystemTimeToFileTime(lpSystemTime *SYSTEMTIME, lpFileTime *FILETIME) bool {445ret, _, _ := syscall.Syscall(systemTimeToFileTime.Addr(), 2,446uintptr(unsafe.Pointer(lpSystemTime)),447uintptr(unsafe.Pointer(lpFileTime)),4480)449
450return ret != 0451}
452