go-tg-screenshot-bot

Форк
0
531 строка · 13.3 Кб
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
	"syscall"
11
	"unsafe"
12

13
	"golang.org/x/sys/windows"
14
)
15

16
const (
17
	CLSCTX_INPROC_SERVER          = 0x1
18
	CLSCTX_INPROC_HANDLER         = 0x2
19
	CLSCTX_LOCAL_SERVER           = 0x4
20
	CLSCTX_INPROC_SERVER16        = 0x8
21
	CLSCTX_REMOTE_SERVER          = 0x10
22
	CLSCTX_INPROC_HANDLER16       = 0x20
23
	CLSCTX_RESERVED1              = 0x40
24
	CLSCTX_RESERVED2              = 0x80
25
	CLSCTX_RESERVED3              = 0x100
26
	CLSCTX_RESERVED4              = 0x200
27
	CLSCTX_NO_CODE_DOWNLOAD       = 0x400
28
	CLSCTX_RESERVED5              = 0x800
29
	CLSCTX_NO_CUSTOM_MARSHAL      = 0x1000
30
	CLSCTX_ENABLE_CODE_DOWNLOAD   = 0x2000
31
	CLSCTX_NO_FAILURE_LOG         = 0x4000
32
	CLSCTX_DISABLE_AAA            = 0x8000
33
	CLSCTX_ENABLE_AAA             = 0x10000
34
	CLSCTX_FROM_DEFAULT_CONTEXT   = 0x20000
35
	CLSCTX_ACTIVATE_32_BIT_SERVER = 0x40000
36
	CLSCTX_ACTIVATE_64_BIT_SERVER = 0x80000
37
	CLSCTX_ENABLE_CLOAKING        = 0x100000
38
	CLSCTX_PS_DLL                 = 0x80000000
39
	CLSCTX_INPROC                 = CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER
40
	CLSCTX_ALL                    = CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER | CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER
41
	CLSCTX_SERVER                 = CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER
42
)
43

44
const (
45
	COINIT_APARTMENTTHREADED = 0x2 // Apartment model
46
	COINIT_MULTITHREADED     = 0x0 // OLE calls objects on any thread.
47
	COINIT_DISABLE_OLE1DDE   = 0x4 // Don't use DDE for Ole1 support.
48
	COINIT_SPEED_OVER_MEMORY = 0x8 // Trade memory for speed.
49
)
50

51
// Verbs for IOleObject.DoVerb
52
const (
53
	OLEIVERB_PRIMARY          = 0
54
	OLEIVERB_SHOW             = -1
55
	OLEIVERB_OPEN             = -2
56
	OLEIVERB_HIDE             = -3
57
	OLEIVERB_UIACTIVATE       = -4
58
	OLEIVERB_INPLACEACTIVATE  = -5
59
	OLEIVERB_DISCARDUNDOSTATE = -6
60
)
61

62
// OLECLOSE constants
63
const (
64
	OLECLOSE_SAVEIFDIRTY = 0
65
	OLECLOSE_NOSAVE      = 1
66
	OLECLOSE_PROMPTSAVE  = 2
67
)
68

69
type IID syscall.GUID
70
type CLSID syscall.GUID
71
type REFIID *IID
72
type REFCLSID *CLSID
73

74
var (
75
	IID_IClassFactory             = IID{0x00000001, 0x0000, 0x0000, [8]byte{0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}}
76
	IID_IConnectionPointContainer = IID{0xB196B284, 0xBAB4, 0x101A, [8]byte{0xB6, 0x9C, 0x00, 0xAA, 0x00, 0x34, 0x1D, 0x07}}
77
	IID_IOleClientSite            = IID{0x00000118, 0x0000, 0x0000, [8]byte{0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}}
78
	IID_IOleInPlaceObject         = IID{0x00000113, 0x0000, 0x0000, [8]byte{0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}}
79
	IID_IOleInPlaceSite           = IID{0x00000119, 0x0000, 0x0000, [8]byte{0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}}
80
	IID_IOleObject                = IID{0x00000112, 0x0000, 0x0000, [8]byte{0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}}
81
	IID_IUnknown                  = IID{0x00000000, 0x0000, 0x0000, [8]byte{0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}}
82
)
83

84
func EqualREFIID(a, b REFIID) bool {
85
	if a == b {
86
		return true
87
	}
88
	if a == nil || b == nil {
89
		return false
90
	}
91

92
	if a.Data1 != b.Data1 || a.Data2 != b.Data2 || a.Data3 != b.Data3 {
93
		return false
94
	}
95

96
	for i := 0; i < 8; i++ {
97
		if a.Data4[i] != b.Data4[i] {
98
			return false
99
		}
100
	}
101

102
	return true
103
}
104

105
type IClassFactoryVtbl struct {
106
	QueryInterface uintptr
107
	AddRef         uintptr
108
	Release        uintptr
109
	CreateInstance uintptr
110
	LockServer     uintptr
111
}
112

113
type IClassFactory struct {
114
	LpVtbl *IClassFactoryVtbl
115
}
116

117
func (cf *IClassFactory) Release() uint32 {
118
	ret, _, _ := syscall.Syscall(cf.LpVtbl.Release, 1,
119
		uintptr(unsafe.Pointer(cf)),
120
		0,
121
		0)
122

123
	return uint32(ret)
124
}
125

126
func (cf *IClassFactory) CreateInstance(pUnkOuter *IUnknown, riid REFIID, ppvObject *unsafe.Pointer) HRESULT {
127
	ret, _, _ := syscall.Syscall6(cf.LpVtbl.CreateInstance, 4,
128
		uintptr(unsafe.Pointer(cf)),
129
		uintptr(unsafe.Pointer(pUnkOuter)),
130
		uintptr(unsafe.Pointer(riid)),
131
		uintptr(unsafe.Pointer(ppvObject)),
132
		0,
133
		0)
134

135
	return HRESULT(ret)
136
}
137

138
type IConnectionPointVtbl struct {
139
	QueryInterface              uintptr
140
	AddRef                      uintptr
141
	Release                     uintptr
142
	GetConnectionInterface      uintptr
143
	GetConnectionPointContainer uintptr
144
	Advise                      uintptr
145
	Unadvise                    uintptr
146
	EnumConnections             uintptr
147
}
148

149
type IConnectionPoint struct {
150
	LpVtbl *IConnectionPointVtbl
151
}
152

153
func (cp *IConnectionPoint) Release() uint32 {
154
	ret, _, _ := syscall.Syscall(cp.LpVtbl.Release, 1,
155
		uintptr(unsafe.Pointer(cp)),
156
		0,
157
		0)
158

159
	return uint32(ret)
160
}
161

162
func (cp *IConnectionPoint) Advise(pUnkSink unsafe.Pointer, pdwCookie *uint32) HRESULT {
163
	ret, _, _ := syscall.Syscall(cp.LpVtbl.Advise, 3,
164
		uintptr(unsafe.Pointer(cp)),
165
		uintptr(pUnkSink),
166
		uintptr(unsafe.Pointer(pdwCookie)))
167

168
	return HRESULT(ret)
169
}
170

171
type IConnectionPointContainerVtbl struct {
172
	QueryInterface       uintptr
173
	AddRef               uintptr
174
	Release              uintptr
175
	EnumConnectionPoints uintptr
176
	FindConnectionPoint  uintptr
177
}
178

179
type IConnectionPointContainer struct {
180
	LpVtbl *IConnectionPointContainerVtbl
181
}
182

183
func (cpc *IConnectionPointContainer) Release() uint32 {
184
	ret, _, _ := syscall.Syscall(cpc.LpVtbl.Release, 1,
185
		uintptr(unsafe.Pointer(cpc)),
186
		0,
187
		0)
188

189
	return uint32(ret)
190
}
191

192
func (cpc *IConnectionPointContainer) FindConnectionPoint(riid REFIID, ppCP **IConnectionPoint) HRESULT {
193
	ret, _, _ := syscall.Syscall(cpc.LpVtbl.FindConnectionPoint, 3,
194
		uintptr(unsafe.Pointer(cpc)),
195
		uintptr(unsafe.Pointer(riid)),
196
		uintptr(unsafe.Pointer(ppCP)))
197

198
	return HRESULT(ret)
199
}
200

201
type IOleClientSiteVtbl struct {
202
	QueryInterface         uintptr
203
	AddRef                 uintptr
204
	Release                uintptr
205
	SaveObject             uintptr
206
	GetMoniker             uintptr
207
	GetContainer           uintptr
208
	ShowObject             uintptr
209
	OnShowWindow           uintptr
210
	RequestNewObjectLayout uintptr
211
}
212

213
type IOleClientSite struct {
214
	LpVtbl *IOleClientSiteVtbl
215
}
216

217
type IOleInPlaceFrameVtbl struct {
218
	QueryInterface       uintptr
219
	AddRef               uintptr
220
	Release              uintptr
221
	GetWindow            uintptr
222
	ContextSensitiveHelp uintptr
223
	GetBorder            uintptr
224
	RequestBorderSpace   uintptr
225
	SetBorderSpace       uintptr
226
	SetActiveObject      uintptr
227
	InsertMenus          uintptr
228
	SetMenu              uintptr
229
	RemoveMenus          uintptr
230
	SetStatusText        uintptr
231
	EnableModeless       uintptr
232
	TranslateAccelerator uintptr
233
}
234

235
type IOleInPlaceFrame struct {
236
	LpVtbl *IOleInPlaceFrameVtbl
237
}
238

239
type IOleInPlaceObjectVtbl struct {
240
	QueryInterface       uintptr
241
	AddRef               uintptr
242
	Release              uintptr
243
	GetWindow            uintptr
244
	ContextSensitiveHelp uintptr
245
	InPlaceDeactivate    uintptr
246
	UIDeactivate         uintptr
247
	SetObjectRects       uintptr
248
	ReactivateAndUndo    uintptr
249
}
250

251
type IOleInPlaceObject struct {
252
	LpVtbl *IOleInPlaceObjectVtbl
253
}
254

255
func (obj *IOleInPlaceObject) Release() uint32 {
256
	ret, _, _ := syscall.Syscall(obj.LpVtbl.Release, 1,
257
		uintptr(unsafe.Pointer(obj)),
258
		0,
259
		0)
260

261
	return uint32(ret)
262
}
263

264
func (obj *IOleInPlaceObject) SetObjectRects(lprcPosRect, lprcClipRect *RECT) HRESULT {
265
	ret, _, _ := syscall.Syscall(obj.LpVtbl.SetObjectRects, 3,
266
		uintptr(unsafe.Pointer(obj)),
267
		uintptr(unsafe.Pointer(lprcPosRect)),
268
		uintptr(unsafe.Pointer(lprcClipRect)))
269

270
	return HRESULT(ret)
271
}
272

273
type IOleInPlaceSiteVtbl struct {
274
	QueryInterface       uintptr
275
	AddRef               uintptr
276
	Release              uintptr
277
	GetWindow            uintptr
278
	ContextSensitiveHelp uintptr
279
	CanInPlaceActivate   uintptr
280
	OnInPlaceActivate    uintptr
281
	OnUIActivate         uintptr
282
	GetWindowContext     uintptr
283
	Scroll               uintptr
284
	OnUIDeactivate       uintptr
285
	OnInPlaceDeactivate  uintptr
286
	DiscardUndoState     uintptr
287
	DeactivateAndUndo    uintptr
288
	OnPosRectChange      uintptr
289
}
290

291
type IOleInPlaceSite struct {
292
	LpVtbl *IOleInPlaceSiteVtbl
293
}
294

295
type IOleObjectVtbl struct {
296
	QueryInterface   uintptr
297
	AddRef           uintptr
298
	Release          uintptr
299
	SetClientSite    uintptr
300
	GetClientSite    uintptr
301
	SetHostNames     uintptr
302
	Close            uintptr
303
	SetMoniker       uintptr
304
	GetMoniker       uintptr
305
	InitFromData     uintptr
306
	GetClipboardData uintptr
307
	DoVerb           uintptr
308
	EnumVerbs        uintptr
309
	Update           uintptr
310
	IsUpToDate       uintptr
311
	GetUserClassID   uintptr
312
	GetUserType      uintptr
313
	SetExtent        uintptr
314
	GetExtent        uintptr
315
	Advise           uintptr
316
	Unadvise         uintptr
317
	EnumAdvise       uintptr
318
	GetMiscStatus    uintptr
319
	SetColorScheme   uintptr
320
}
321

322
type IOleObject struct {
323
	LpVtbl *IOleObjectVtbl
324
}
325

326
func (obj *IOleObject) QueryInterface(riid REFIID, ppvObject *unsafe.Pointer) HRESULT {
327
	ret, _, _ := syscall.Syscall(obj.LpVtbl.QueryInterface, 3,
328
		uintptr(unsafe.Pointer(obj)),
329
		uintptr(unsafe.Pointer(riid)),
330
		uintptr(unsafe.Pointer(ppvObject)))
331

332
	return HRESULT(ret)
333
}
334

335
func (obj *IOleObject) Release() uint32 {
336
	ret, _, _ := syscall.Syscall(obj.LpVtbl.Release, 1,
337
		uintptr(unsafe.Pointer(obj)),
338
		0,
339
		0)
340

341
	return uint32(ret)
342
}
343

344
func (obj *IOleObject) SetClientSite(pClientSite *IOleClientSite) HRESULT {
345
	ret, _, _ := syscall.Syscall(obj.LpVtbl.SetClientSite, 2,
346
		uintptr(unsafe.Pointer(obj)),
347
		uintptr(unsafe.Pointer(pClientSite)),
348
		0)
349

350
	return HRESULT(ret)
351
}
352

353
func (obj *IOleObject) SetHostNames(szContainerApp, szContainerObj *uint16) HRESULT {
354
	ret, _, _ := syscall.Syscall(obj.LpVtbl.SetHostNames, 3,
355
		uintptr(unsafe.Pointer(obj)),
356
		uintptr(unsafe.Pointer(szContainerApp)),
357
		uintptr(unsafe.Pointer(szContainerObj)))
358

359
	return HRESULT(ret)
360
}
361

362
func (obj *IOleObject) Close(dwSaveOption uint32) HRESULT {
363
	ret, _, _ := syscall.Syscall(obj.LpVtbl.Close, 2,
364
		uintptr(unsafe.Pointer(obj)),
365
		uintptr(dwSaveOption),
366
		0)
367

368
	return HRESULT(ret)
369
}
370

371
func (obj *IOleObject) DoVerb(iVerb int32, lpmsg *MSG, pActiveSite *IOleClientSite, lindex int32, hwndParent HWND, lprcPosRect *RECT) HRESULT {
372
	ret, _, _ := syscall.Syscall9(obj.LpVtbl.DoVerb, 7,
373
		uintptr(unsafe.Pointer(obj)),
374
		uintptr(iVerb),
375
		uintptr(unsafe.Pointer(lpmsg)),
376
		uintptr(unsafe.Pointer(pActiveSite)),
377
		uintptr(lindex),
378
		uintptr(hwndParent),
379
		uintptr(unsafe.Pointer(lprcPosRect)),
380
		0,
381
		0)
382

383
	return HRESULT(ret)
384
}
385

386
type IUnknownVtbl struct {
387
	QueryInterface uintptr
388
	AddRef         uintptr
389
	Release        uintptr
390
}
391

392
type IUnknown struct {
393
	LpVtbl *IUnknownVtbl
394
}
395

396
type OLEINPLACEFRAMEINFO struct {
397
	Cb            uint32
398
	FMDIApp       BOOL
399
	HwndFrame     HWND
400
	Haccel        HACCEL
401
	CAccelEntries uint32
402
}
403

404
type COAUTHIDENTITY struct {
405
	User           *uint16
406
	UserLength     uint32
407
	Domain         *uint16
408
	DomainLength   uint32
409
	Password       *uint16
410
	PasswordLength uint32
411
	Flags          uint32
412
}
413

414
type COAUTHINFO struct {
415
	dwAuthnSvc           uint32
416
	dwAuthzSvc           uint32
417
	pwszServerPrincName  *uint16
418
	dwAuthnLevel         uint32
419
	dwImpersonationLevel uint32
420
	pAuthIdentityData    *COAUTHIDENTITY
421
	dwCapabilities       uint32
422
}
423

424
type COSERVERINFO struct {
425
	dwReserved1 uint32
426
	pwszName    *uint16
427
	pAuthInfo   *COAUTHINFO
428
	dwReserved2 uint32
429
}
430

431
var (
432
	// Library
433
	libole32 *windows.LazyDLL
434

435
	// Functions
436
	coCreateInstance      *windows.LazyProc
437
	coGetClassObject      *windows.LazyProc
438
	coInitializeEx        *windows.LazyProc
439
	coTaskMemFree         *windows.LazyProc
440
	coUninitialize        *windows.LazyProc
441
	oleInitialize         *windows.LazyProc
442
	oleSetContainedObject *windows.LazyProc
443
	oleUninitialize       *windows.LazyProc
444
)
445

446
func init() {
447
	// Library
448
	libole32 = windows.NewLazySystemDLL("ole32.dll")
449

450
	// Functions
451
	coCreateInstance = libole32.NewProc("CoCreateInstance")
452
	coGetClassObject = libole32.NewProc("CoGetClassObject")
453
	coInitializeEx = libole32.NewProc("CoInitializeEx")
454
	coTaskMemFree = libole32.NewProc("CoTaskMemFree")
455
	coUninitialize = libole32.NewProc("CoUninitialize")
456
	oleInitialize = libole32.NewProc("OleInitialize")
457
	oleSetContainedObject = libole32.NewProc("OleSetContainedObject")
458
	oleUninitialize = libole32.NewProc("OleUninitialize")
459
}
460

461
func CoCreateInstance(rclsid REFCLSID, pUnkOuter *IUnknown, dwClsContext uint32, riid REFIID, ppv *unsafe.Pointer) HRESULT {
462
	ret, _, _ := syscall.Syscall6(coCreateInstance.Addr(), 5,
463
		uintptr(unsafe.Pointer(rclsid)),
464
		uintptr(unsafe.Pointer(pUnkOuter)),
465
		uintptr(dwClsContext),
466
		uintptr(unsafe.Pointer(riid)),
467
		uintptr(unsafe.Pointer(ppv)),
468
		0)
469

470
	return HRESULT(ret)
471
}
472

473
func CoGetClassObject(rclsid REFCLSID, dwClsContext uint32, pServerInfo *COSERVERINFO, riid REFIID, ppv *unsafe.Pointer) HRESULT {
474
	ret, _, _ := syscall.Syscall6(coGetClassObject.Addr(), 5,
475
		uintptr(unsafe.Pointer(rclsid)),
476
		uintptr(dwClsContext),
477
		uintptr(unsafe.Pointer(pServerInfo)),
478
		uintptr(unsafe.Pointer(riid)),
479
		uintptr(unsafe.Pointer(ppv)),
480
		0)
481

482
	return HRESULT(ret)
483
}
484

485
func CoInitializeEx(reserved unsafe.Pointer, coInit uint32) HRESULT {
486
	ret, _, _ := syscall.Syscall(coInitializeEx.Addr(), 2,
487
		uintptr(reserved),
488
		uintptr(coInit),
489
		0)
490

491
	return HRESULT(ret)
492
}
493

494
func CoUninitialize() {
495
	syscall.Syscall(coUninitialize.Addr(), 0,
496
		0,
497
		0,
498
		0)
499
}
500

501
func CoTaskMemFree(pv uintptr) {
502
	syscall.Syscall(coTaskMemFree.Addr(), 1,
503
		pv,
504
		0,
505
		0)
506
}
507

508
func OleInitialize() HRESULT {
509
	ret, _, _ := syscall.Syscall(oleInitialize.Addr(), 1, // WTF, why does 0 not work here?
510
		0,
511
		0,
512
		0)
513

514
	return HRESULT(ret)
515
}
516

517
func OleSetContainedObject(pUnknown *IUnknown, fContained bool) HRESULT {
518
	ret, _, _ := syscall.Syscall(oleSetContainedObject.Addr(), 2,
519
		uintptr(unsafe.Pointer(pUnknown)),
520
		uintptr(BoolToBOOL(fContained)),
521
		0)
522

523
	return HRESULT(ret)
524
}
525

526
func OleUninitialize() {
527
	syscall.Syscall(oleUninitialize.Addr(), 0,
528
		0,
529
		0,
530
		0)
531
}
532

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

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

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

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