I'm trying to call function RegisterDeviceNotificationW of user32.dll. But the first param of the function is the "handle to the window or service that will receive device events" (this I getted from Microsoft).
Based on Cloud Printer Connector I tried to get the handler with svc.StatusHandler(), but does not work for me, every time I run I get the follow error:
The handle is invalid.
Well, using the same code of the examples of sys I created my own "service", replacing the beep function by RegisterDeviceNotification (the same code of google) and sent the name of my service and the value returned from svc.StatusHandler(), but I received again the same message: "The handle is invalid."
Function RegisterDeviceNotification
var (
u32 = syscall.MustLoadDLL("user32.dll")
registerDeviceNotificationProc = u32.MustFindProc("RegisterDeviceNotificationW")
)
Here is where I need sent a valid Handler
func RegisterDeviceNotification(handle windows.Handle) error {
var notificationFilter DevBroadcastDevinterface
notificationFilter.dwSize = uint32(unsafe.Sizeof(notificationFilter))
notificationFilter.dwDeviceType = DBT_DEVTYP_DEVICEINTERFACE
notificationFilter.dwReserved = 0
// BUG(pastarmovj): This class is ignored for now. Figure out what the right GUID is.
notificationFilter.classGuid = PRINTERS_DEVICE_CLASS
notificationFilter.szName = 0
r1, _, err := registerDeviceNotificationProc.Call(uintptr(handle), uintptr(unsafe.Pointer(¬ificationFilter)), DEVICE_NOTIFY_SERVICE_HANDLE|DEVICE_NOTIFY_ALL_INTERFACE_CLASSES)
if r1 == 0 {
return err
}
return nil
}
Call the function
func (m *myservice) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
h := windows.Handle(uintptr(unsafe.Pointer(m)))
err := RegisterDeviceNotification(h)
fmt.Println(err)
}
NOTE: I tried too with the pointer of "myService":
h := windows.Handle(uintptr(unsafe.Pointer(m)))
err := RegisterDeviceNotification(h)
EDIT: I tried with GetCurrentProcess, but does not work:
Retrieves a pseudo handle for the current process.
h, err := syscall.GetCurrentProcess()
err = RegisterDeviceNotification(windows.Handle(h))
The question: How can I get a valid handle of the process in Go?
PS: If exists any problem with my question, please, let me know to improve.
hwhich is the handle you need. github.com/golang/sys/blob/…