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

📄 usb.cs

📁 USBView的C#源码
💻 CS
📖 第 1 页 / 共 4 页
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Diagnostics;

namespace USBLib
{

    public partial class USB
    {
    #region "API Region"

        // ********************** Constants ************************

        const int GENERIC_WRITE = 0x40000000;
        const int FILE_SHARE_READ = 0x1;
        const int FILE_SHARE_WRITE = 0x2;
        const int OPEN_EXISTING = 0x3;
        const int INVALID_HANDLE_VALUE = -1;

        const int IOCTL_GET_HCD_DRIVERKEY_NAME = 0x220424;
        const int IOCTL_USB_GET_ROOT_HUB_NAME = 0x220408;
        const int IOCTL_USB_GET_NODE_INFORMATION = 0x220408;    // same as above... strange, eh?
        const int IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX = 0x220448;
        const int IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION = 0x220410;
        const int IOCTL_USB_GET_NODE_CONNECTION_NAME = 0x220414;
        const int IOCTL_USB_GET_NODE_CONNECTION_DRIVERKEY_NAME = 0x220420;

        const int USB_DEVICE_DESCRIPTOR_TYPE = 0x1;
        const int USB_STRING_DESCRIPTOR_TYPE = 0x3;

        const int BUFFER_SIZE = 2048;
        const int MAXIMUM_USB_STRING_LENGTH = 255;

        const string GUID_DEVINTERFACE_HUBCONTROLLER = "3abf6f2d-71c4-462a-8a92-1e6861e6af27";
        const string REGSTR_KEY_USB = "USB";
        const int DIGCF_PRESENT = 0x2;
        const int DIGCF_ALLCLASSES = 0x4;
        const int DIGCF_DEVICEINTERFACE = 0x10;
        const int SPDRP_DRIVER = 0x9;
        const int SPDRP_DEVICEDESC = 0x0;
        const int REG_SZ = 1;

        // ********************** Enumerations ************************

        //typedef enum _USB_HUB_NODE {
        //    UsbHub,
        //    UsbMIParent
        //} USB_HUB_NODE;
        enum USB_HUB_NODE
        {
            UsbHub,
            UsbMIParent
        }

        //typedef enum _USB_CONNECTION_STATUS {
        //    NoDeviceConnected,
        //    DeviceConnected,
        //    DeviceFailedEnumeration,
        //    DeviceGeneralFailure,
        //    DeviceCausedOvercurrent,
        //    DeviceNotEnoughPower,
        //    DeviceNotEnoughBandwidth,
        //    DeviceHubNestedTooDeeply,
        //    DeviceInLegacyHub
        //} USB_CONNECTION_STATUS, *PUSB_CONNECTION_STATUS;
        enum USB_CONNECTION_STATUS
        {
            NoDeviceConnected,
            DeviceConnected,
            DeviceFailedEnumeration,
            DeviceGeneralFailure,
            DeviceCausedOvercurrent,
            DeviceNotEnoughPower,
            DeviceNotEnoughBandwidth,
            DeviceHubNestedTooDeeply,
            DeviceInLegacyHub
        }

        //typedef enum _USB_DEVICE_SPEED {
        //    UsbLowSpeed = 0,
        //    UsbFullSpeed,
        //    UsbHighSpeed
        //} USB_DEVICE_SPEED;
        enum USB_DEVICE_SPEED : byte
        {
            UsbLowSpeed,
            UsbFullSpeed,
            UsbHighSpeed
        }

        // ********************** Stuctures ************************

        //typedef struct _SP_DEVINFO_DATA {
        //  DWORD cbSize;
        //  GUID ClassGuid;
        //  DWORD DevInst;
        //  ULONG_PTR Reserved;
        //} SP_DEVINFO_DATA,  *PSP_DEVINFO_DATA;
        [StructLayout(LayoutKind.Sequential)]
        struct SP_DEVINFO_DATA
        {
            public int cbSize;
            public Guid ClassGuid;
            public IntPtr DevInst;
            public IntPtr Reserved;
        }

        //typedef struct _SP_DEVICE_INTERFACE_DATA {
        //  DWORD cbSize;
        //  GUID InterfaceClassGuid;
        //  DWORD Flags;
        //  ULONG_PTR Reserved;
        //} SP_DEVICE_INTERFACE_DATA,  *PSP_DEVICE_INTERFACE_DATA;
        [StructLayout(LayoutKind.Sequential)]
        struct SP_DEVICE_INTERFACE_DATA
        {
            public int cbSize;
            public Guid InterfaceClassGuid;
            public int Flags;
            public IntPtr Reserved;
        }

        //typedef struct _SP_DEVICE_INTERFACE_DETAIL_DATA {
        //  DWORD cbSize;
        //  TCHAR DevicePath[ANYSIZE_ARRAY];
        //} SP_DEVICE_INTERFACE_DETAIL_DATA,  *PSP_DEVICE_INTERFACE_DETAIL_DATA;
        [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
        struct SP_DEVICE_INTERFACE_DETAIL_DATA
        {
            public int cbSize;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = BUFFER_SIZE)]
            public string DevicePath;
        }

        //typedef struct _USB_HCD_DRIVERKEY_NAME {
        //    ULONG ActualLength;
        //    WCHAR DriverKeyName[1];
        //} USB_HCD_DRIVERKEY_NAME, *PUSB_HCD_DRIVERKEY_NAME;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        struct USB_HCD_DRIVERKEY_NAME
        {
            public int ActualLength;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = BUFFER_SIZE)]
            public string DriverKeyName;
        }

        //typedef struct _USB_ROOT_HUB_NAME {
        //    ULONG  ActualLength;
        //    WCHAR  RootHubName[1];
        //} USB_ROOT_HUB_NAME, *PUSB_ROOT_HUB_NAME;
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        struct USB_ROOT_HUB_NAME
        {
            public int ActualLength;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = BUFFER_SIZE)]
            public string RootHubName;
        }

        //typedef struct _USB_HUB_DESCRIPTOR {
        //    UCHAR  bDescriptorLength;
        //    UCHAR  bDescriptorType;
        //    UCHAR  bNumberOfPorts;
        //    USHORT  wHubCharacteristics;
        //    UCHAR  bPowerOnToPowerGood;
        //    UCHAR  bHubControlCurrent;
        //    UCHAR  bRemoveAndPowerMask[64];
        //} USB_HUB_DESCRIPTOR, *PUSB_HUB_DESCRIPTOR;
        [StructLayout(LayoutKind.Sequential, Pack=1)]
        struct USB_HUB_DESCRIPTOR
        {
            public byte bDescriptorLength;
            public byte bDescriptorType;
            public byte bNumberOfPorts;
            public short wHubCharacteristics;
            public byte bPowerOnToPowerGood;
            public byte bHubControlCurrent;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
            public byte[] bRemoveAndPowerMask;
        }

        //typedef struct _USB_HUB_INFORMATION {
        //    USB_HUB_DESCRIPTOR HubDescriptor;
        //    BOOLEAN HubIsBusPowered;
        //} USB_HUB_INFORMATION, *PUSB_HUB_INFORMATION;
        [StructLayout(LayoutKind.Sequential)]
        struct USB_HUB_INFORMATION
        {
            public USB_HUB_DESCRIPTOR HubDescriptor;
            public byte HubIsBusPowered;
        }

        //typedef struct _USB_NODE_INFORMATION {
        //    USB_HUB_NODE  NodeType;
        //    union {
        //        USB_HUB_INFORMATION  HubInformation;
        //        USB_MI_PARENT_INFORMATION  MiParentInformation;
        //    } u;
        //} USB_NODE_INFORMATION, *PUSB_NODE_INFORMATION;
        [StructLayout(LayoutKind.Sequential)]
        struct USB_NODE_INFORMATION
        {
            public int NodeType;
            public USB_HUB_INFORMATION HubInformation;          // Yeah, I'm assuming we'll just use the first form
        }

        //typedef struct _USB_NODE_CONNECTION_INFORMATION_EX {
        //    ULONG  ConnectionIndex;
        //    USB_DEVICE_DESCRIPTOR  DeviceDescriptor;
        //    UCHAR  CurrentConfigurationValue;
        //    UCHAR  Speed;
        //    BOOLEAN  DeviceIsHub;
        //    USHORT  DeviceAddress;
        //    ULONG  NumberOfOpenPipes;
        //    USB_CONNECTION_STATUS  ConnectionStatus;
        //    USB_PIPE_INFO  PipeList[0];
        //} USB_NODE_CONNECTION_INFORMATION_EX, *PUSB_NODE_CONNECTION_INFORMATION_EX;
        [StructLayout(LayoutKind.Sequential, Pack=1)]
        struct USB_NODE_CONNECTION_INFORMATION_EX
        {
            public int ConnectionIndex;
            public USB_DEVICE_DESCRIPTOR DeviceDescriptor;
            public byte CurrentConfigurationValue;
            public byte Speed;
            public byte DeviceIsHub;
            public short DeviceAddress;
            public int NumberOfOpenPipes;
            public int ConnectionStatus;
            //public IntPtr PipeList;
        }

        //typedef struct _USB_DEVICE_DESCRIPTOR {
        //    UCHAR  bLength;
        //    UCHAR  bDescriptorType;
        //    USHORT  bcdUSB;
        //    UCHAR  bDeviceClass;
        //    UCHAR  bDeviceSubClass;
        //    UCHAR  bDeviceProtocol;
        //    UCHAR  bMaxPacketSize0;
        //    USHORT  idVendor;
        //    USHORT  idProduct;
        //    USHORT  bcdDevice;
        //    UCHAR  iManufacturer;
        //    UCHAR  iProduct;
        //    UCHAR  iSerialNumber;
        //    UCHAR  bNumConfigurations;
        //} USB_DEVICE_DESCRIPTOR, *PUSB_DEVICE_DESCRIPTOR ;
        [StructLayout(LayoutKind.Sequential, Pack=1)]
        internal struct USB_DEVICE_DESCRIPTOR
        {
            public byte bLength;
            public byte bDescriptorType;
            public short bcdUSB;
            public byte bDeviceClass;
            public byte bDeviceSubClass;
            public byte bDeviceProtocol;
            public byte bMaxPacketSize0;
            public short idVendor;
            public short idProduct;
            public short bcdDevice;
            public byte iManufacturer;
            public byte iProduct;
            public byte iSerialNumber;
            public byte bNumConfigurations;
        }
        
        //typedef struct _USB_STRING_DESCRIPTOR {
        //    UCHAR bLength;
        //    UCHAR bDescriptorType;
        //    WCHAR bString[1];
        //} USB_STRING_DESCRIPTOR, *PUSB_STRING_DESCRIPTOR;
        [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
        struct USB_STRING_DESCRIPTOR
        {
            public byte bLength;
            public byte bDescriptorType;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst=MAXIMUM_USB_STRING_LENGTH)]
            public string bString;
        }

        //typedef struct _USB_DESCRIPTOR_REQUEST {
        //  ULONG ConnectionIndex;
        //  struct {
        //    UCHAR  bmRequest;
        //    UCHAR  bRequest;
        //    USHORT  wValue;
        //    USHORT  wIndex;
        //    USHORT  wLength;
        //  } SetupPacket;
        //  UCHAR  Data[0];
        //} USB_DESCRIPTOR_REQUEST, *PUSB_DESCRIPTOR_REQUEST
        [StructLayout(LayoutKind.Sequential)]
        struct USB_SETUP_PACKET
        {
            public byte bmRequest;
            public byte bRequest;
            public short wValue;
            public short wIndex;
            public short wLength;
        }
        [StructLayout(LayoutKind.Sequential)]
        struct USB_DESCRIPTOR_REQUEST
        {
            public int ConnectionIndex;
            public USB_SETUP_PACKET SetupPacket;

⌨️ 快捷键说明

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