Exemple #1
0
        public DeviceInterfaceDetail(IntPtr handle, DeviceInterfaceData did)
        {
            // build a DevInfo Data structure
            _deviceInfoData        = new DevInfoData();
            _deviceInfoData.cbSize = (uint)Marshal.SizeOf(_deviceInfoData);

            // build a Device Interface Detail Data structure
            _didd = new DeviceInterfaceDetailData();
            if (IntPtr.Size == 8) // for 64 bit operating systems
            {
                _didd.cbSize = 8;
            }
            else
            {
                _didd.cbSize = 4 + Marshal.SystemDefaultCharSize; // for 32 bit systems
            }
            // now we can get some more detailed information
            uint       nRequiredSize;
            const uint nBytes = DeviceInterfaceDetailData.BufferSize;

            if (!SetupDiGetDeviceInterfaceDetail(handle, ref did, ref _didd, nBytes, out nRequiredSize, ref _deviceInfoData))
            {
                var lastError = Marshal.GetLastWin32Error();
                throw new Win32Exception(lastError, "SetupDiGetDeviceInterfaceDetail failed");
            }
        }
Exemple #2
0
 public static extern Boolean SetupDiEnumDeviceInterfaces(
     IntPtr hDevInfo,
     IntPtr devInfo,
     ref Guid interfaceClassGuid,
     UInt32 memberIndex,
     ref DeviceInterfaceData deviceInterfaceData
     );
 public static extern Boolean SetupDiEnumDeviceInterfaces(
     IntPtr hDevInfo,
     IntPtr devInfo,
     ref Guid interfaceClassGuid,
     UInt32 memberIndex,
     ref DeviceInterfaceData deviceInterfaceData
     );
Exemple #4
0
 public static extern Boolean SetupDiGetDeviceInterfaceDetail(
     IntPtr hDevInfo,
     ref DeviceInterfaceData deviceInterfaceData,
     ref DeviceInterfaceDetailData deviceInterfaceDetailData,
     UInt32 deviceInterfaceDetailDataSize,
     out UInt32 requiredSize,
     ref DevInfoData deviceInfoData
     );
 public static extern Boolean SetupDiGetDeviceInterfaceDetail(
     IntPtr hDevInfo,
     ref DeviceInterfaceData deviceInterfaceData,
     ref DeviceInterfaceDetailData deviceInterfaceDetailData,
     UInt32 deviceInterfaceDetailDataSize,
     out UInt32 requiredSize,
     ref DevInfoData deviceInfoData
     );
 public DeviceInterface(IntPtr handle, DeviceInterfaceData did)
 {
     _handle = handle;
     _did = did;
     try
     {
         // On Windows 8 we see exceptions with getting the device info when a bluetooth device is connected.
         _details = new DeviceInterfaceDetail(_handle, _did);
         _isValidUsbDevice = true;
     }
     catch (Exception)
     {
         _isValidUsbDevice = false;
     }
 }
Exemple #7
0
 public DeviceInterface(IntPtr handle, DeviceInterfaceData did)
 {
     _handle = handle;
     _did    = did;
     try
     {
         // On Windows 8 we see exceptions with getting the device info when a bluetooth device is connected.
         _details          = new DeviceInterfaceDetail(_handle, _did);
         _isValidUsbDevice = true;
     }
     catch (Exception)
     {
         _isValidUsbDevice = false;
     }
 }
        public IEnumerable<DeviceInterface> GetDeviceInterfaces(Guid interfaceClassGuid)
        {
            bool success = true;
            int i = 0;
            while (success)
            {
                // create a Device Interface Data structure
                DeviceInterfaceData dia = new DeviceInterfaceData();
                dia.cbSize = Marshal.SizeOf(dia);

                // start the enumeration
                success = SetupDiEnumDeviceInterfaces(_handle, IntPtr.Zero, ref interfaceClassGuid, (uint)i, ref dia);
                if (success)
                {
                    yield return new DeviceInterface(_handle, dia);
                }
                i++;
            }
        }
Exemple #9
0
        public IEnumerable <DeviceInterface> GetDeviceInterfaces(Guid interfaceClassGuid)
        {
            bool success = true;
            int  i       = 0;

            while (success)
            {
                // create a Device Interface Data structure
                DeviceInterfaceData dia = new DeviceInterfaceData();
                dia.cbSize = Marshal.SizeOf(dia);

                // start the enumeration
                success = SetupDiEnumDeviceInterfaces(_handle, IntPtr.Zero, ref interfaceClassGuid, (uint)i, ref dia);
                if (success)
                {
                    yield return(new DeviceInterface(_handle, dia));
                }
                i++;
            }
        }
        public DeviceInterfaceDetail(IntPtr handle, DeviceInterfaceData did)
        {
            // build a DevInfo Data structure
            _deviceInfoData = new DevInfoData();
            _deviceInfoData.cbSize = (uint)Marshal.SizeOf(_deviceInfoData);

            // build a Device Interface Detail Data structure
            _didd = new DeviceInterfaceDetailData();
            if (IntPtr.Size == 8) // for 64 bit operating systems
                _didd.cbSize = 8;
            else
                _didd.cbSize = 4 + Marshal.SystemDefaultCharSize; // for 32 bit systems

            // now we can get some more detailed information
            uint nRequiredSize;
            const uint nBytes = DeviceInterfaceDetailData.BufferSize;
            if (!SetupDiGetDeviceInterfaceDetail(handle, ref did, ref _didd, nBytes, out nRequiredSize, ref _deviceInfoData))
            {
                var lastError = Marshal.GetLastWin32Error();
                throw new Win32Exception(lastError, "SetupDiGetDeviceInterfaceDetail failed");
            }
        }
 /// <summary>
 /// Helper method to return the device path given a DeviceInterfaceData structure and an InfoSet handle.
 /// Used in 'FindDevice' so check that method out to see how to get an InfoSet handle and a DeviceInterfaceData.
 /// </summary>
 /// <param name="hInfoSet">Handle to the InfoSet</param>
 /// <param name="interfaceData">DeviceInterfaceData structure</param>
 /// <returns>The device path or null if there was some problem</returns>
 private static string GetDevicePath(IntPtr hInfoSet, ref DeviceInterfaceData oInterface)
 {
     uint nRequiredSize = 0;
     // Get the device interface details
     if (!SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, IntPtr.Zero, 0, ref nRequiredSize, IntPtr.Zero))
     {
         DeviceInterfaceDetailData oDetail = new DeviceInterfaceDetailData();
         oDetail.Size = 5;	// hardcoded to 5! Sorry, but this works and trying more future proof versions by setting the size to the struct sizeof failed miserably. If you manage to sort it, mail me! Thx
         if (SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref oDetail, nRequiredSize, ref nRequiredSize, IntPtr.Zero))
         {
             return oDetail.DevicePath;
         }
     }
     return null;
 }
 /// <summary>
 /// Finds a device given its PID and VID
 /// </summary>
 /// <param name="vendorId">Vendor id for device (VID)</param>
 /// <param name="productId">Product id for device (PID)</param>
 /// <param name="creatorDelegate">A delgate that will be invoked to create the actual device class.</param>
 /// <returns>A new device class of the given type or null</returns>
 public static HidDevice FindDevice(int vendorId, int productId, HidDeviceCreatorDelegate creatorDelegate)
 {
     string searchString = string.Format("vid_{0:x4}&pid_{1:x4}", vendorId, productId); // first, build the path search string
     Guid gHid;
     HidD_GetHidGuid(out gHid);	// next, get the GUID from Windows that it uses to represent the HID USB interface
     IntPtr hInfoSet = SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);	// this gets a list of all HID devices currently connected to the computer (InfoSet)
     try
     {
         DeviceInterfaceData interfaceData = new DeviceInterfaceData();	// build up a device interface data block
         interfaceData.Size = Marshal.SizeOf(interfaceData);
         // Now iterate through the InfoSet memory block assigned within Windows in the call to SetupDiGetClassDevs
         // to get device details for each device connected
         int index = 0;
         while (SetupDiEnumDeviceInterfaces(hInfoSet, 0, ref gHid, (uint)index, ref interfaceData))	// this gets the device interface information for a device at index 'nIndex' in the memory block
         {
             string devicePath = GetDevicePath(hInfoSet, ref interfaceData);	// get the device path (see helper method 'GetDevicePath')
             if (devicePath.IndexOf(searchString) >= 0)	// do a string search, if we find the VID/PID string then we found our device!
             {
                 HidDevice newDevice = creatorDelegate(devicePath);	// create an instance of the class for this device
                 return newDevice;	// and return it
             }
             index++;	// if we get here, we didn't find our device. So move on to the next one.
         }
     }
     finally
     {
         // Before we go, we have to free up the InfoSet memory reserved by SetupDiGetClassDevs
         SetupDiDestroyDeviceInfoList(hInfoSet);
     }
     return null;	// oops, didn't find our device
 }
 protected static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr lpDeviceInfoSet, ref DeviceInterfaceData oInterfaceData, ref DeviceInterfaceDetailData oDetailData, uint nDeviceInterfaceDetailDataSize, ref uint nRequiredSize, IntPtr lpDeviceInfoData);
 protected static extern bool SetupDiEnumDeviceInterfaces(IntPtr lpDeviceInfoSet, uint nDeviceInfoData, ref Guid gClass, uint nIndex, ref DeviceInterfaceData oInterfaceData);