⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usb.cs

📁 USBView的C#源码
💻 CS
📖 第 1 页 / 共 4 页
字号:
            //public byte[] Data;
        }

        //typedef struct _USB_NODE_CONNECTION_NAME {
        //    ULONG  ConnectionIndex;
        //    ULONG  ActualLength;
        //    WCHAR  NodeName[1];
        //} USB_NODE_CONNECTION_NAME, *PUSB_NODE_CONNECTION_NAME;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        struct USB_NODE_CONNECTION_NAME
        {
            public int ConnectionIndex;
            public int ActualLength;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = BUFFER_SIZE)]
            public string NodeName;
        }

        //typedef struct _USB_NODE_CONNECTION_DRIVERKEY_NAME {
        //    ULONG  ConnectionIndex;
        //    ULONG  ActualLength;
        //    WCHAR  DriverKeyName[1];
        //} USB_NODE_CONNECTION_DRIVERKEY_NAME, *PUSB_NODE_CONNECTION_DRIVERKEY_NAME;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        struct USB_NODE_CONNECTION_DRIVERKEY_NAME               // Yes, this is the same as the structure above...
        {
            public int ConnectionIndex;
            public int ActualLength;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = BUFFER_SIZE)]
            public string DriverKeyName;
        }

        // ********************** API Definitions ************************

        //HDEVINFO SetupDiGetClassDevs(
        //  const GUID* ClassGuid,
        //  PCTSTR Enumerator,
        //  HWND hwndParent,
        //  DWORD Flags
        //);
        [DllImport("setupapi.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SetupDiGetClassDevs(               // 1st form using a ClassGUID
            ref Guid ClassGuid,
            int  Enumerator,
            IntPtr hwndParent,
            int Flags
        );
        [DllImport("setupapi.dll", CharSet = CharSet.Auto)]     // 2nd form uses an Enumerator
        static extern IntPtr SetupDiGetClassDevs(
            int ClassGuid,
            string Enumerator,
            IntPtr hwndParent,
            int Flags
        );

        //BOOL SetupDiEnumDeviceInterfaces(
        //  HDEVINFO DeviceInfoSet,
        //  PSP_DEVINFO_DATA DeviceInfoData,
        //  const GUID* InterfaceClassGuid,
        //  DWORD MemberIndex,
        //  PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData
        //);
        [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern bool SetupDiEnumDeviceInterfaces(
            IntPtr DeviceInfoSet,
            IntPtr DeviceInfoData,
            ref Guid InterfaceClassGuid,
            int MemberIndex,
            ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData
        );

        //BOOL SetupDiGetDeviceInterfaceDetail(
        //  HDEVINFO DeviceInfoSet,
        //  PSP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
        //  PSP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData,
        //  DWORD DeviceInterfaceDetailDataSize,
        //  PDWORD RequiredSize,
        //  PSP_DEVINFO_DATA DeviceInfoData
        //);
        [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern bool SetupDiGetDeviceInterfaceDetail(
            IntPtr DeviceInfoSet,
            ref SP_DEVICE_INTERFACE_DATA DeviceInterfaceData,
            ref SP_DEVICE_INTERFACE_DETAIL_DATA DeviceInterfaceDetailData,
            int DeviceInterfaceDetailDataSize,
            ref int RequiredSize,
            ref SP_DEVINFO_DATA DeviceInfoData
        );

        //BOOL SetupDiGetDeviceRegistryProperty(
        //  HDEVINFO DeviceInfoSet,
        //  PSP_DEVINFO_DATA DeviceInfoData,
        //  DWORD Property,
        //  PDWORD PropertyRegDataType,
        //  PBYTE PropertyBuffer,
        //  DWORD PropertyBufferSize,
        //  PDWORD RequiredSize
        //);
        [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern bool SetupDiGetDeviceRegistryProperty(
            IntPtr DeviceInfoSet,
            ref SP_DEVINFO_DATA DeviceInfoData,
            int iProperty,
            ref int PropertyRegDataType,
            IntPtr PropertyBuffer,
            int PropertyBufferSize,
            ref int RequiredSize
        );

        //BOOL SetupDiEnumDeviceInfo(
        //  HDEVINFO DeviceInfoSet,
        //  DWORD MemberIndex,
        //  PSP_DEVINFO_DATA DeviceInfoData
        //);
        [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern bool SetupDiEnumDeviceInfo(
            IntPtr DeviceInfoSet,
            int MemberIndex,
            ref SP_DEVINFO_DATA DeviceInfoData
        );

        //BOOL SetupDiDestroyDeviceInfoList(
        //  HDEVINFO DeviceInfoSet
        //);
        [DllImport("setupapi.dll", SetLastError = true)]
        static extern bool SetupDiDestroyDeviceInfoList(
            IntPtr DeviceInfoSet
        );

        //WINSETUPAPI BOOL WINAPI  SetupDiGetDeviceInstanceId(
        //    IN HDEVINFO  DeviceInfoSet,
        //    IN PSP_DEVINFO_DATA  DeviceInfoData,
        //    OUT PTSTR  DeviceInstanceId,
        //    IN DWORD  DeviceInstanceIdSize,
        //    OUT PDWORD  RequiredSize  OPTIONAL
        //);
        [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern bool SetupDiGetDeviceInstanceId(
            IntPtr DeviceInfoSet,
            ref SP_DEVINFO_DATA DeviceInfoData,
            StringBuilder DeviceInstanceId,
            int DeviceInstanceIdSize,
            out int RequiredSize
        );

        //BOOL DeviceIoControl(
        //  HANDLE hDevice,
        //  DWORD dwIoControlCode,
        //  LPVOID lpInBuffer,
        //  DWORD nInBufferSize,
        //  LPVOID lpOutBuffer,
        //  DWORD nOutBufferSize,
        //  LPDWORD lpBytesReturned,
        //  LPOVERLAPPED lpOverlapped
        //);
        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern bool DeviceIoControl(
            IntPtr hDevice,
            int dwIoControlCode,
            IntPtr lpInBuffer,
            int nInBufferSize,
            IntPtr lpOutBuffer,
            int nOutBufferSize,
            out int lpBytesReturned,
            IntPtr lpOverlapped
        );

        //HANDLE CreateFile(
        //  LPCTSTR lpFileName,
        //  DWORD dwDesiredAccess,
        //  DWORD dwShareMode,
        //  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
        //  DWORD dwCreationDisposition,
        //  DWORD dwFlagsAndAttributes,
        //  HANDLE hTemplateFile
        //);
        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern IntPtr CreateFile(
           string lpFileName,
           int dwDesiredAccess,
           int dwShareMode,
           IntPtr lpSecurityAttributes,
           int dwCreationDisposition,
           int dwFlagsAndAttributes,
           IntPtr hTemplateFile
        );

        //BOOL CloseHandle(
        //  HANDLE hObject
        //);
        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern bool CloseHandle(
            IntPtr hObject
        );
        #endregion

        //
        // Return a list of USB Host Controllers
        //
        static public System.Collections.ObjectModel.ReadOnlyCollection<USBController> GetHostControllers()
        {
            List<USBController> HostList = new List<USBController>();
            Guid 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.ToInt32() != INVALID_HANDLE_VALUE)
            {
                IntPtr ptrBuf = Marshal.AllocHGlobal(BUFFER_SIZE);
                bool Success;
                int i = 0;
                do
                {
                    USBController host = new USBController();
                    host.ControllerIndex = i;

                    // create a Device Interface Data structure
                    SP_DEVICE_INTERFACE_DATA dia = new SP_DEVICE_INTERFACE_DATA();
                    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
                        SP_DEVINFO_DATA da = new SP_DEVINFO_DATA();
                        da.cbSize = Marshal.SizeOf(da);

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

                        // now we can get some more detailed information
                        int nRequiredSize = 0;
                        int nBytes = BUFFER_SIZE;
                        if (SetupDiGetDeviceInterfaceDetail(h, ref dia, ref didd, nBytes, 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 System.Collections.ObjectModel.ReadOnlyCollection<USBController>(HostList);
        }

        //
        // The USB Host Controller Class
        //
        public class USBController
        {
            internal int ControllerIndex;
            internal string ControllerDriverKeyName, ControllerDevicePath, ControllerDeviceDesc;

            // A simple default constructor
            public USBController()
            {
                ControllerIndex = 0;
                ControllerDevicePath = "";
                ControllerDeviceDesc = "";
                ControllerDriverKeyName = "";
            }

            // Return the index of the instance
            public int Index
            {
                get { return ControllerIndex; }
            }

            // Return the Device Path, such as "\\?\pci#ven_10de&dev_005a&subsys_815a1043&rev_a2#3&267a616a&0&58#{3abf6f2d-71c4-462a-8a92-1e6861e6af27}"
            public string DevicePath
            {
                get { return ControllerDevicePath; }
            }

            // The DriverKeyName may be useful as a search key
            public string DriverKeyName
            {
                get { return ControllerDriverKeyName; }
            }

            // Return the Friendly Name, such as "VIA USB Enhanced Host Controller"
            public string Name
            {
                get { return ControllerDeviceDesc; }
            }

            // Return Root Hub for this Controller

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -