Created
May 26, 2020 12:30
-
-
Save aykevl/fbca8694e74a8630f49db1cdbb232300 to your computer and use it in GitHub Desktop.
Querying static WinRT methods using Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"unsafe" | |
"syscall" | |
"github.com/go-ole/go-ole" | |
) | |
type IBluetoothAdapterStatics struct { | |
ole.IInspectable | |
} | |
type IBluetoothAdapterStaticsVtbl struct { | |
ole.IInspectableVtbl | |
GetDeviceSelector uintptr | |
FromIdAsync uintptr | |
GetDefaultAsync uintptr | |
} | |
func (v *IBluetoothAdapterStatics) VTable() *IBluetoothAdapterStaticsVtbl { | |
return (*IBluetoothAdapterStaticsVtbl)(unsafe.Pointer(v.RawVTable)) | |
} | |
func (v *IBluetoothAdapterStatics) GetDeviceSelector() (id string, err error) { | |
var hstring ole.HString | |
hr, _, _ := syscall.Syscall( | |
v.VTable().GetDeviceSelector, | |
2, | |
uintptr(unsafe.Pointer(v)), | |
uintptr(unsafe.Pointer(&hstring)), | |
0) | |
if hr != 0 { | |
err = ole.NewError(hr) | |
return | |
} | |
id = hstring.String() | |
ole.DeleteHString(hstring) | |
return | |
} | |
func main() { | |
ole.RoInitialize(1) | |
factory, _ := ole.RoGetActivationFactory("Windows.Devices.Bluetooth.BluetoothAdapter", ole.IID_IInspectable) | |
adapterObj, _ := factory.QueryInterface(ole.NewGUID("8B02FB6A-AC4C-4741-8661-8EAB7D17EA9F")) | |
adapter := (*IBluetoothAdapterStatics)(unsafe.Pointer(adapterObj)) | |
selector, _ := adapter.GetDeviceSelector() | |
println("selector:", selector) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment