コード例 #1
0
        private static bool HidUsbRegistryCallBack(IntPtr DeviceInfoSet, int deviceIndex, ref SetupApi.SP_DEVINFO_DATA DeviceInfoData, object classEnumeratorCallbackParam1)
        {
            List<WinHidRegistry> deviceList = (List<WinHidRegistry>)classEnumeratorCallbackParam1;
            Guid hid = GetHidGuid();
            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 hid, null, IntPtr.Zero, SetupApi.DIGCF.PRESENT | SetupApi.DIGCF.DEVICEINTERFACE);
            if (deviceInfo != IntPtr.Zero)
            {
                while ((SetupApi.SetupDiEnumDeviceInterfaces(deviceInfo, null, ref hid, 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)
                    {
                        WinHidRegistry regInfo = new WinHidRegistry();

                        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);

                        // Debug.WriteLine(detailHelper.DevicePath);

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

                        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();
                        }
                        deviceList.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);

        }
コード例 #2
0
        /*
        private static bool WinUsbRegistryCallBack(IntPtr deviceInfoSet,
                                                   int deviceIndex,
                                                   ref SetupApi.SP_DEVINFO_DATA deviceInfoData,
                                                   object classEnumeratorCallbackParam1)
        {

            List<WinUsbRegistry> deviceList = (List<WinUsbRegistry>) classEnumeratorCallbackParam1;

            RegistryValueKind propertyType;
            byte[] propBuffer = new byte[256];
            int requiredSize;
            bool isNew = true;
            bool bSuccess;

            bSuccess = SetupApi.SetupDiGetCustomDeviceProperty(deviceInfoSet,
                                                                    ref deviceInfoData,
                                                                    DEVICE_INTERFACE_GUIDS,
                                                                    SetupApi.DICUSTOMDEVPROP.NONE,
                                                                    out propertyType,
                                                                    propBuffer,
                                                                    propBuffer.Length,
                                                                    out requiredSize);
            if (bSuccess)
            {
                string[] devInterfaceGuids = GetAsStringArray(propBuffer, requiredSize);

                foreach (String devInterfaceGuid in devInterfaceGuids)
                {
                    Guid g = new Guid(devInterfaceGuid);
                    List<string> devicePaths;
                    if (SetupApi.GetDevicePathList(g, out devicePaths))
                    {
                        foreach (string devicePath in devicePaths)
                        {
                            WinUsbRegistry regInfo = new WinUsbRegistry();

                            SetupApi.getSPDRPProperties(deviceInfoSet, ref deviceInfoData, 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, devicePath);

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

                            // Don't add duplicate devices (with the same device path)
                            WinUsbRegistry foundRegistry=null;
                            foreach (WinUsbRegistry usbRegistry in deviceList)
                            {
                                if (usbRegistry.SymbolicName == regInfo.SymbolicName)
                                {
                                    foundRegistry = usbRegistry;
                                    break;
                                }
                            }
                            if (foundRegistry == null)
                                deviceList.Add(regInfo);
                            else
                            {
                                if (isNew)
                                {
                                    deviceList.Remove(foundRegistry);
                                    deviceList.Add(regInfo);
                                }
                                else
                                {

                                    // If the device path already exists, add this compatible guid
                                    // to the foundRegstry guid list.
                                    List<Guid> newGuidList = new List<Guid>(foundRegistry.mDeviceInterfaceGuids);
                                    if (!newGuidList.Contains(g))
                                    {
                                        newGuidList.Add(g);
                                        foundRegistry.mDeviceInterfaceGuids = newGuidList.ToArray();
                                    }
                                }
                            }
                            isNew = false;
                        }
                    }
                }
            }

            return false;
        }
        */
        private static bool WinUsbRegistryCallBack(IntPtr deviceInfoSet,
            int deviceIndex,
            ref SetupApi.SP_DEVINFO_DATA deviceInfoData,
            object classEnumeratorCallbackParam1)
        {
            List<WinUsbRegistry> deviceList = (List<WinUsbRegistry>)classEnumeratorCallbackParam1;

            RegistryValueKind propertyType;
            byte[] propBuffer = new byte[256];
            int requiredSize;
            bool bSuccess;

            bSuccess = SetupApi.SetupDiGetCustomDeviceProperty(deviceInfoSet,
                                                                    ref deviceInfoData,
                                                                    DEVICE_INTERFACE_GUIDS,
                                                                    SetupApi.DICUSTOMDEVPROP.NONE,
                                                                    out propertyType,
                                                                    propBuffer,
                                                                    propBuffer.Length,
                                                                    out requiredSize);
            if (bSuccess)
            {
                string[] devInterfaceGuids = GetAsStringArray(propBuffer, requiredSize);

                foreach (String devInterfaceGuid in devInterfaceGuids)
                {
                    Guid g = new Guid(devInterfaceGuid);
                    List<WinUsbRegistry> tempList;
                    if (GetWinUsbRegistryList(g, out tempList))
                    {
                        foreach (WinUsbRegistry regInfo in tempList)
                        {
                            // Don't add duplicate devices (with the same device path)
                            WinUsbRegistry foundRegistry = null;
                            foreach (WinUsbRegistry usbRegistry in deviceList)
                            {
                                if (usbRegistry.SymbolicName == regInfo.SymbolicName)
                                {
                                    foundRegistry = usbRegistry;
                                    break;
                                }
                            }
                            if (foundRegistry == null)
                                deviceList.Add(regInfo);
                            else
                            {
                                // If the device path already exists, add this compatible guid
                                // to the foundRegstry guid list.
                                List<Guid> newGuidList = new List<Guid>(foundRegistry.mDeviceInterfaceGuids);
                                if (!newGuidList.Contains(g))
                                {
                                    newGuidList.Add(g);
                                    foundRegistry.mDeviceInterfaceGuids = newGuidList.ToArray();
                                }
                            }
                        }
                    }
                }
            }

            return false;
        }