go-tg-screenshot-bot
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
7package win
8
9import (
10"syscall"
11"unsafe"
12
13"golang.org/x/sys/windows"
14)
15
16const (
17CLSCTX_INPROC_SERVER = 0x1
18CLSCTX_INPROC_HANDLER = 0x2
19CLSCTX_LOCAL_SERVER = 0x4
20CLSCTX_INPROC_SERVER16 = 0x8
21CLSCTX_REMOTE_SERVER = 0x10
22CLSCTX_INPROC_HANDLER16 = 0x20
23CLSCTX_RESERVED1 = 0x40
24CLSCTX_RESERVED2 = 0x80
25CLSCTX_RESERVED3 = 0x100
26CLSCTX_RESERVED4 = 0x200
27CLSCTX_NO_CODE_DOWNLOAD = 0x400
28CLSCTX_RESERVED5 = 0x800
29CLSCTX_NO_CUSTOM_MARSHAL = 0x1000
30CLSCTX_ENABLE_CODE_DOWNLOAD = 0x2000
31CLSCTX_NO_FAILURE_LOG = 0x4000
32CLSCTX_DISABLE_AAA = 0x8000
33CLSCTX_ENABLE_AAA = 0x10000
34CLSCTX_FROM_DEFAULT_CONTEXT = 0x20000
35CLSCTX_ACTIVATE_32_BIT_SERVER = 0x40000
36CLSCTX_ACTIVATE_64_BIT_SERVER = 0x80000
37CLSCTX_ENABLE_CLOAKING = 0x100000
38CLSCTX_PS_DLL = 0x80000000
39CLSCTX_INPROC = CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER
40CLSCTX_ALL = CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER | CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER
41CLSCTX_SERVER = CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER | CLSCTX_REMOTE_SERVER
42)
43
44const (
45COINIT_APARTMENTTHREADED = 0x2 // Apartment model
46COINIT_MULTITHREADED = 0x0 // OLE calls objects on any thread.
47COINIT_DISABLE_OLE1DDE = 0x4 // Don't use DDE for Ole1 support.
48COINIT_SPEED_OVER_MEMORY = 0x8 // Trade memory for speed.
49)
50
51// Verbs for IOleObject.DoVerb
52const (
53OLEIVERB_PRIMARY = 0
54OLEIVERB_SHOW = -1
55OLEIVERB_OPEN = -2
56OLEIVERB_HIDE = -3
57OLEIVERB_UIACTIVATE = -4
58OLEIVERB_INPLACEACTIVATE = -5
59OLEIVERB_DISCARDUNDOSTATE = -6
60)
61
62// OLECLOSE constants
63const (
64OLECLOSE_SAVEIFDIRTY = 0
65OLECLOSE_NOSAVE = 1
66OLECLOSE_PROMPTSAVE = 2
67)
68
69type IID syscall.GUID
70type CLSID syscall.GUID
71type REFIID *IID
72type REFCLSID *CLSID
73
74var (
75IID_IClassFactory = IID{0x00000001, 0x0000, 0x0000, [8]byte{0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}}
76IID_IConnectionPointContainer = IID{0xB196B284, 0xBAB4, 0x101A, [8]byte{0xB6, 0x9C, 0x00, 0xAA, 0x00, 0x34, 0x1D, 0x07}}
77IID_IOleClientSite = IID{0x00000118, 0x0000, 0x0000, [8]byte{0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}}
78IID_IOleInPlaceObject = IID{0x00000113, 0x0000, 0x0000, [8]byte{0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}}
79IID_IOleInPlaceSite = IID{0x00000119, 0x0000, 0x0000, [8]byte{0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}}
80IID_IOleObject = IID{0x00000112, 0x0000, 0x0000, [8]byte{0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}}
81IID_IUnknown = IID{0x00000000, 0x0000, 0x0000, [8]byte{0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46}}
82)
83
84func EqualREFIID(a, b REFIID) bool {
85if a == b {
86return true
87}
88if a == nil || b == nil {
89return false
90}
91
92if a.Data1 != b.Data1 || a.Data2 != b.Data2 || a.Data3 != b.Data3 {
93return false
94}
95
96for i := 0; i < 8; i++ {
97if a.Data4[i] != b.Data4[i] {
98return false
99}
100}
101
102return true
103}
104
105type IClassFactoryVtbl struct {
106QueryInterface uintptr
107AddRef uintptr
108Release uintptr
109CreateInstance uintptr
110LockServer uintptr
111}
112
113type IClassFactory struct {
114LpVtbl *IClassFactoryVtbl
115}
116
117func (cf *IClassFactory) Release() uint32 {
118ret, _, _ := syscall.Syscall(cf.LpVtbl.Release, 1,
119uintptr(unsafe.Pointer(cf)),
1200,
1210)
122
123return uint32(ret)
124}
125
126func (cf *IClassFactory) CreateInstance(pUnkOuter *IUnknown, riid REFIID, ppvObject *unsafe.Pointer) HRESULT {
127ret, _, _ := syscall.Syscall6(cf.LpVtbl.CreateInstance, 4,
128uintptr(unsafe.Pointer(cf)),
129uintptr(unsafe.Pointer(pUnkOuter)),
130uintptr(unsafe.Pointer(riid)),
131uintptr(unsafe.Pointer(ppvObject)),
1320,
1330)
134
135return HRESULT(ret)
136}
137
138type IConnectionPointVtbl struct {
139QueryInterface uintptr
140AddRef uintptr
141Release uintptr
142GetConnectionInterface uintptr
143GetConnectionPointContainer uintptr
144Advise uintptr
145Unadvise uintptr
146EnumConnections uintptr
147}
148
149type IConnectionPoint struct {
150LpVtbl *IConnectionPointVtbl
151}
152
153func (cp *IConnectionPoint) Release() uint32 {
154ret, _, _ := syscall.Syscall(cp.LpVtbl.Release, 1,
155uintptr(unsafe.Pointer(cp)),
1560,
1570)
158
159return uint32(ret)
160}
161
162func (cp *IConnectionPoint) Advise(pUnkSink unsafe.Pointer, pdwCookie *uint32) HRESULT {
163ret, _, _ := syscall.Syscall(cp.LpVtbl.Advise, 3,
164uintptr(unsafe.Pointer(cp)),
165uintptr(pUnkSink),
166uintptr(unsafe.Pointer(pdwCookie)))
167
168return HRESULT(ret)
169}
170
171type IConnectionPointContainerVtbl struct {
172QueryInterface uintptr
173AddRef uintptr
174Release uintptr
175EnumConnectionPoints uintptr
176FindConnectionPoint uintptr
177}
178
179type IConnectionPointContainer struct {
180LpVtbl *IConnectionPointContainerVtbl
181}
182
183func (cpc *IConnectionPointContainer) Release() uint32 {
184ret, _, _ := syscall.Syscall(cpc.LpVtbl.Release, 1,
185uintptr(unsafe.Pointer(cpc)),
1860,
1870)
188
189return uint32(ret)
190}
191
192func (cpc *IConnectionPointContainer) FindConnectionPoint(riid REFIID, ppCP **IConnectionPoint) HRESULT {
193ret, _, _ := syscall.Syscall(cpc.LpVtbl.FindConnectionPoint, 3,
194uintptr(unsafe.Pointer(cpc)),
195uintptr(unsafe.Pointer(riid)),
196uintptr(unsafe.Pointer(ppCP)))
197
198return HRESULT(ret)
199}
200
201type IOleClientSiteVtbl struct {
202QueryInterface uintptr
203AddRef uintptr
204Release uintptr
205SaveObject uintptr
206GetMoniker uintptr
207GetContainer uintptr
208ShowObject uintptr
209OnShowWindow uintptr
210RequestNewObjectLayout uintptr
211}
212
213type IOleClientSite struct {
214LpVtbl *IOleClientSiteVtbl
215}
216
217type IOleInPlaceFrameVtbl struct {
218QueryInterface uintptr
219AddRef uintptr
220Release uintptr
221GetWindow uintptr
222ContextSensitiveHelp uintptr
223GetBorder uintptr
224RequestBorderSpace uintptr
225SetBorderSpace uintptr
226SetActiveObject uintptr
227InsertMenus uintptr
228SetMenu uintptr
229RemoveMenus uintptr
230SetStatusText uintptr
231EnableModeless uintptr
232TranslateAccelerator uintptr
233}
234
235type IOleInPlaceFrame struct {
236LpVtbl *IOleInPlaceFrameVtbl
237}
238
239type IOleInPlaceObjectVtbl struct {
240QueryInterface uintptr
241AddRef uintptr
242Release uintptr
243GetWindow uintptr
244ContextSensitiveHelp uintptr
245InPlaceDeactivate uintptr
246UIDeactivate uintptr
247SetObjectRects uintptr
248ReactivateAndUndo uintptr
249}
250
251type IOleInPlaceObject struct {
252LpVtbl *IOleInPlaceObjectVtbl
253}
254
255func (obj *IOleInPlaceObject) Release() uint32 {
256ret, _, _ := syscall.Syscall(obj.LpVtbl.Release, 1,
257uintptr(unsafe.Pointer(obj)),
2580,
2590)
260
261return uint32(ret)
262}
263
264func (obj *IOleInPlaceObject) SetObjectRects(lprcPosRect, lprcClipRect *RECT) HRESULT {
265ret, _, _ := syscall.Syscall(obj.LpVtbl.SetObjectRects, 3,
266uintptr(unsafe.Pointer(obj)),
267uintptr(unsafe.Pointer(lprcPosRect)),
268uintptr(unsafe.Pointer(lprcClipRect)))
269
270return HRESULT(ret)
271}
272
273type IOleInPlaceSiteVtbl struct {
274QueryInterface uintptr
275AddRef uintptr
276Release uintptr
277GetWindow uintptr
278ContextSensitiveHelp uintptr
279CanInPlaceActivate uintptr
280OnInPlaceActivate uintptr
281OnUIActivate uintptr
282GetWindowContext uintptr
283Scroll uintptr
284OnUIDeactivate uintptr
285OnInPlaceDeactivate uintptr
286DiscardUndoState uintptr
287DeactivateAndUndo uintptr
288OnPosRectChange uintptr
289}
290
291type IOleInPlaceSite struct {
292LpVtbl *IOleInPlaceSiteVtbl
293}
294
295type IOleObjectVtbl struct {
296QueryInterface uintptr
297AddRef uintptr
298Release uintptr
299SetClientSite uintptr
300GetClientSite uintptr
301SetHostNames uintptr
302Close uintptr
303SetMoniker uintptr
304GetMoniker uintptr
305InitFromData uintptr
306GetClipboardData uintptr
307DoVerb uintptr
308EnumVerbs uintptr
309Update uintptr
310IsUpToDate uintptr
311GetUserClassID uintptr
312GetUserType uintptr
313SetExtent uintptr
314GetExtent uintptr
315Advise uintptr
316Unadvise uintptr
317EnumAdvise uintptr
318GetMiscStatus uintptr
319SetColorScheme uintptr
320}
321
322type IOleObject struct {
323LpVtbl *IOleObjectVtbl
324}
325
326func (obj *IOleObject) QueryInterface(riid REFIID, ppvObject *unsafe.Pointer) HRESULT {
327ret, _, _ := syscall.Syscall(obj.LpVtbl.QueryInterface, 3,
328uintptr(unsafe.Pointer(obj)),
329uintptr(unsafe.Pointer(riid)),
330uintptr(unsafe.Pointer(ppvObject)))
331
332return HRESULT(ret)
333}
334
335func (obj *IOleObject) Release() uint32 {
336ret, _, _ := syscall.Syscall(obj.LpVtbl.Release, 1,
337uintptr(unsafe.Pointer(obj)),
3380,
3390)
340
341return uint32(ret)
342}
343
344func (obj *IOleObject) SetClientSite(pClientSite *IOleClientSite) HRESULT {
345ret, _, _ := syscall.Syscall(obj.LpVtbl.SetClientSite, 2,
346uintptr(unsafe.Pointer(obj)),
347uintptr(unsafe.Pointer(pClientSite)),
3480)
349
350return HRESULT(ret)
351}
352
353func (obj *IOleObject) SetHostNames(szContainerApp, szContainerObj *uint16) HRESULT {
354ret, _, _ := syscall.Syscall(obj.LpVtbl.SetHostNames, 3,
355uintptr(unsafe.Pointer(obj)),
356uintptr(unsafe.Pointer(szContainerApp)),
357uintptr(unsafe.Pointer(szContainerObj)))
358
359return HRESULT(ret)
360}
361
362func (obj *IOleObject) Close(dwSaveOption uint32) HRESULT {
363ret, _, _ := syscall.Syscall(obj.LpVtbl.Close, 2,
364uintptr(unsafe.Pointer(obj)),
365uintptr(dwSaveOption),
3660)
367
368return HRESULT(ret)
369}
370
371func (obj *IOleObject) DoVerb(iVerb int32, lpmsg *MSG, pActiveSite *IOleClientSite, lindex int32, hwndParent HWND, lprcPosRect *RECT) HRESULT {
372ret, _, _ := syscall.Syscall9(obj.LpVtbl.DoVerb, 7,
373uintptr(unsafe.Pointer(obj)),
374uintptr(iVerb),
375uintptr(unsafe.Pointer(lpmsg)),
376uintptr(unsafe.Pointer(pActiveSite)),
377uintptr(lindex),
378uintptr(hwndParent),
379uintptr(unsafe.Pointer(lprcPosRect)),
3800,
3810)
382
383return HRESULT(ret)
384}
385
386type IUnknownVtbl struct {
387QueryInterface uintptr
388AddRef uintptr
389Release uintptr
390}
391
392type IUnknown struct {
393LpVtbl *IUnknownVtbl
394}
395
396type OLEINPLACEFRAMEINFO struct {
397Cb uint32
398FMDIApp BOOL
399HwndFrame HWND
400Haccel HACCEL
401CAccelEntries uint32
402}
403
404type COAUTHIDENTITY struct {
405User *uint16
406UserLength uint32
407Domain *uint16
408DomainLength uint32
409Password *uint16
410PasswordLength uint32
411Flags uint32
412}
413
414type COAUTHINFO struct {
415dwAuthnSvc uint32
416dwAuthzSvc uint32
417pwszServerPrincName *uint16
418dwAuthnLevel uint32
419dwImpersonationLevel uint32
420pAuthIdentityData *COAUTHIDENTITY
421dwCapabilities uint32
422}
423
424type COSERVERINFO struct {
425dwReserved1 uint32
426pwszName *uint16
427pAuthInfo *COAUTHINFO
428dwReserved2 uint32
429}
430
431var (
432// Library
433libole32 *windows.LazyDLL
434
435// Functions
436coCreateInstance *windows.LazyProc
437coGetClassObject *windows.LazyProc
438coInitializeEx *windows.LazyProc
439coTaskMemFree *windows.LazyProc
440coUninitialize *windows.LazyProc
441oleInitialize *windows.LazyProc
442oleSetContainedObject *windows.LazyProc
443oleUninitialize *windows.LazyProc
444)
445
446func init() {
447// Library
448libole32 = windows.NewLazySystemDLL("ole32.dll")
449
450// Functions
451coCreateInstance = libole32.NewProc("CoCreateInstance")
452coGetClassObject = libole32.NewProc("CoGetClassObject")
453coInitializeEx = libole32.NewProc("CoInitializeEx")
454coTaskMemFree = libole32.NewProc("CoTaskMemFree")
455coUninitialize = libole32.NewProc("CoUninitialize")
456oleInitialize = libole32.NewProc("OleInitialize")
457oleSetContainedObject = libole32.NewProc("OleSetContainedObject")
458oleUninitialize = libole32.NewProc("OleUninitialize")
459}
460
461func CoCreateInstance(rclsid REFCLSID, pUnkOuter *IUnknown, dwClsContext uint32, riid REFIID, ppv *unsafe.Pointer) HRESULT {
462ret, _, _ := syscall.Syscall6(coCreateInstance.Addr(), 5,
463uintptr(unsafe.Pointer(rclsid)),
464uintptr(unsafe.Pointer(pUnkOuter)),
465uintptr(dwClsContext),
466uintptr(unsafe.Pointer(riid)),
467uintptr(unsafe.Pointer(ppv)),
4680)
469
470return HRESULT(ret)
471}
472
473func CoGetClassObject(rclsid REFCLSID, dwClsContext uint32, pServerInfo *COSERVERINFO, riid REFIID, ppv *unsafe.Pointer) HRESULT {
474ret, _, _ := syscall.Syscall6(coGetClassObject.Addr(), 5,
475uintptr(unsafe.Pointer(rclsid)),
476uintptr(dwClsContext),
477uintptr(unsafe.Pointer(pServerInfo)),
478uintptr(unsafe.Pointer(riid)),
479uintptr(unsafe.Pointer(ppv)),
4800)
481
482return HRESULT(ret)
483}
484
485func CoInitializeEx(reserved unsafe.Pointer, coInit uint32) HRESULT {
486ret, _, _ := syscall.Syscall(coInitializeEx.Addr(), 2,
487uintptr(reserved),
488uintptr(coInit),
4890)
490
491return HRESULT(ret)
492}
493
494func CoUninitialize() {
495syscall.Syscall(coUninitialize.Addr(), 0,
4960,
4970,
4980)
499}
500
501func CoTaskMemFree(pv uintptr) {
502syscall.Syscall(coTaskMemFree.Addr(), 1,
503pv,
5040,
5050)
506}
507
508func OleInitialize() HRESULT {
509ret, _, _ := syscall.Syscall(oleInitialize.Addr(), 1, // WTF, why does 0 not work here?
5100,
5110,
5120)
513
514return HRESULT(ret)
515}
516
517func OleSetContainedObject(pUnknown *IUnknown, fContained bool) HRESULT {
518ret, _, _ := syscall.Syscall(oleSetContainedObject.Addr(), 2,
519uintptr(unsafe.Pointer(pUnknown)),
520uintptr(BoolToBOOL(fContained)),
5210)
522
523return HRESULT(ret)
524}
525
526func OleUninitialize() {
527syscall.Syscall(oleUninitialize.Addr(), 0,
5280,
5290,
5300)
531}
532