public static bool IsNotificationForTargetDevice(Message m) { if (string.IsNullOrEmpty(devicepath)) { return(false); } try { var devBroadcastDeviceInterface = new DevBroadcastDeviceinterface1(); var devBroadcastHeader = new DevBroadcastHdr(); try { Marshal.PtrToStructure(m.LParam, devBroadcastHeader); } catch (Exception ex) { Debug.WriteLine(ex.Message); return(false); } // Is the notification event concerning a device interface? if ((devBroadcastHeader.dbch_devicetype == Constants.DbtDevtypDeviceinterface)) { // Get the device path name of the affected device var stringSize = Convert.ToInt32((devBroadcastHeader.dbch_size - 32) / 2); devBroadcastDeviceInterface.dbcc_name = new Char[stringSize + 1]; Marshal.PtrToStructure(m.LParam, devBroadcastDeviceInterface); var deviceNameString = new string(devBroadcastDeviceInterface.dbcc_name, 0, stringSize); // Compare the device name with our target device's pathname (strings are moved to lower case return(string.Compare(deviceNameString.ToLower(), devicepath.ToLower(), StringComparison.OrdinalIgnoreCase) == 0); } } catch (Exception ex) { Debug.WriteLine("usbGenericHidCommunication:isNotificationForTargetDevice() -> EXCEPTION: An unknown exception has occured!"); Debug.WriteLine(ex.Message); return(false); } return(false); }
private string GetDeviceName(Message m) { try { if (m.LParam == IntPtr.Zero) { return(null); } var devBroadcastDeviceInterface = new DevBroadcastDeviceinterface1(); var devBroadcastHeader = new DevBroadcastHdr(); try { Marshal.PtrToStructure(m.LParam, devBroadcastHeader); } catch (Exception ex) { LogHelper.Log.Error(ex.Message); } // Is the notification event concerning a device interface? if ((devBroadcastHeader.dbch_devicetype == Constants.DbtDevtypDeviceinterface)) { // Get the device path name of the affected device var stringSize = Convert.ToInt32((devBroadcastHeader.dbch_size - 32) / 2); devBroadcastDeviceInterface.dbcc_name = new Char[stringSize + 1]; Marshal.PtrToStructure(m.LParam, devBroadcastDeviceInterface); var deviceNameString = new string(devBroadcastDeviceInterface.dbcc_name, 0, stringSize); return(deviceNameString); // Compare the device name with our target device's pathname (strings are moved to lower case //return (string.Compare(deviceNameString.ToLower(), devicepath.ToLower(), StringComparison.OrdinalIgnoreCase) == 0); } } catch (Exception ex) { LogHelper.Log.Error("GetDeviceName(Message m) -> 发生异常:", ex); } return(null); }
/// <summary> /// Compares two device path names. Used to find out if the device name /// of a recently attached or removed device matches the name of a /// device the application is communicating with. /// </summary> /// /// <param name="m"> a WM_DEVICECHANGE message. A call to RegisterDeviceNotification /// causes WM_DEVICECHANGE messages to be passed to an OnDeviceChange routine.. </param> /// <param name="mydevicePathName"> a device pathname returned by /// SetupDiGetDeviceInterfaceDetail in an SP_DEVICE_INTERFACE_DETAIL_DATA structure. </param> /// /// <returns> /// True if the names match, False if not. /// </returns> /// internal Boolean DeviceNameMatch(Message m, String mydevicePathName) { var devBroadcastDeviceInterface = new DevBroadcastDeviceinterface1(); var devBroadcastHeader = new DevBroadcastHdr(); // The LParam parameter of Message is a pointer to a DEV_BROADCAST_HDR structure. Marshal.PtrToStructure(m.LParam, devBroadcastHeader); if ((devBroadcastHeader.dbch_devicetype == DbtDevtypDeviceinterface)) { // The dbch_devicetype parameter indicates that the event applies to a device interface. // So the structure in LParam is actually a DEV_BROADCAST_INTERFACE structure, // which begins with a DEV_BROADCAST_HDR. // Obtain the number of characters in dbch_name by subtracting the 32 bytes // in the strucutre that are not part of dbch_name and dividing by 2 because there are // 2 bytes per character. var stringSize = Convert.ToInt32((devBroadcastHeader.dbch_size - 32) / 2); // The dbcc_name parameter of devBroadcastDeviceInterface contains the device name. // Trim dbcc_name to match the size of the String. devBroadcastDeviceInterface.dbcc_name = new Char[stringSize + 1]; // Marshal data from the unmanaged block pointed to by m.LParam // to the managed object devBroadcastDeviceInterface. Marshal.PtrToStructure(m.LParam, devBroadcastDeviceInterface); // Store the device name in a String. var deviceNameString = new String(devBroadcastDeviceInterface.dbcc_name, 0, stringSize); // Compare the name of the newly attached device with the name of the device // the application is accessing (mydevicePathName). // Set ignorecase True. return(String.Compare(deviceNameString, mydevicePathName, StringComparison.OrdinalIgnoreCase) == 0); } return(false); }
/// <summary> /// Compares two device path names. Used to find out if the device name /// of a recently attached or removed device matches the name of a /// device the application is communicating with. /// </summary> /// /// <param name="m"> a WM_DEVICECHANGE message. A call to RegisterDeviceNotification /// causes WM_DEVICECHANGE messages to be passed to an OnDeviceChange routine.. </param> /// <param name="mydevicePathName"> a device pathname returned by /// SetupDiGetDeviceInterfaceDetail in an SP_DEVICE_INTERFACE_DETAIL_DATA structure. </param> /// /// <returns> /// True if the names match, False if not. /// </returns> /// internal Boolean DeviceNameMatch(Message m, String mydevicePathName) { var devBroadcastDeviceInterface = new DevBroadcastDeviceinterface1(); var devBroadcastHeader = new DevBroadcastHdr(); // The LParam parameter of Message is a pointer to a DEV_BROADCAST_HDR structure. Marshal.PtrToStructure(m.LParam, devBroadcastHeader); if ((devBroadcastHeader.dbch_devicetype == DbtDevtypDeviceinterface)) { // The dbch_devicetype parameter indicates that the event applies to a device interface. // So the structure in LParam is actually a DEV_BROADCAST_INTERFACE structure, // which begins with a DEV_BROADCAST_HDR. // Obtain the number of characters in dbch_name by subtracting the 32 bytes // in the strucutre that are not part of dbch_name and dividing by 2 because there are // 2 bytes per character. var stringSize = Convert.ToInt32((devBroadcastHeader.dbch_size - 32) / 2); // The dbcc_name parameter of devBroadcastDeviceInterface contains the device name. // Trim dbcc_name to match the size of the String. devBroadcastDeviceInterface.dbcc_name = new Char[stringSize + 1]; // Marshal data from the unmanaged block pointed to by m.LParam // to the managed object devBroadcastDeviceInterface. Marshal.PtrToStructure(m.LParam, devBroadcastDeviceInterface); // Store the device name in a String. var deviceNameString = new String(devBroadcastDeviceInterface.dbcc_name, 0, stringSize); // Compare the name of the newly attached device with the name of the device // the application is accessing (mydevicePathName). // Set ignorecase True. return (String.Compare(deviceNameString, mydevicePathName, StringComparison.OrdinalIgnoreCase) == 0); } return false; }