示例#1
0
        private static Tuple <bool, Wrapper.SpDevInfoData> GetDeviceInfoData(DeviceInfoSet deviceInfoSet, int memberIndex)
        {
            var  deviceInfoData = new Wrapper.SpDevInfoData(true);
            bool infoAvailable  = Wrapper.SetupDiEnumDeviceInfo(deviceInfoSet.Handle.DangerousGetHandle(), memberIndex, ref deviceInfoData);

            return(new Tuple <bool, Wrapper.SpDevInfoData>(infoAvailable, deviceInfoData));
        }
示例#2
0
        private static IEnumerable <Tuple <string, DeviceHeritage> > GetKeyboardBasicData()
        {
            var interfaceGuid = new Guid(GuidInterfaceKeyboard);

            using (var deviceInfoSet = new DeviceInfoSet(interfaceGuid, Wrapper.GetDeviceInfoSetFlags.Present | Wrapper.GetDeviceInfoSetFlags.DeviceInterface))
            {
                foreach (var interfaceData in EnumerateDeviceInterfaces(deviceInfoSet, interfaceGuid))
                {
                    var deviceFileInstancePath = GetDeviceFileInstancePath(deviceInfoSet, interfaceData.Item2);

                    var deviceData = GetDeviceInfoData(deviceInfoSet, interfaceData.Item1);

                    var deviceInstancePath = deviceData.Item1
                        ? GetPropertyString(deviceInfoSet, deviceData.Item2, Wrapper.DevPropKey.DEVPKEY_Device_InstanceId)
                        : string.Empty;

                    var parentInstancePath = deviceData.Item1
                        ? GetPropertyString(deviceInfoSet, deviceData.Item2, Wrapper.DevPropKey.DEVPKEY_Device_Parent)
                        : string.Empty;

                    var grandParentInstancePath = deviceData.Item1
                        ? GetGrandParent(parentInstancePath)
                        : string.Empty;

                    var deviceHeritage = new DeviceHeritage
                    {
                        DeviceId            = deviceInstancePath,
                        ParentDeviceId      = parentInstancePath,
                        GrandParentDeviceId = grandParentInstancePath
                    };

                    yield return(new Tuple <string, DeviceHeritage>(deviceFileInstancePath, deviceHeritage));
                }
            }
        }
示例#3
0
        private static int GetInterfaceDetailBufferSize(DeviceInfoSet deviceInfo, Wrapper.SpDeviceInterfaceData deviceInterfaceData)
        {
            int  bufferSize   = 0;
            bool isSuccessful = Wrapper.SetupDiGetDeviceInterfaceDetail(deviceInfo.Handle.DangerousGetHandle(), ref deviceInterfaceData, default(IntPtr), 0, ref bufferSize, default(IntPtr));
            var  errorCode    = Marshal.GetLastWin32Error();

            if (!isSuccessful && errorCode != Wrapper.Win32ErrorInsufficientBuffer)
            {
                throw new Win32Exception(errorCode);
            }

            return(bufferSize);
        }
示例#4
0
        private static int GetPropertyBufferSize(DeviceInfoSet deviceInfoSet, Wrapper.SpDevInfoData deviceInfoData, Wrapper.DevPropKey key)
        {
            var parentInstanceIdProperty = key;

            bool isSuccessful = Wrapper.SetupDiGetDeviceProperty(deviceInfoSet.Handle.DangerousGetHandle(),
                                                                 ref deviceInfoData,
                                                                 ref parentInstanceIdProperty,
                                                                 out int _,
                                                                 IntPtr.Zero,
                                                                 0,
                                                                 out int size);
            int errorCode = Marshal.GetLastWin32Error();

            if (!isSuccessful && errorCode != Wrapper.Win32ErrorInsufficientBuffer)
            {
                throw new Win32Exception(errorCode);
            }

            return(size);
        }
示例#5
0
        private static string GetDeviceFileInstancePath(DeviceInfoSet deviceInfo, Wrapper.SpDeviceInterfaceData deviceInterfaceData)
        {
            int bufferSize = GetInterfaceDetailBufferSize(deviceInfo, deviceInterfaceData);

            using (var interfaceDetailBuffer = new DeviceInterfaceDetailData(bufferSize))
            {
                bool isSuccessful = Wrapper.SetupDiGetDeviceInterfaceDetail(deviceInfo.Handle.DangerousGetHandle(),
                                                                            ref deviceInterfaceData,
                                                                            interfaceDetailBuffer.Handle.DangerousGetHandle(),
                                                                            bufferSize,
                                                                            ref bufferSize,
                                                                            default(IntPtr));
                var errorCode = Marshal.GetLastWin32Error();
                if (!isSuccessful)
                {
                    throw new Win32Exception(errorCode);
                }

                return(interfaceDetailBuffer.GetPathName());
            }
        }
示例#6
0
        private static string GetGrandParent(string parentDeviceInstancePath)
        {
            string grandParent = string.Empty;
            var    guid        = new Guid(GuidInterfaceUsbDevice);

            using (var deviceInfoSet = new DeviceInfoSet(guid, Wrapper.GetDeviceInfoSetFlags.DeviceInterface | Wrapper.GetDeviceInfoSetFlags.Present))
            {
                foreach (var usbDevice in EnumerateDeviceInfoData(deviceInfoSet))
                {
                    var childrenDevices = GetPropertyArray(deviceInfoSet, usbDevice.Item2, Wrapper.DevPropKey.DEVPKEY_Device_Children);

                    if (!childrenDevices.Contains(parentDeviceInstancePath))
                    {
                        continue;
                    }

                    grandParent = GetPropertyString(deviceInfoSet, usbDevice.Item2, Wrapper.DevPropKey.DEVPKEY_Device_InstanceId);
                    break;
                }
            }
            return(grandParent);
        }
示例#7
0
        private static string GetPropertyString(DeviceInfoSet deviceInfoSet, Wrapper.SpDevInfoData deviceInfoData, Wrapper.DevPropKey key)
        {
            var propertySize = GetPropertyBufferSize(deviceInfoSet, deviceInfoData, key);
            var devPropKey   = key;

            using (var buffer = new DeviceIdBuffer(propertySize))
            {
                bool isSuccessful = Wrapper.SetupDiGetDeviceProperty(deviceInfoSet.Handle.DangerousGetHandle(),
                                                                     ref deviceInfoData,
                                                                     ref devPropKey,
                                                                     out int _,
                                                                     buffer.Handle.DangerousGetHandle(),
                                                                     propertySize,
                                                                     out int _);
                int errorCode = Marshal.GetLastWin32Error();
                if (!isSuccessful)
                {
                    throw new Win32Exception(errorCode);
                }

                return(Marshal.PtrToStringAuto(buffer.Handle.DangerousGetHandle()));
            }
        }
示例#8
0
        private static IEnumerable <string> GetPropertyArray(DeviceInfoSet deviceInfoSet, Wrapper.SpDevInfoData deviceInfoData, Wrapper.DevPropKey key)
        {
            var result = new string[0];

            try
            {
                var propertySize = GetPropertyBufferSize(deviceInfoSet, deviceInfoData, key);
                var devPropKey   = key;

                using (var buffer = new DeviceIdBuffer(propertySize))
                {
                    bool isSuccessful = Wrapper.SetupDiGetDeviceProperty(deviceInfoSet.Handle.DangerousGetHandle(),
                                                                         ref deviceInfoData,
                                                                         ref devPropKey,
                                                                         out int _,
                                                                         buffer.Handle.DangerousGetHandle(),
                                                                         propertySize,
                                                                         out int _);
                    int errorCode = Marshal.GetLastWin32Error();
                    if (!isSuccessful)
                    {
                        throw new Win32Exception(errorCode);
                    }

                    var data = new byte[propertySize];
                    Marshal.Copy(buffer.Handle.DangerousGetHandle(), data, 0, propertySize);

                    result = System.Text.Encoding.Unicode.GetString(data).TrimEnd('\0').Split('\0');
                }
            }
            catch (Exception e)
            {
                Debug.Print(e.Message);
            }
            return(result);
        }
示例#9
0
        private static IEnumerable <Tuple <int, Wrapper.SpDevInfoData> > EnumerateDeviceInfoData(DeviceInfoSet deviceInfoSet)
        {
            bool isEnumerationCompleted;
            var  memberIndex = -1;

            do
            {
                var deviceInfoData = new Wrapper.SpDevInfoData(true);

                memberIndex++;

                bool infoAvailable = Wrapper.SetupDiEnumDeviceInfo(deviceInfoSet.Handle.DangerousGetHandle(), memberIndex, ref deviceInfoData);
                isEnumerationCompleted = Marshal.GetLastWin32Error().Equals(Wrapper.Win32ErrorNoMoreItems);

                if (!infoAvailable)
                {
                    continue;
                }

                yield return(new Tuple <int, Wrapper.SpDevInfoData>(memberIndex, deviceInfoData));
            } while (!isEnumerationCompleted);
        }