| 1 | //go:build windows |
| 2 | |
| 3 | package combridge |
| 4 | |
| 5 | import ( |
| 6 | "syscall" |
| 7 | "unsafe" |
| 8 | |
| 9 | "golang.org/x/sys/windows" |
| 10 | ) |
| 11 | |
| 12 | // IUnknownFromPointer cast a generic pointer into a IUnknownImpl pointer |
| 13 | func IUnknownFromPointer(ref unsafe.Pointer) *IUnknownImpl { |
| 14 | return (*IUnknownImpl)(ref) |
| 15 | } |
| 16 | |
| 17 | // IUnknownFromPointer cast native pointer into a IUnknownImpl pointer |
| 18 | func IUnknownFromUintptr(ref uintptr) *IUnknownImpl { |
| 19 | return IUnknownFromPointer(unsafe.Pointer(ref)) |
| 20 | } |
| 21 | |
| 22 | type IUnknownVtbl struct { |
| 23 | queryInterface uintptr |
| 24 | addRef uintptr |
| 25 | release uintptr |
| 26 | } |
| 27 | |
| 28 | func (i *IUnknownVtbl) QueryInterface(this unsafe.Pointer, refiid *windows.GUID, ppvObject **IUnknownImpl) error { |
| 29 | r, _, _ := syscall.SyscallN( |
| 30 | i.queryInterface, |
| 31 | uintptr(this), |
| 32 | uintptr(unsafe.Pointer(refiid)), |
| 33 | uintptr(unsafe.Pointer(ppvObject)), |
| 34 | ) |
| 35 | |
| 36 | if r != uintptr(windows.S_OK) { |
| 37 | return syscall.Errno(r) |
| 38 | } |
| 39 | |
| 40 | return nil |
| 41 | } |
| 42 | |
| 43 | func (i *IUnknownVtbl) AddRef(this unsafe.Pointer) uint32 { |
| 44 | r, _, _ := syscall.SyscallN( |
| 45 | i.addRef, |
| 46 | uintptr(this), |
| 47 | ) |
| 48 | return uint32(r) |
| 49 | } |
| 50 | |
| 51 | func (i *IUnknownVtbl) Release(this unsafe.Pointer) uint32 { |
| 52 | r, _, _ := syscall.SyscallN( |
| 53 | i.release, |
| 54 | uintptr(this), |
| 55 | ) |
| 56 | |
| 57 | return uint32(r) |
| 58 | } |
| 59 | |
| 60 | type IUnknownImpl struct { |
| 61 | vtbl *IUnknownVtbl |
| 62 | } |
| 63 | |
| 64 | func (i *IUnknownImpl) QueryInterface(refiid *windows.GUID, ppvObject **IUnknownImpl) error { |
| 65 | return i.vtbl.QueryInterface(unsafe.Pointer(i), refiid, ppvObject) |
| 66 | } |
| 67 | |
| 68 | func (i *IUnknownImpl) AddRef() uint32 { |
| 69 | return i.vtbl.AddRef(unsafe.Pointer(i)) |
| 70 | } |
| 71 | |
| 72 | func (i *IUnknownImpl) Release() uint32 { |
| 73 | return i.vtbl.Release(unsafe.Pointer(i)) |
| 74 | } |
| 75 |