Exemple #1
0
        private string CheckDevice(InteropDevices.SP_DEVICE_INTERFACE_DATA deviceInterfaceData, IntPtr deviceInfoSet, out IntPtr deviceDetailData, string vendorId, string productId)
        {
            var requiredBufferSize = -1;

            // get the required size for the buffer
            InteropSetupAPI.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref deviceInterfaceData, IntPtr.Zero,
                                                            0, ref requiredBufferSize, IntPtr.Zero);

            // allocate memory using the required size
            deviceDetailData = Marshal.AllocHGlobal(requiredBufferSize);

            // store cbSize in the first bytes of the array, with size based on 32 or 64 bit system
            Marshal.WriteInt32(deviceDetailData, (IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8);

            // now we've allocated the buffer, pass it in to retrieve the information
            if (InteropSetupAPI.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref deviceInterfaceData,
                                                                deviceDetailData, requiredBufferSize, ref requiredBufferSize,
                                                                IntPtr.Zero))
            {
                // ignore cbSize
                var pDevicePath = new IntPtr(deviceDetailData.ToInt32() + 4);

                // retrieve the path name
                var devicePathName = Marshal.PtrToStringAuto(pDevicePath);

                // append underscore to we match the correct thing
                var vendorIdString = ("vid_" + vendorId).ToLower();
                var productIdString = ("pid_" + productId).ToLower();
                if (devicePathName != null &&
                    devicePathName.Contains(vendorIdString) &&
                    devicePathName.Contains(productIdString))
                {
                    return devicePathName;
                }
            }

            // not our guy, bring out the next
            return null;
        }
 public static extern Boolean SetupDiGetDeviceInterfaceDetail(IntPtr DeviceInfoSet, ref InteropDevices.SP_DEVICE_INTERFACE_DATA DeviceInterfaceData, IntPtr DeviceInterfaceDetailData, Int32 DeviceInterfaceDetailDataSize, ref Int32 RequiredSize, IntPtr DeviceInfoData);
 public static extern Boolean SetupDiEnumDeviceInterfaces(IntPtr DeviceInfoSet, IntPtr DeviceInfoData, ref System.Guid InterfaceClassGuid, Int32 MemberIndex, ref InteropDevices.SP_DEVICE_INTERFACE_DATA DeviceInterfaceData);