/// <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) { Int32 stringSize; try { DEV_BROADCAST_DEVICEINTERFACE_1 devBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE_1(); DEV_BROADCAST_HDR devBroadcastHeader = new DEV_BROADCAST_HDR(); // The LParam parameter of Message is a pointer to a DEV_BROADCAST_HDR structure. Marshal.PtrToStructure(m.LParam, devBroadcastHeader); if ((devBroadcastHeader.dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)) { // 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. stringSize = System.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. String 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. if ((String.Compare(DeviceNameString, mydevicePathName, true) == 0)) { return true; } else { return false; } } } catch (Exception ex) { throw; } 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) { Int32 stringSize; try { DEV_BROADCAST_DEVICEINTERFACE_1 devBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE_1(); DEV_BROADCAST_HDR devBroadcastHeader = new DEV_BROADCAST_HDR(); // The LParam parameter of Message is a pointer to a DEV_BROADCAST_HDR structure. Marshal.PtrToStructure(m.LParam, devBroadcastHeader); if ((devBroadcastHeader.dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)) { // 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. stringSize = System.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. String 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. if ((String.Compare(DeviceNameString, mydevicePathName, true) == 0)) { return(true); } else { return(false); } } } catch (Exception ex) { throw; } return(false); }
/// <summary> /// isNotificationForTargetDevice - Compares the target devices pathname against the /// pathname of the device which caused the event message /// </summary> private Boolean isNotificationForTargetDevice(Message m) { Int32 stringSize; try { DEV_BROADCAST_DEVICEINTERFACE_1 devBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE_1(); DEV_BROADCAST_HDR devBroadcastHeader = new DEV_BROADCAST_HDR(); Marshal.PtrToStructure(m.LParam, devBroadcastHeader); // Is the notification event concerning a device interface? if ((devBroadcastHeader.dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)) { // Get the device path name of the affected device stringSize = System.Convert.ToInt32((devBroadcastHeader.dbch_size - 32) / 2); devBroadcastDeviceInterface.dbcc_name = new Char[stringSize + 1]; Marshal.PtrToStructure(m.LParam, devBroadcastDeviceInterface); String deviceNameString = new String(devBroadcastDeviceInterface.dbcc_name, 0, stringSize); // Compare the device name with our target device's pathname (strings are moved to lower case // using en-US to ensure case insensitivity accross different regions) if ((String.Compare(deviceNameString.ToLower(new System.Globalization.CultureInfo("en-US")), deviceInformation.devicePathName.ToLower(new System.Globalization.CultureInfo("en-US")), true) == 0)) return true; else return false; } } catch (Exception) { Debug.WriteLine("usbGenericHidCommunication:isNotificationForTargetDevice() -> EXCEPTION: An unknown exception has occured!"); return false; } return false; }
// Get device name from notification message. // Also checks checkGuid with the GUID from the message to check the notification // is for a relevant device. Other messages might be received. public static string GetNotifyMessageDeviceName(IntPtr pDevBroadcastHeader, Guid checkGuid) { int stringSize; DEV_BROADCAST_DEVICEINTERFACE_1 devBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE_1(); DEV_BROADCAST_HDR devBroadcastHeader = new DEV_BROADCAST_HDR(); // The LParam parameter of Message is a pointer to a DEV_BROADCAST_HDR structure. Marshal.PtrToStructure(pDevBroadcastHeader, devBroadcastHeader); if ((devBroadcastHeader.dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)) { // 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. stringSize = System.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(pDevBroadcastHeader, devBroadcastDeviceInterface); // Check if message is for the GUID if (devBroadcastDeviceInterface.dbcc_classguid != checkGuid) { return(null); } // Store the device name in a String. string deviceNameString = new String(devBroadcastDeviceInterface.dbcc_name, 0, stringSize); return(deviceNameString); } else if ((devBroadcastHeader.dbch_devicetype == DBT_DEVTYP_VOLUME)) { DEV_BROADCAST_VOLUME vol = new DEV_BROADCAST_VOLUME(); Marshal.PtrToStructure(pDevBroadcastHeader, vol); Int32 Mask = vol.dbcv_unitmask; Int32 DriveInt = Convert.ToInt32('A') - 1; do { Mask /= 2; DriveInt++; }while (Mask > 0); return(@"\\.\" + Convert.ToChar(DriveInt) + ":"); } 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"> 一个 WM_DEVICECHANGE 消息. 调用 RegisterDeviceNotification 函数 /// 以便 WM_DEVICECHANGE 消息可以传递到 OnDeviceChange 例行程序.. </param> /// <param name="mydevicePathName"> a device pathname returned by /// SetupDiGetDeviceInterfaceDetail in an SP_DEVICE_INTERFACE_DETAIL_DATA structure. </param> /// <returns> /// 名称匹配返回True,否则返回False /// </returns> public Boolean DeviceNameMatch(Message m, String mydevicePathName) { Int32 stringSize; try { DEV_BROADCAST_DEVICEINTERFACE_1 devBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE_1(); DEV_BROADCAST_HDR devBroadcastHeader = new DEV_BROADCAST_HDR(); // Message 的 LParam 参数 is 指向 DEV_BROADCAST_HDR 结构的指针. Marshal.PtrToStructure(m.LParam, devBroadcastHeader); if ((devBroadcastHeader.dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)) { // dbch_devicetype 参数指出了应用设备的接口 // 因此 LParam 参数实际是以DEV_BROADCAST_HDR开始的 DEV_BROADCAST_INTERFACE结构 // 通过减去结构中不是dbch_name部分的32字节后得到了字符的个数,由于一个字符两个字节,所以再除以2 stringSize = System.Convert.ToInt32((devBroadcastHeader.dbch_size - 32) / 2); // devBroadcastDeviceInterface中的dbcc_name 参数包含了设备名称 // 去掉前后的空格以匹配字符串长度 devBroadcastDeviceInterface.dbcc_name = new Char[stringSize + 1]; // 将数据从不受管理的数据块传输到受管理的devBroadcastDeviceInterface对象中 Marshal.PtrToStructure(m.LParam, devBroadcastDeviceInterface); // 将设备名用String类型存储 String DeviceNameString = new String(devBroadcastDeviceInterface.dbcc_name, 0, stringSize); // 比较新连接的设备名字和我们已知的设备(mydevicePathName)名字,匹配返回true if ((String.Compare(DeviceNameString, mydevicePathName, true) == 0)) { return(true); } else { return(false); } } } catch (Exception) { throw; } return(false); }
// Get device name from notification message. // Also checks checkGuid with the GUID from the message to check the notification // is for a relevant device. Other messages might be received. public static string GetNotifyMessageDeviceName(Message m, Guid checkGuid) { int stringSize; DEV_BROADCAST_DEVICEINTERFACE_1 devBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE_1(); DEV_BROADCAST_HDR devBroadcastHeader = new DEV_BROADCAST_HDR(); // The LParam parameter of Message is a pointer to a DEV_BROADCAST_HDR structure. Marshal.PtrToStructure(m.LParam, devBroadcastHeader); if ((devBroadcastHeader.dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)) { // 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. stringSize = System.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); // Check if message is for the GUID if (devBroadcastDeviceInterface.dbcc_classguid != checkGuid) return null; // Store the device name in a String. string deviceNameString = new String(devBroadcastDeviceInterface.dbcc_name, 0, stringSize); return deviceNameString; } return null; }
/// <summary> /// /// </summary> /// <param name="LParam"></param> /// <returns></returns> public static bool NameAndClass(IntPtr LParam, ref String name) { DEV_BROADCAST_DEVICEINTERFACE_1 devBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE_1(); DEV_BROADCAST_HDR devBroadcastHeader = new DEV_BROADCAST_HDR(); Marshal.PtrToStructure(LParam, devBroadcastHeader); if ((devBroadcastHeader.dbch_devicetype == DbtDevtypDeviceinterface)) { Int32 stringSize = Convert.ToInt32((devBroadcastHeader.dbch_size - 32) / 2); Array.Resize(ref devBroadcastDeviceInterface.dbcc_name, stringSize); Marshal.PtrToStructure(LParam, devBroadcastDeviceInterface); name = new String(devBroadcastDeviceInterface.dbcc_name, 0, stringSize); name = FriendlyName(name); return(true); } return(false); }
/// <summary> /// isNotificationForTargetDevice - Compares the target devices pathname against the /// pathname of the device which caused the event message /// </summary> private Boolean isNotificationForTargetDevice(Message m) { Int32 stringSize; try { DEV_BROADCAST_DEVICEINTERFACE_1 devBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE_1(); DEV_BROADCAST_HDR devBroadcastHeader = new DEV_BROADCAST_HDR(); Marshal.PtrToStructure(m.LParam, devBroadcastHeader); // Is the notification event concerning a device interface? if ((devBroadcastHeader.dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)) { // Get the device path name of the affected device stringSize = System.Convert.ToInt32((devBroadcastHeader.dbch_size - 32) / 2); devBroadcastDeviceInterface.dbcc_name = new Char[stringSize + 1]; Marshal.PtrToStructure(m.LParam, devBroadcastDeviceInterface); String deviceNameString = new String(devBroadcastDeviceInterface.dbcc_name, 0, stringSize); // Compare the device name with our target device's pathname (strings are moved to lower case // using en-US to ensure case insensitivity accross different regions) if ((String.Compare(deviceNameString.ToLower(new System.Globalization.CultureInfo("en-US")), deviceInformation.devicePathName.ToLower(new System.Globalization.CultureInfo("en-US")), true) == 0)) { return(true); } else { return(false); } } } catch (Exception ex) { MessageBox.Show(ex.Message); Debug.WriteLine("usbGenericHidCommunication:isNotificationForTargetDevice() -> EXCEPTION: An unknown exception has occured!"); return(false); } return(false); }
/// <summary> /// isNotificationForTargetDevice - Compares the target devices pathname against the /// pathname of the device which caused the event message /// </summary> private Boolean isNotificationForTargetDevice(Message m) { Int32 stringSize; try { DEV_BROADCAST_DEVICEINTERFACE_1 devBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE_1(); DEV_BROADCAST_HDR devBroadcastHeader = new DEV_BROADCAST_HDR(); Marshal.PtrToStructure(m.LParam, devBroadcastHeader); // Is the notification event concerning a device interface? if ((devBroadcastHeader.dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)) { // Get the device path name of the affected device stringSize = System.Convert.ToInt32((devBroadcastHeader.dbch_size - 32) / 2); devBroadcastDeviceInterface.dbcc_name = new Char[stringSize + 1]; Marshal.PtrToStructure(m.LParam, devBroadcastDeviceInterface); String deviceNameString = new String(devBroadcastDeviceInterface.dbcc_name, 0, stringSize); // Compare the device name with our target device's pathname if ((String.Compare(deviceNameString, deviceInformation.devicePathName, true) == 0)) { return(true); } else { return(false); } } } catch (Exception) { Debug.WriteLine("usbGenericHidCommunication:isNotificationForTargetDevice() -> EXCEPTION: An unknown exception has occured!"); return(false); } return(false); }
internal void OnDeviceChange(Message m) { DEV_BROADCAST_DEVICEINTERFACE_1 devBroadcastDeviceInterface = new DEV_BROADCAST_DEVICEINTERFACE_1(); DEV_BROADCAST_HDR devBroadcastHeader = new DEV_BROADCAST_HDR(); if ((int)m.LParam != 0) { Marshal.PtrToStructure(m.LParam, devBroadcastHeader); } else { return; } if ((devBroadcastHeader.dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE)) { Int32 stringSize = Convert.ToInt32((devBroadcastHeader.dbch_size - 32) / 2); Array.Resize(ref devBroadcastDeviceInterface.dbcc_name, stringSize); Marshal.PtrToStructure(m.LParam, devBroadcastDeviceInterface); String deviceNameString = new String(devBroadcastDeviceInterface.dbcc_name, 0, stringSize); USB_Printing = true; if ((m.WParam.ToInt32() == DBT_DEVICEARRIVAL)) { textBox1.AppendText(deviceNameString); textBox1.AppendText(" attached\r\n"); } else if ((m.WParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE)) { textBox1.AppendText(deviceNameString); textBox1.AppendText(" removed\r\n"); } USB_Printing = false; } }