예제 #1
0
 public static extern bool SetupDiGetDeviceInterfaceDetail(
     IntPtr deviceInfoSet,
     ref SpDeviceInterfaceData deviceInterfaceData,
     ref SpDeviceInterfaceDetailData deviceInterfaceDetailData,
     int deviceInterfaceDetailDataSize,
     IntPtr unusedRequiredSize,
     IntPtr unusedDeviceInfoData);             // actually struct but optional
예제 #2
0
        internal static string GetDevicePath(string device)
        {
            Main.SendDebug(string.Format("Getting Drive path for Device: {0}", device));
            var deviceNumber = GetDeviceNumber(device);
            var diskGUID     = new Guid("53F56307-B6BF-11D0-94F2-00A0C91EFB8B");
            var handle       = SetupDiGetClassDevs(ref diskGUID, IntPtr.Zero, IntPtr.Zero, (uint)(DIGCF.DIGCFPresent | DIGCF.DIGCFDeviceinterface));

            try {
                if (handle != new IntPtr(-1))
                {
                    var  success = true;
                    uint i       = 0;
                    while (success)
                    {
                        var devinterfacedata = new SpDeviceInterfaceData();
                        devinterfacedata.cbSize = (uint)Marshal.SizeOf(devinterfacedata);
                        success = SetupDiEnumDeviceInterfaces(handle, IntPtr.Zero, ref diskGUID, i, ref devinterfacedata);
                        if (!success)
                        {
                            continue;
                        }
                        var devinfodata = new SpDevinfoData();
                        devinfodata.cbSize = (uint)Marshal.SizeOf(devinfodata);
                        var devinterfacedetaildata = new SpDeviceInterfaceDetailData {
                            cbSize = IntPtr.Size == 8 ? 8 : (uint)(4 + Marshal.SystemDefaultCharSize)
                        };
                        uint nRequiredSize = 0;
                        if (!SetupDiGetDeviceInterfaceDetail(handle, ref devinterfacedata, ref devinterfacedetaildata, 1000, ref nRequiredSize, ref devinfodata))
                        {
                            SetupDiGetDeviceInterfaceDetail(handle, ref devinterfacedata, ref devinterfacedetaildata, nRequiredSize, ref nRequiredSize, ref devinfodata);
                        }
                        var devnum = GetDeviceNumber(devinterfacedetaildata.DevicePath);
                        if (devnum == deviceNumber)
                        {
                            SetupDiDestroyDeviceInfoList(handle);
                            return(devinterfacedetaildata.DevicePath);
                        }
                        i++;
                    }
                    throw new X360NANDManagerException(X360NANDManagerException.ErrorLevels.NoDeviceFound);
                }
            }
            finally {
                SetupDiDestroyDeviceInfoList(handle);
            }
            throw new Win32Exception(Marshal.GetLastWin32Error());
        }
예제 #3
0
        /// <summary>
        ///     Find a device based upon a Device Number
        /// </summary>
        /// <param name="devNum">Device Number</param>
        /// <param name="deviceGuid">Device GUID</param>
        /// <returns>USB device</returns>
        static UsbDevice FindDeviceNumber(int devNum, string deviceGuid)
        {
            UsbDevice foundDevice = null;
            string    instanceId  = "";

            Guid diskGuid = new Guid(deviceGuid);

            // We start at the "root" of the device tree and look for all
            // devices that match the interface GUID of a disk
            IntPtr h = SetupDiGetClassDevs(ref diskGuid, 0, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

            if (h.ToInt32() != INVALID_HANDLE_VALUE)
            {
                bool success;
                int  i = 0;
                do
                {
                    // create a Device Interface Data structure
                    SpDeviceInterfaceData dia = new SpDeviceInterfaceData();
                    dia.cbSize = Marshal.SizeOf(dia);

                    // start the enumeration
                    success = SetupDiEnumDeviceInterfaces(h, IntPtr.Zero, ref diskGuid, i, ref dia);
                    if (success)
                    {
                        // build a DevInfo Data structure
                        SpDevinfoData da = new SpDevinfoData();
                        da.cbSize = Marshal.SizeOf(da);

                        // build a Device Interface Detail Data structure
                        SpDeviceInterfaceDetailData didd =
                            new SpDeviceInterfaceDetailData {
                            cbSize = 4 + Marshal.SystemDefaultCharSize
                        };                                                                                // trust me :)

                        // now we can get some more detailed information
                        int       nRequiredSize = 0;
                        const int N_BYTES       = BUFFER_SIZE;
                        if (SetupDiGetDeviceInterfaceDetail(h, ref dia, ref didd, N_BYTES, ref nRequiredSize, ref da))
                        {
                            if (GetDeviceNumber(didd.DevicePath) == devNum)
                            {
                                // current InstanceID is at the "USBSTOR" level, so we
                                // need up "move up" one level to get to the "USB" level
                                CM_Get_Parent(out IntPtr ptrPrevious, da.DevInst, 0);

                                // Now we get the InstanceID of the USB level device
                                IntPtr ptrInstanceBuf = Marshal.AllocHGlobal(N_BYTES);
                                CM_Get_Device_ID(ptrPrevious, ptrInstanceBuf, N_BYTES, 0);
                                instanceId = Marshal.PtrToStringAuto(ptrInstanceBuf);

                                Marshal.FreeHGlobal(ptrInstanceBuf);
                                System.Console.WriteLine("InstanceId: {0}", instanceId);
                                //break;
                            }
                        }
                    }
                    i++;
                }while(success);

                SetupDiDestroyDeviceInfoList(h);
            }

            // Did we find an InterfaceID of a USB device?
            if (instanceId?.StartsWith("USB\\", StringComparison.Ordinal) == true)
            {
                foundDevice = FindDeviceByInstanceId(instanceId);
            }
            return(foundDevice);
        }
예제 #4
0
 public static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet, SpDeviceInterfaceData deviceInterfaceData,
                                                           ref SpDeviceInterfaceDetailData deviceInterfaceDetailData, int deviceInterfaceDetailDataSize,
                                                           ref int requiredSize, IntPtr unusedDeviceInfoData);
예제 #5
0
 [DllImport("setupapi.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern Boolean SetupDiGetDeviceInterfaceDetail(IntPtr hDevInfo, ref SpDeviceInterfaceData deviceInterfaceData, ref SpDeviceInterfaceDetailData deviceInterfaceDetailData, UInt32 deviceInterfaceDetailDataSize, ref UInt32 requiredSize, ref SpDevinfoData deviceInfoData);
예제 #6
0
파일: Usb.cs 프로젝트: morefun0302/Aaru
        /// <summary>Return a list of USB Host Controllers</summary>
        /// <returns>List of USB Host Controllers</returns>
        static IEnumerable <UsbController> GetHostControllers()
        {
            List <UsbController> hostList = new List <UsbController>();
            var hostGuid = new Guid(GUID_DEVINTERFACE_HUBCONTROLLER);

            // We start at the "root" of the device tree and look for all
            // devices that match the interface GUID of a Hub Controller
            IntPtr h = SetupDiGetClassDevs(ref hostGuid, 0, IntPtr.Zero, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);

            if (h == INVALID_HANDLE_VALUE)
            {
                return(new ReadOnlyCollection <UsbController>(hostList));
            }

            IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
            bool   success;
            int    i = 0;

            do
            {
                var host = new UsbController
                {
                    ControllerIndex = i
                };

                // create a Device Interface Data structure
                var dia = new SpDeviceInterfaceData();
                dia.cbSize = Marshal.SizeOf(dia);

                // start the enumeration
                success = SetupDiEnumDeviceInterfaces(h, IntPtr.Zero, ref hostGuid, i, ref dia);

                if (success)
                {
                    // build a DevInfo Data structure
                    var da = new SpDevinfoData();
                    da.cbSize = Marshal.SizeOf(da);

                    // build a Device Interface Detail Data structure
                    var didd = new SpDeviceInterfaceDetailData
                    {
                        cbSize = 4 + Marshal.SystemDefaultCharSize
                    };

                    // trust me :)

                    // now we can get some more detailed information
                    int       nRequiredSize = 0;
                    const int N_BYTES       = BUFFER_SIZE;

                    if (SetupDiGetDeviceInterfaceDetail(h, ref dia, ref didd, N_BYTES, ref nRequiredSize, ref da))
                    {
                        host.ControllerDevicePath = didd.DevicePath;

                        // get the Device Description and DriverKeyName
                        int requiredSize = 0;
                        int regType      = REG_SZ;

                        if (SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DEVICEDESC, ref regType, ptrBuf,
                                                             BUFFER_SIZE, ref requiredSize))
                        {
                            host.ControllerDeviceDesc = Marshal.PtrToStringAuto(ptrBuf);
                        }

                        if (SetupDiGetDeviceRegistryProperty(h, ref da, SPDRP_DRIVER, ref regType, ptrBuf, BUFFER_SIZE,
                                                             ref requiredSize))
                        {
                            host.ControllerDriverKeyName = Marshal.PtrToStringAuto(ptrBuf);
                        }
                    }

                    hostList.Add(host);
                }

                i++;
            } while(success);

            Marshal.FreeHGlobal(ptrBuf);
            SetupDiDestroyDeviceInfoList(h);

            // convert it into a Collection
            return(new ReadOnlyCollection <UsbController>(hostList));
        }
예제 #7
0
 internal static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet,
                                                             ref SpDeviceInterfaceData deviceInterfaceData, ref SpDeviceInterfaceDetailData deviceInterfaceDetailData,
                                                             int deviceInterfaceDetailDataSize, ref int requiredSize, ref SpDevinfoData deviceInfoData);
 internal static extern bool SetupDiGetDeviceInterfaceDetail(SafeHandle hDevInfo, ref SpDeviceInterfaceData deviceInterfaceData, ref SpDeviceInterfaceDetailData deviceInterfaceDetailData, [MarshalAs(UnmanagedType.U4)] uint deviceInterfaceDetailDataSize, IntPtr requiredSize, ref SpDeviceInfoData deviceInfoData);
예제 #9
0
 internal static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr deviceInfoSet, ref SpDeviceInterfaceData deviceInterfaceData, ref SpDeviceInterfaceDetailData deviceInterfaceDetailData, int deviceInterfaceDetailDataSize, ref int requiredSize, IntPtr deviceInfoData);
예제 #10
0
 public static extern Boolean SetupDiGetDeviceInterfaceDetail(
    IntPtr hDevInfo,
    ref SpDeviceInterfaceData deviceInterfaceData,
    ref SpDeviceInterfaceDetailData deviceInterfaceDetailData,
    UInt32 deviceInterfaceDetailDataSize,
    out UInt32 requiredSize,
    ref SpDevinfoData deviceInfoData
 );
예제 #11
0
 public static extern bool SetupDiGetDeviceInterfaceDetail(IntPtr hDevInfo, ref SpDeviceInterfaceData deviceInterfaceData, ref SpDeviceInterfaceDetailData deviceInterfaceDetailData, uint deviceInterfaceDetailDataSize, out uint requiredSize, ref SpDeviceInfoData deviceInfoData);
예제 #12
0
 internal static string GetDevicePath(string device)
 {
     Main.SendDebug(string.Format("Getting Drive path for Device: {0}", device));
     var deviceNumber = GetDeviceNumber(device);
     var diskGUID = new Guid("53F56307-B6BF-11D0-94F2-00A0C91EFB8B");
     var handle = SetupDiGetClassDevs(ref diskGUID, IntPtr.Zero, IntPtr.Zero, (uint) (DIGCF.DIGCFPresent | DIGCF.DIGCFDeviceinterface));
     try {
         if(handle != new IntPtr(-1)) {
             var success = true;
             uint i = 0;
             while(success) {
                 var devinterfacedata = new SpDeviceInterfaceData();
                 devinterfacedata.cbSize = (uint) Marshal.SizeOf(devinterfacedata);
                 success = SetupDiEnumDeviceInterfaces(handle, IntPtr.Zero, ref diskGUID, i, ref devinterfacedata);
                 if(!success)
                     continue;
                 var devinfodata = new SpDevinfoData();
                 devinfodata.cbSize = (uint) Marshal.SizeOf(devinfodata);
                 var devinterfacedetaildata = new SpDeviceInterfaceDetailData { cbSize = IntPtr.Size == 8 ? 8 : (uint) (4 + Marshal.SystemDefaultCharSize) };
                 uint nRequiredSize = 0;
                 if(!SetupDiGetDeviceInterfaceDetail(handle, ref devinterfacedata, ref devinterfacedetaildata, 1000, ref nRequiredSize, ref devinfodata))
                     SetupDiGetDeviceInterfaceDetail(handle, ref devinterfacedata, ref devinterfacedetaildata, nRequiredSize, ref nRequiredSize, ref devinfodata);
                 var devnum = GetDeviceNumber(devinterfacedetaildata.DevicePath);
                 if(devnum == deviceNumber) {
                     SetupDiDestroyDeviceInfoList(handle);
                     return devinterfacedetaildata.DevicePath;
                 }
                 i++;
             }
             throw new X360NANDManagerException(X360NANDManagerException.ErrorLevels.NoDeviceFound);
         }
     }
     finally {
         SetupDiDestroyDeviceInfoList(handle);
     }
     throw new Win32Exception(Marshal.GetLastWin32Error());
 }
예제 #13
0
        public static IEnumerable <UsbController> GetControllers()
        {
            var hostGUID = new Guid(UsbConstants.GUID_DEVINTERFACE_HUBCONTROLLER);

            var h = SetupApi.SetupDiGetClassDevs(ref hostGUID, default(int), IntPtr.Zero, UsbConstants.DIGCF_PRESENT | UsbConstants.DIGCF_DEVICEINTERFACE);

            if (h.ToInt32() != UsbConstants.INVALID_HANDLE_VALUE)
            {
                var ptrBuf = Marshal.AllocHGlobal(UsbConstants.BUFFER_SIZE);

                var i       = default(int);
                var success = default(bool);
                do
                {
                    var host = new UsbController(null)
                    {
                        ControllerIndex = i,
                    };

                    var dia = new SpDeviceInterfaceData();
                    dia.cbSize = Marshal.SizeOf(dia);

                    success = SetupApi.SetupDiEnumDeviceInterfaces(h, IntPtr.Zero, ref hostGUID, i, ref dia);
                    if (success)
                    {
                        var da = new SpDevinfoData();
                        da.cbSize = Marshal.SizeOf(da);

                        var didd = new SpDeviceInterfaceDetailData
                        {
                            cbSize = 4 + Marshal.SystemDefaultCharSize,
                        };

                        var nRequiredSize = default(int);
                        var nBytes        = UsbConstants.BUFFER_SIZE;

                        if (SetupApi.SetupDiGetDeviceInterfaceDetail(h, ref dia, ref didd, nBytes, ref nRequiredSize, ref da))
                        {
                            host.ControllerDevicePath = didd.DevicePath;

                            var requiredSize = default(int);
                            var regType      = UsbConstants.REG_SZ;

                            if (SetupApi.SetupDiGetDeviceRegistryProperty(h, ref da, UsbConstants.SPDRP_DEVICEDESC, ref regType, ptrBuf, UsbConstants.BUFFER_SIZE, ref requiredSize))
                            {
                                host.ControllerDeviceDesc = Marshal.PtrToStringAuto(ptrBuf);
                            }
                            if (SetupApi.SetupDiGetDeviceRegistryProperty(h, ref da, UsbConstants.SPDRP_DRIVER, ref regType, ptrBuf, UsbConstants.BUFFER_SIZE, ref requiredSize))
                            {
                                host.ControllerDriverKeyName = Marshal.PtrToStringAuto(ptrBuf);
                            }
                        }

                        yield return(host);
                    }
                    i++;
                }while (success);

                Marshal.FreeHGlobal(ptrBuf);
                SetupApi.SetupDiDestroyDeviceInfoList(h);
            }
        }
예제 #14
0
파일: Hid.cs 프로젝트: Razyre/Logitech-LCD
        public static extern bool SetupDiGetDeviceInterfaceDetail(
			IntPtr deviceInfoSet,
			ref SpDeviceInterfaceData deviceInterfaceData,
			ref SpDeviceInterfaceDetailData deviceInterfaceDetailData,
			int deviceInterfaceDetailDataSize,
			IntPtr unusedRequiredSize,
			IntPtr unusedDeviceInfoData);
예제 #15
0
        public static UsbDevice FindDriveLetter(string driveName)
        {
            UsbDevice foundDevice = null;
            var       instanceID  = string.Empty;

            var devNum = UsbLogic.GetDeviceNumber($@"\\.\{driveName.TrimEnd('\\')}");

            if (devNum < default(int))
            {
                return(foundDevice);
            }

            var diskGUID = new Guid(UsbConstants.GUID_DEVINTERFACE_DISK);

            var h = SetupApi.SetupDiGetClassDevs(ref diskGUID, default(int), IntPtr.Zero, UsbConstants.DIGCF_PRESENT | UsbConstants.DIGCF_DEVICEINTERFACE);

            if (h.ToInt32() != UsbConstants.INVALID_HANDLE_VALUE)
            {
                var i       = default(int);
                var success = true;
                do
                {
                    var dia = new SpDeviceInterfaceData();
                    dia.cbSize = Marshal.SizeOf(dia);

                    success = SetupApi.SetupDiEnumDeviceInterfaces(h, IntPtr.Zero, ref diskGUID, i, ref dia);
                    if (success)
                    {
                        var da = new SpDevinfoData();
                        da.cbSize = Marshal.SizeOf(da);

                        var didd = new SpDeviceInterfaceDetailData
                        {
                            cbSize = 4 + Marshal.SystemDefaultCharSize,
                        };

                        var nRequiredSize = default(int);
                        int nBytes        = UsbConstants.BUFFER_SIZE;
                        if (SetupApi.SetupDiGetDeviceInterfaceDetail(h, ref dia, ref didd, nBytes, ref nRequiredSize, ref da))
                        {
                            if (UsbLogic.GetDeviceNumber(didd.DevicePath) == devNum)
                            {
                                SetupApi.CM_Get_Parent(out IntPtr ptrPrevious, da.DevInst, default(int));

                                var ptrInstanceBuf = Marshal.AllocHGlobal(nBytes);
                                SetupApi.CM_Get_Device_ID(ptrPrevious, ptrInstanceBuf, nBytes, default(int));
                                instanceID = Marshal.PtrToStringAuto(ptrInstanceBuf);

                                Marshal.FreeHGlobal(ptrInstanceBuf);
                                break;
                            }
                        }
                    }
                    i++;
                } while (success);
                SetupApi.SetupDiDestroyDeviceInfoList(h);
            }

            if (instanceID.StartsWith("USB\\"))
            {
                foundDevice = UsbLogic.FindDeviceByInstanceID(instanceID, driveName);
            }

            return(foundDevice);
        }
 internal static extern bool SetupDiGetDeviceInterfaceDetail(SafeHandle hDevInfo, ref SpDeviceInterfaceData deviceInterfaceData, ref SpDeviceInterfaceDetailData deviceInterfaceDetailData, [MarshalAs(UnmanagedType.U4)] uint deviceInterfaceDetailDataSize, IntPtr requiredSize, ref SpDeviceInfoData deviceInfoData);