Exemplo n.º 1
        /// <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="oInterface">DeviceInterfaceData structure</param>
        /// <returns>The device path or null if there was some problem</returns>
        private static string GetDevicePath(IntPtr hInfoSet, ref SP_DEVICE_INTERFACE_DATA oInterface)
            int nRequiredSize = 0;

            // Get the device interface details
            if (!Setup.SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, IntPtr.Zero, 0, ref nRequiredSize, IntPtr.Zero))
                oDetail.cbSize = 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 (Setup.SetupDiGetDeviceInterfaceDetail(hInfoSet, ref oInterface, ref oDetail, nRequiredSize, ref nRequiredSize, IntPtr.Zero))
                    return oDetail.DevicePath;
            return null;
Exemplo n.º 2
 /// <summary>
 /// Finds a device given its PID and VID
 /// </summary>
 /// <param name="nVid">Vendor id for device (VID)</param>
 /// <param name="nPid">Product id for device (PID)</param>
 /// <param name="oType">Type of device class to create</param>
 /// <returns>A new device class of the given type or null</returns>
 public static HIDDevice FindDevice(int nVid, int nPid, Type oType)
     string strPath = string.Empty;
     string strSearch = string.Format("vid_{0:x4}&pid_{1:x4}", nVid, nPid); // first, build the path search string
     Guid gHid;
     Hid.HidD_GetHidGuid(out gHid);	// next, get the GUID from Windows that it uses to represent the HID USB interface
     IntPtr hInfoSet = Setup.SetupDiGetClassDevs(ref gHid, null, IntPtr.Zero, Setup.DIGCF_DEVICEINTERFACE | Setup.DIGCF_PRESENT);	// this gets a list of all HID devices currently connected to the computer (InfoSet)
         SP_DEVICE_INTERFACE_DATA oInterface = new SP_DEVICE_INTERFACE_DATA();	// build up a device interface data block
         oInterface.cbSize = Marshal.SizeOf(oInterface);
         // Now iterate through the InfoSet memory block assigned within Windows in the call to SetupDiGetClassDevs
         // to get device details for each device connected
         int nIndex = 0;
         while (Setup.SetupDiEnumDeviceInterfaces(hInfoSet, IntPtr.Zero, ref gHid, nIndex, ref oInterface))	// this gets the device interface information for a device at index 'nIndex' in the memory block
             string strDevicePath = GetDevicePath(hInfoSet, ref oInterface);	// get the device path (see helper method 'GetDevicePath')
             if (strDevicePath.IndexOf(strSearch) >= 0)	// do a string search, if we find the VID/PID string then we found our device!
                 HIDDevice oNewDevice = (HIDDevice)Activator.CreateInstance(oType);	// create an instance of the class for this device
                 oNewDevice.Initialise(strDevicePath);	// initialise it with the device path
                 return oNewDevice;	// and return it
             nIndex++;	// if we get here, we didn't find our device. So move on to the next one.
         // Before we go, we have to free up the InfoSet memory reserved by SetupDiGetClassDevs
     return null;	// oops, didn't find our device
Exemplo n.º 3
 public static extern Boolean SetupDiEnumDeviceInterfaces(
     IntPtr DeviceInfoSet, 
     IntPtr DeviceInfoData, 
     ref Guid InterfaceClassGuid, 
     Int32 MemberIndex, 
     ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData);
Exemplo n.º 4
 public static extern Boolean SetupDiGetDeviceInterfaceDetail(IntPtr DeviceInfoSet, 
     ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
     ref SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData, 
     Int32 DeviceInterfaceDetailDataSize, 
     ref Int32 RequiredSize, 
     IntPtr DeviceInfoData);
Exemplo n.º 5
        public static IEnumerable<HidDevice> GetDevices()
            Guid hidGuid = HidDevice.HIDGuid;

            // Get the list of HID devices
            IntPtr deviceInfoSet = new IntPtr();
            deviceInfoSet = setup.SetupDiGetClassDevs(ref hidGuid, IntPtr.Zero, IntPtr.Zero, setup.DIGCF_PRESENT | setup.DIGCF_DEVICEINTERFACE);

            int memberIndex = 0;
            SP_DEVICE_INTERFACE_DATA MyDeviceInterfaceData = new SP_DEVICE_INTERFACE_DATA();
            MyDeviceInterfaceData.cbSize = Marshal.SizeOf(MyDeviceInterfaceData);

            bool success;
            bool lastDevice = false;
            int bufferSize = 0;
            UnmanagedMemory detailDataBuffer;
            List<HidDevice> devicePathNames = new List<HidDevice>();
            bool deviceFound = false;

                success = setup.SetupDiEnumDeviceInterfaces(deviceInfoSet,
                    ref hidGuid,
                    ref MyDeviceInterfaceData);

                if (!success)
                    lastDevice = true;
                    // First call to get size of data buffer
                    success = setup.SetupDiGetDeviceInterfaceDetail(deviceInfoSet,
                        ref MyDeviceInterfaceData,
                        ref bufferSize,

                    // Allocate memory for the SP_DEVICE_INTERFACE_DETAIL_DATA structure using the returned buffer size
                    detailDataBuffer = new UnmanagedMemory(bufferSize);

                    // Store cbSize in the first bytes of the array. The number of bytes varies with 32- and 64-bit systems.

                    Marshal.WriteInt32(detailDataBuffer.MemoryPointer, (IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8);

                    // Call SetupDiGetDeviceInterfaceDetail again.
                    // This time, pass a pointer to DetailDataBuffer
                    // and the returned required buffer size.
                    success = setup.SetupDiGetDeviceInterfaceDetail
                        ref MyDeviceInterfaceData,
                        ref bufferSize,

                    //diDetail.cbSize = (IntPtr.Size == 4) ? (4 + Marshal.SystemDefaultCharSize) : 8;
                    //diDetail.cbSize = Marshal.SizeOf(diDetail);
                    //success = setup.SetupDiGetDeviceInterfaceDetail
                    //    (deviceInfoSet,
                    //    ref MyDeviceInterfaceData,
                    //    ref diDetail,
                    //    diDetail.cbSize,
                    //    ref bufferSize,
                    //    IntPtr.Zero);

                    IntPtr pDevicePathName = detailDataBuffer.MemoryPointer + 4;
                    string devicePathName = Marshal.PtrToStringAuto(pDevicePathName);

                    HidDevice newDevice = new HidDevice(devicePathName);
                    deviceFound = true;
            } while (!lastDevice);

            // Destroy the device Info Set to cleanup memory

            return devicePathNames;