Golang调用CM_Register_Notification返回错误31求助
Go调用CM_Register_Notification返回错误31的问题
我正在尝试在Golang中实现Windows 10的CM_Register_Notification接口,明确不想使用RegisterDeviceNotification,但代码始终返回错误31。我已尝试多种CM_NOTIFY_FILTER的有效组合,问题仍未解决,期望返回码为0。相关代码如下:
package main import ( "fmt" "syscall" "unsafe" ) var ( cfgmgr32 = syscall.NewLazyDLL("cfgmgr32.dll") cmRegisterNotification = cfgmgr32.NewProc("CM_Register_Notification") ) type CM_NOTIFY_FILTER_TYPE uint32 const ( CM_NOTIFY_FILTER_TYPE_DEVICEHANDLE CM_NOTIFY_FILTER_TYPE = 0x00000000 CM_NOTIFY_FILTER_TYPE_DEVICEINSTANCE CM_NOTIFY_FILTER_TYPE = 0x00000001 CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE CM_NOTIFY_FILTER_TYPE = 0x00000002 ) type FILTER_FLAGS uint32 const ( None FILTER_FLAGS = 0 CM_NOTIFY_FILTER_FLAG_ALL_INTERFACE_CLASSES FILTER_FLAGS = 0x00000001 CM_NOTIFY_FILTER_FLAG_ALL_DEVICE_INSTANCES FILTER_FLAGS = 0x00000002 ) type CM_NOTIFY_FILTER struct { cbSize uint32 Flags FILTER_FLAGS FilterType CM_NOTIFY_FILTER_TYPE Reserved uint32 u struct { DeviceInterface struct { ClassGuid syscall.GUID } DeviceHandle struct { hTarget syscall.Handle } DeviceInstance struct { InstanceId [200]uint16 } } } type HCMNOTIFICATION uintptr type CM_NOTIFY_ACTION uint32 type PCM_NOTIFY_EVENT_DATA uintptr type PCM_NOTIFY_CALLBACK func(hNotify HCMNOTIFICATION, context unsafe.Pointer, action CM_NOTIFY_ACTION, eventData PCM_NOTIFY_EVENT_DATA, eventDataSize uint32) uintptr func main() { filter := &CM_NOTIFY_FILTER{ cbSize: uint32(unsafe.Sizeof(CM_NOTIFY_FILTER{})), FilterType: CM_NOTIFY_FILTER_TYPE_DEVICEINTERFACE, Flags: CM_NOTIFY_FILTER_FLAG_ALL_INTERFACE_CLASSES, Reserved: 0, u: struct { DeviceInterface struct { ClassGuid syscall.GUID } DeviceHandle struct { hTarget syscall.Handle } DeviceInstance struct { InstanceId [200]uint16 } }{ DeviceInterface: struct{ ClassGuid syscall.GUID }{ ClassGuid: syscall.GUID{Data1: 0x4d36e96c, Data2: 0xe325, Data3: 0x11ce, Data4: [8]byte{0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18}}, }, }, } var hNotify HCMNOTIFICATION ret, ret2, err := cmRegisterNotification.Call( uintptr(unsafe.Pointer(filter)), uintptr(unsafe.Pointer(nil)), syscall.NewCallback(PCM_NOTIFY_CALLBACK(NotifyCallback)), uintptr(unsafe.Pointer(&hNotify)), ) if ret != 0 { fmt.Println("Falha ao registrar notificação") fmt.Println("Falha ao registrar notificação", ret) fmt.Println("Falha ao registrar notificação", ret2) fmt.Println("Falha ao registrar notificação", err) return } fmt.Println("Notificação registrada com sucesso") } func NotifyCallback(hNotify HCMNOTIFICATION, context unsafe.Pointer, action CM_NOTIFY_ACTION, eventData PCM_NOTIFY_EVENT_DATA, eventDataSize uint32) uintptr { switch action { case 0: fmt.Println("Dispositivo de interface conectado") case 1: fmt.Println("Dispositivo de interface removido") default: fmt.Printf("Evento desconhecido: %d\n", action) } return 0 }
内容的提问来源于stack exchange,提问作者lcbrunetti
相关产品推荐
相关产品推荐

