Skip to content

Instantly share code, notes, and snippets.

@aykevl
Created May 26, 2020 12:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aykevl/fbca8694e74a8630f49db1cdbb232300 to your computer and use it in GitHub Desktop.
Save aykevl/fbca8694e74a8630f49db1cdbb232300 to your computer and use it in GitHub Desktop.
Querying static WinRT methods using Go
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