//Manufacturer = unsafeNativeMethodsWrapper.GetProperty(devicePropertyFactory, deviceInfoSet, deviceInfoData, Spdrp.Mfg);
        //var hardwareIDs = GetMultiStringProperty(deviceInfoSet, deviceInfoData, SPDRP.SPDRP_HARDWAREID);

        //var regex = new Regex("^USB\\\\VID_([0-9A-F]{4})&PID_([0-9A-F]{4})", RegexOptions.IgnoreCase);
        //var foundVidPid = false;
        //foreach (var hardwareID in hardwareIDs)
        //{
        //    var match = regex.Match(hardwareID);
        //    if (match.Success)
        //    {
        //        details.VID = ushort.Parse(match.Groups[1].Value, System.Globalization.NumberStyles.AllowHexSpecifier);
        //        details.PID = ushort.Parse(match.Groups[2].Value, System.Globalization.NumberStyles.AllowHexSpecifier);
        //        foundVidPid = true;
        //        break;
        //    }
        //}

        //if (!foundVidPid)
        //    throw new APIException("Failed to find VID and PID for USB device. No hardware ID could be parsed.");
        /// <summary>
        /// The dispose.
        /// </summary>
        public void Dispose()
        {
            if (DeviceInterfaceDetailBuffer != IntPtr.Zero)
            {
                _marshalWrapper.FreeHGlobal(DeviceInterfaceDetailBuffer);
            }
        }
Пример #2
0
        /// <summary>
        /// The register for device notifications.
        /// </summary>
        /// <param name="windowHandleToReceiveNotifications">
        /// The window handle to receive notifications.
        /// </param>
        /// <returns>
        /// The <see cref="ProcessHandleResult"/>.
        /// </returns>
        public IProcessHandleResult Register(IntPtr windowHandleToReceiveNotifications)
        {
            var devBroadcastDeviceInterface       = _broadcastDeviceInterfaceFactory.CreateBroadcastDeviceInterface();
            var devBroadcastDeviceInterfaceBuffer = IntPtr.Zero;

            try
            {
                devBroadcastDeviceInterfaceBuffer = _marshallWrapper.AllocHGlobal(devBroadcastDeviceInterface.Size);
                _marshallWrapper.StructureToPointer(devBroadcastDeviceInterface, devBroadcastDeviceInterfaceBuffer, false);
                var deviceNotificationHandle = _unsafeNativeMethodsWrapper.RegisterDeviceNotification(
                    windowHandleToReceiveNotifications,
                    devBroadcastDeviceInterfaceBuffer,
                    DeviceNotifyWindowHandle);

                return(_processHandleResultFactory.Create(deviceNotificationHandle));
            }
            finally
            {
                if (devBroadcastDeviceInterfaceBuffer != IntPtr.Zero)
                {
                    _marshallWrapper.FreeHGlobal(devBroadcastDeviceInterfaceBuffer);
                }
            }
        }