Beispiel #1
0
 public static extern Boolean SetupDiEnumDeviceInterfaces(
     IntPtr hDevInfo,
     IntPtr devInfo,
     ref Guid interfaceClassGuid,
     int memberIndex,
     ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData
     );
Beispiel #2
0
 public static extern bool SetupDiGetDeviceInterfaceDetail(
     IntPtr hDevInfo,
     ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData,
     IntPtr deviceInterfaceDetailData,
     int deviceInterfaceDetailDataSize,
     out int requiredSize,
     ref SP_DEVINFO_DATA deviceInfoData
     );
Beispiel #3
0
        public IList<string> FindDevices()
        {
            IList<string> deviceNames = new List<string>();

            Guid hidGuid = new Guid();

            Hid.HidD_GetHidGuid(ref hidGuid);

            uint flags = (uint) (DiGetClassFlags.DIGCF_PRESENT | DiGetClassFlags.DIGCF_DEVICEINTERFACE);
            IntPtr pnpHandle = IntPtr.Zero;
            try
            {
                pnpHandle = SetupApi.SetupDiGetClassDevs(ref hidGuid, IntPtr.Zero, IntPtr.Zero, flags);

                SP_DEVICE_INTERFACE_DATA data = new SP_DEVICE_INTERFACE_DATA();
                data.cbSize = Marshal.SizeOf(data);
                int devIndex = 0;
                bool success = false;
                do
                {
                    success = SetupApi.SetupDiEnumDeviceInterfaces(pnpHandle, IntPtr.Zero, ref hidGuid, devIndex,
                                                                   ref data);
                    if (success)
                    {
                        SP_DEVINFO_DATA devData = new SP_DEVINFO_DATA();
                        devData.cbSize = Marshal.SizeOf(devData);

                        int bytesReturned;

                        SetupApi.SetupDiGetDeviceInterfaceDetail(pnpHandle, ref data, IntPtr.Zero, 0, out bytesReturned,
                                                                 ref devData);

                        if (bytesReturned != 0 && Marshal.GetLastWin32Error() == ErrorCodes.ERROR_INSUFFICIENT_BUFFER)
                        {
                            unsafe
                            {
                                IntPtr deviceInterfaceDetailData = Marshal.AllocHGlobal(bytesReturned);
                                SP_DEVICE_INTERFACE_DETAIL_DATA* interfaceData =
                                    (SP_DEVICE_INTERFACE_DETAIL_DATA*) deviceInterfaceDetailData.ToPointer();
                                interfaceData->cbSize = sizeof (SP_DEVICE_INTERFACE_DETAIL_DATA);

                                if (SetupApi.SetupDiGetDeviceInterfaceDetail(pnpHandle, ref data,
                                                                             deviceInterfaceDetailData,
                                                                             bytesReturned,
                                                                             out bytesReturned, ref devData))
                                {
                                    IntPtr devicePathTchar = IntPtr.Add(deviceInterfaceDetailData, sizeof (int));
                                    string devicePath = Marshal.PtrToStringAuto(devicePathTchar);
                                    deviceNames.Add(devicePath);
                                }

                                Marshal.FreeHGlobal(deviceInterfaceDetailData);
                            }
                        }
                    }
                    ++devIndex;
                } while (success);

                return deviceNames;
            }
            finally
            {
                if (pnpHandle != IntPtr.Zero)
                {
                    SetupApi.SetupDiDestroyDeviceInfoList(pnpHandle);
                }
            }
        }