private void HandleDeviceChange(Message m) { if (m.WParam.ToInt32() == NativeApi.DBT_DEVICEARRIVAL) { NativeApi.DEV_BROADCAST_HDR DevBroadcastHeader = new NativeApi.DEV_BROADCAST_HDR(); NativeApi.DEV_BROADCAST_DEVICEINTERFACE_1 DevBroadcastDeviceInterface = new NativeApi.DEV_BROADCAST_DEVICEINTERFACE_1(); try { Marshal.PtrToStructure(m.LParam, DevBroadcastHeader); if (DevBroadcastHeader.dbch_devicetype == NativeApi.DBT_DEVTYP_DEVICEINTERFACE) { int StringSize = System.Convert.ToInt32((DevBroadcastHeader.dbch_size - 28) / 2); DevBroadcastDeviceInterface.dbcc_name = new char[StringSize + 1]; Marshal.PtrToStructure(m.LParam, DevBroadcastDeviceInterface); string DeviceNameString = new string(DevBroadcastDeviceInterface.dbcc_name, 0, StringSize); DeviceNameString = DeviceNameString.Substring(0, DeviceNameString.IndexOf('}') + 1); DeviceNameString = DeviceNameString.ToLowerInvariant(); AddDeviceToList(DeviceNameString); } } catch { EmitLogMessage(HidLogMessageType.Warning, "Error on arrival!"); } } else if (m.WParam.ToInt32() == NativeApi.DBT_DEVICEREMOVECOMPLETE) { NativeApi.DEV_BROADCAST_HDR DevBroadcastHeader = new NativeApi.DEV_BROADCAST_HDR(); NativeApi.DEV_BROADCAST_DEVICEINTERFACE_1 DevBroadcastDeviceInterface = new NativeApi.DEV_BROADCAST_DEVICEINTERFACE_1(); string DeviceNameString; try { Marshal.PtrToStructure(m.LParam, DevBroadcastHeader); if (DevBroadcastHeader.dbch_devicetype == NativeApi.DBT_DEVTYP_DEVICEINTERFACE) { int StringSize = System.Convert.ToInt32((DevBroadcastHeader.dbch_size - 28) / 2); DevBroadcastDeviceInterface.dbcc_name = new char[StringSize + 1]; Marshal.PtrToStructure(m.LParam, DevBroadcastDeviceInterface); DeviceNameString = new string(DevBroadcastDeviceInterface.dbcc_name, 0, StringSize); DeviceNameString = DeviceNameString.Substring(0, DeviceNameString.IndexOf('}') + 1); DeviceNameString = DeviceNameString.ToLowerInvariant(); RemoveDeviceFromList(DeviceNameString); } } catch { EmitLogMessage(HidLogMessageType.Warning, "Error on removal!"); } } }
private void HandleDeviceChange(Message m) { if (m.WParam.ToInt32() == NativeApi.DBT_DEVICEARRIVAL) { NativeApi.DEV_BROADCAST_HDR DevBroadcastHeader = new NativeApi.DEV_BROADCAST_HDR(); NativeApi.DEV_BROADCAST_DEVICEINTERFACE_1 DevBroadcastDeviceInterface = new NativeApi.DEV_BROADCAST_DEVICEINTERFACE_1(); try { Marshal.PtrToStructure(m.LParam, DevBroadcastHeader); if (DevBroadcastHeader.dbch_devicetype == NativeApi.DBT_DEVTYP_DEVICEINTERFACE) { int StringSize = System.Convert.ToInt32((DevBroadcastHeader.dbch_size - 28) / 2); DevBroadcastDeviceInterface.dbcc_name = new char[StringSize + 1]; Marshal.PtrToStructure(m.LParam, DevBroadcastDeviceInterface); string DeviceNameString = new string(DevBroadcastDeviceInterface.dbcc_name, 0, StringSize); DeviceNameString = DeviceNameString.Substring(0, DeviceNameString.IndexOf('}') + 1); DeviceNameString = DeviceNameString.ToLowerInvariant(); AddDeviceToList(DeviceNameString); } } catch { EmitLogMessage(HidLogMessageType.Warning, "Error on arrival!"); } } else if (m.WParam.ToInt32() == NativeApi.DBT_DEVICEREMOVECOMPLETE) { NativeApi.DEV_BROADCAST_HDR DevBroadcastHeader = new NativeApi.DEV_BROADCAST_HDR(); NativeApi.DEV_BROADCAST_DEVICEINTERFACE_1 DevBroadcastDeviceInterface = new NativeApi.DEV_BROADCAST_DEVICEINTERFACE_1(); string DeviceNameString; try { Marshal.PtrToStructure(m.LParam, DevBroadcastHeader); if (DevBroadcastHeader.dbch_devicetype == NativeApi.DBT_DEVTYP_DEVICEINTERFACE) { int StringSize = System.Convert.ToInt32((DevBroadcastHeader.dbch_size - 28) / 2); DevBroadcastDeviceInterface.dbcc_name = new char[StringSize + 1]; Marshal.PtrToStructure(m.LParam, DevBroadcastDeviceInterface); DeviceNameString = new string(DevBroadcastDeviceInterface.dbcc_name, 0, StringSize); DeviceNameString = DeviceNameString.Substring(0, DeviceNameString.IndexOf('}')+1); DeviceNameString = DeviceNameString.ToLowerInvariant(); RemoveDeviceFromList(DeviceNameString); } } catch { EmitLogMessage(HidLogMessageType.Warning, "Error on removal!"); } } }