//Name: HookHardwareNotifications //Inputs: Handle to a window or service, // Boolean specifying true if the handle belongs to a window //Outputs: false if fail, otherwise true //Errors: This method may log the following errors. // NONE //Remarks: Allow a window or service to receive ALL hardware notifications. // NOTE: I have yet to figure out how to make this work properly // for a service written in C#, though it kicks butt in C++. At any // rate, it works fine for windows forms in either. private bool HookHardwareNotifications(IntPtr callback, bool UseWindowHandle) { try { Native.DEV_BROADCAST_DEVICEINTERFACE dbdi = new Native.DEV_BROADCAST_DEVICEINTERFACE(); dbdi.dbcc_size = Marshal.SizeOf(dbdi); dbdi.dbcc_reserved = 0; dbdi.dbcc_devicetype = Native.DBT_DEVTYP_DEVICEINTERFACE; if (UseWindowHandle) { Native.RegisterDeviceNotification(callback, dbdi, Native.DEVICE_NOTIFY_ALL_INTERFACE_CLASSES | Native.DEVICE_NOTIFY_WINDOW_HANDLE); } else { Native.RegisterDeviceNotification(callback, dbdi, Native.DEVICE_NOTIFY_ALL_INTERFACE_CLASSES | Native.DEVICE_NOTIFY_SERVICE_HANDLE); } return(true); } catch (Exception ex) { string err = ex.Message; return(false); } }
//Name: HookHardwareNotifications //Inputs: Handle to a window or service, // Boolean specifying true if the handle belongs to a window //Outputs: false if fail, otherwise true //Errors: This method may log the following errors. // NONE //Remarks: Allow a window or service to receive ALL hardware notifications. // NOTE: I have yet to figure out how to make this work properly // for a service written in C#, though it kicks butt in C++. At any // rate, it works fine for windows forms in either. private bool HookHardwareNotifications(IntPtr callback, bool UseWindowHandle) { try { Native.DEV_BROADCAST_DEVICEINTERFACE dbdi = new Native.DEV_BROADCAST_DEVICEINTERFACE(); dbdi.dbcc_size = Marshal.SizeOf(dbdi); dbdi.dbcc_reserved = 0; dbdi.dbcc_devicetype = Native.DBT_DEVTYP_DEVICEINTERFACE; if (UseWindowHandle) { Native.RegisterDeviceNotification(callback, dbdi, Native.DEVICE_NOTIFY_ALL_INTERFACE_CLASSES | Native.DEVICE_NOTIFY_WINDOW_HANDLE); } else { Native.RegisterDeviceNotification(callback, dbdi, Native.DEVICE_NOTIFY_ALL_INTERFACE_CLASSES | Native.DEVICE_NOTIFY_SERVICE_HANDLE); } return true; } catch (Exception ex) { string err = ex.Message; return false; } }