Beispiel #1
0
        /// <summary>
        /// Gets a list of <see cref="WinUsbRegistry"/> classes for the specified interface guid.
        /// </summary>
        /// <param name="deviceInterfaceGuid">The DeviceInterfaceGUID to search for.</param>
        /// <param name="deviceRegistryList">A list of device paths associated with the <paramref name="deviceInterfaceGuid"/>.</param>
        /// <returns>True of one or more device paths was found.</returns>
        /// <remarks>
        /// Each <see cref="WinUsbRegistry"/> in the <paramref name="deviceRegistryList"/> represents a seperate WinUSB device (interface).
        /// </remarks>
        public static bool GetWinUsbRegistryList(Guid deviceInterfaceGuid, out List <WinUsbRegistry> deviceRegistryList)
        {
            deviceRegistryList = new List <WinUsbRegistry>();

            int devicePathIndex = 0;

            SetupApi.SP_DEVICE_INTERFACE_DATA    interfaceData = SetupApi.SP_DEVICE_INTERFACE_DATA.Empty;
            SetupApi.DeviceInterfaceDetailHelper detailHelper;

            SetupApi.SP_DEVINFO_DATA devInfoData = SetupApi.SP_DEVINFO_DATA.Empty;

            // [1]
            IntPtr deviceInfo = SetupApi.SetupDiGetClassDevs(ref deviceInterfaceGuid, null, IntPtr.Zero, SetupApi.DICFG.PRESENT | SetupApi.DICFG.DEVICEINTERFACE);

            if (deviceInfo != IntPtr.Zero)
            {
                while ((SetupApi.SetupDiEnumDeviceInterfaces(deviceInfo, null, ref deviceInterfaceGuid, devicePathIndex, ref interfaceData)))
                {
                    int length = 1024;
                    detailHelper = new SetupApi.DeviceInterfaceDetailHelper(length);
                    bool bResult = SetupApi.SetupDiGetDeviceInterfaceDetail(deviceInfo, ref interfaceData, detailHelper.Handle, length, out length, ref devInfoData);
                    if (bResult)
                    {
                        WinUsbRegistry regInfo = new WinUsbRegistry();

                        SetupApi.getSPDRPProperties(deviceInfo, ref devInfoData, regInfo.mDeviceProperties);

                        // Use the actual winusb device path for SYMBOLIC_NAME_KEY. This will be used to open the device.
                        regInfo.mDeviceProperties.Add(SYMBOLIC_NAME_KEY, detailHelper.DevicePath);
#if VERY_DEBUG
                        Debug.WriteLine(detailHelper.DevicePath);
#endif

                        regInfo.mDeviceInterfaceGuids = new Guid[] { deviceInterfaceGuid };

                        StringBuilder sbDeviceID = new StringBuilder(1024);
                        if (SetupApi.CM_Get_Device_ID(devInfoData.DevInst, sbDeviceID, sbDeviceID.Capacity, 0) == SetupApi.CR.SUCCESS)
                        {
                            regInfo.mDeviceProperties[DEVICE_ID_KEY] = sbDeviceID.ToString();
                        }
                        deviceRegistryList.Add(regInfo);
                    }

                    devicePathIndex++;
                }
            }
            if (devicePathIndex == 0)
            {
                UsbError.Error(ErrorCode.Win32Error, Marshal.GetLastWin32Error(), "GetDevicePathList", typeof(SetupApi));
            }

            if (deviceInfo != IntPtr.Zero)
            {
                SetupApi.SetupDiDestroyDeviceInfoList(deviceInfo);
            }

            return(devicePathIndex > 0);
        }
Beispiel #2
0
        private static bool BuildMasterCallback(IntPtr deviceInfoSet, int deviceindex, ref SetupApi.SP_DEVINFO_DATA deviceInfoData, object userData)
        {
            MasterList    deviceList = userData as MasterList;
            MasterItem    deviceItem = new MasterItem();
            StringBuilder sb         = new StringBuilder(256);

            if (SetupApi.CM_Get_Device_ID(deviceInfoData.DevInst, sb, sb.Capacity, 0) != SetupApi.CR.SUCCESS)
            {
                return(false);
            }

            deviceItem.Add(DEVICE_ID_KEY, sb.ToString());
            deviceList.Add(deviceItem);


            RegistryValueKind propertyType;

            byte[] propBuffer = new byte[256];
            int    requiredSize;
            bool   bSuccess = SetupApi.SetupDiGetCustomDeviceProperty(deviceInfoSet,
                                                                      ref deviceInfoData,
                                                                      UsbRegistry.DEVICE_INTERFACE_GUIDS,
                                                                      SetupApi.DICUSTOMDEVPROP.NONE,
                                                                      out propertyType,
                                                                      propBuffer,
                                                                      propBuffer.Length,
                                                                      out requiredSize);

            if (bSuccess)
            {
                string[] devInterfaceGuids = UsbRegistry.GetAsStringArray(propBuffer, requiredSize);

                deviceItem.Add(UsbRegistry.DEVICE_INTERFACE_GUIDS, devInterfaceGuids);
                foreach (string s in devInterfaceGuids)
                {
                    Guid          g = new Guid(s);
                    List <string> devicePathList;
                    if (WinUsb.WinUsbRegistry.GetDevicePathList(g, out devicePathList))
                    {
                        deviceItem.DevicePaths.Add(g, devicePathList);
                    }
                }
            }
            else
            {
                bSuccess = SetupApi.SetupDiGetCustomDeviceProperty(deviceInfoSet,
                                                                   ref deviceInfoData,
                                                                   UsbRegistry.LIBUSB_INTERFACE_GUIDS,
                                                                   SetupApi.DICUSTOMDEVPROP.NONE,
                                                                   out propertyType,
                                                                   propBuffer,
                                                                   propBuffer.Length,
                                                                   out requiredSize);
                if (bSuccess)
                {
                    string[] devInterfaceGuids = UsbRegistry.GetAsStringArray(propBuffer, requiredSize);

                    deviceItem.Add(UsbRegistry.LIBUSB_INTERFACE_GUIDS, devInterfaceGuids);
                }
            }

            bSuccess =
                SetupApi.SetupDiGetCustomDeviceProperty(deviceInfoSet,
                                                        ref deviceInfoData,
                                                        UsbRegistry.SYMBOLIC_NAME_KEY,
                                                        SetupApi.DICUSTOMDEVPROP.NONE,
                                                        out propertyType,
                                                        propBuffer,
                                                        propBuffer.Length,
                                                        out requiredSize);
            if (bSuccess)
            {
                string symbolicName = UsbRegistry.GetAsString(propBuffer, requiredSize);
                deviceItem.Add(UsbRegistry.SYMBOLIC_NAME_KEY, symbolicName);
            }
            SetupApi.getSPDRPProperties(deviceInfoSet, ref deviceInfoData, deviceItem);

            return(false);
        }