From 834940550bf2830821e3adb56e7ab67377d577b0 Mon Sep 17 00:00:00 2001 From: Alexis Couvreur Date: Sun, 10 May 2026 18:06:38 -0400 Subject: [PATCH] fix(windows): add HRESULT error message --- adapter_windows.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/adapter_windows.go b/adapter_windows.go index 1370fbd0..c3481851 100644 --- a/adapter_windows.go +++ b/adapter_windows.go @@ -3,6 +3,7 @@ package bluetooth import ( "errors" "fmt" + "strings" "syscall" "unsafe" @@ -10,6 +11,7 @@ import ( "github.com/saltosystems/winrt-go" "github.com/saltosystems/winrt-go/windows/devices/bluetooth/advertisement" "github.com/saltosystems/winrt-go/windows/foundation" + "golang.org/x/sys/windows" ) var _ BLEAdapter = (*Adapter)(nil) @@ -107,6 +109,23 @@ func hresultToError(hr uint32) error { return fmt.Errorf("Bluetooth ATT error (code 0x%04X)", code) } + // For FACILITY_WIN32 the lower 16 bits are a plain Win32 error code that + // FormatMessage understands; for other facilities try the full HRESULT. + errCode := hr + if facility == 0x7 { // FACILITY_WIN32 + errCode = code + } + + buf := make([]uint16, 512) + n, err := windows.FormatMessage( + windows.FORMAT_MESSAGE_FROM_SYSTEM|windows.FORMAT_MESSAGE_IGNORE_INSERTS, + 0, errCode, 0, buf, nil, + ) + if err == nil && n > 0 { + msg := strings.TrimRight(windows.UTF16ToString(buf[:n]), " \r\n") + return fmt.Errorf("HRESULT 0x%08X: %s", hr, msg) + } + return fmt.Errorf("HRESULT 0x%08X", hr) }