C#:USB Device Enumeration (i) PInvoke of DeviceIoControl

Source: Internet
Author: User
Tags pack
 
/*----------------------------------------------------------file name: DeviceIoControl.cs Author: Qin Jianhui msn:splashcn@msn.com qq:36748897 Blog: http://blog.csdn.net/jhqin development environment: Visual Studio V2010. NET Framework 4 Client Profile version history: V 1.0 October 10, 2011 Implementation of the DeviceIoControl Interface PInvoke reference: http://www.pinvoke.net/---------------------------------------
---------------------*/using System;

Using System.Runtime.InteropServices; 
    Namespace Splash.IO.PORTS {#region enum public enum Usb_hub_node:uint {UsbHub, usbmiparent public enum Usb_connection_status {nodeviceconnected, deviceconnected, Devicefail Edenumeration, Devicegeneralfailure, Devicecausedovercurrent, Devicenotenoughpower, Device Notenoughbandwidth, devicehubnestedtoodeeply, deviceinlegacyhub} public enum Usb_device_speed: byte {usblowspeed,//Low Speed USB 1.1 usbfullspeed,// Full Speed USB 1.1 usbhighspeed,//High Speed USB 2.0 usbsuperspeed//Speed USB 3.0} #endregion public part
        Ial class USB {Internal const Int32 ioctl_get_hcd_driverkey_name = 0x220424;
        Internal const Int32 Ioctl_usb_get_root_hub_name = 0x220408;
        Internal const Int32 Ioctl_usb_get_node_connection_name = 0x220414;
        Internal const Int32 ioctl_usb_get_node_information = 0x220408;
        Internal const Int32 IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX = 0x220448;

        Internal const Int32 ioctl_usb_get_descriptor_from_node_connection = 0x220410;
        Internal const INT32 Maximum_usb_string_length = 255;

        Internal const INT32 Usb_string_descriptor_type = 3;
            [StructLayout (layoutkind.sequential, CharSet = charset.auto)] internal struct Usb_hcd_driverkey_name {
            Public Int32 actuallength;
        [MarshalAs (UnmanagedType.ByValTStr, SizeConst = 256)] public String Name; }

        #region Usb_node_information [StructLayout (layoutkind.sequential, Pack = 1)] internal struct USB
            _hub_descriptor {public Byte bdescriptorlength;    Public Byte bDescriptorType;     Descriptor type: 0x29 public Byte bnumberofports;   Number of downstream ports supported public Int16 Whubcharacteristics;    Feature description public Byte Bpowerontopowergood;     The time interval (in 2ms) from the port to power the port to work normally is public Byte bhubcontrolcurrent; Maximum current required for the device [MarshalAs (UnmanagedType.ByValArray, SizeConst =)] public byte[] Bremoveandpowermas  K 
        Indicates whether the device attached to the hub port can be moved} [StructLayout (layoutkind.sequential)] internal struct usb_hub_information
            {public Usb_hub_descriptor hubdescriptor;
        Public Byte hubisbuspowered; [StructLayout (layoutkind.sequential)] internal struct Usb_mi_parent_information {p Ublic Int32 NumberofinterfaceS
        
        };
            [StructLayout (layoutkind.explicit)] internal struct Usbnodeunion {[FieldOffset (0)]
            Public Usb_hub_information hubinformation;
        [FieldOffset (0)] public usb_mi_parent_information miparentinformation;  [StructLayout (layoutkind.sequential)] internal struct Usb_node_information {public
            Usb_hub_node NodeType;
        Public usbnodeunion u; } #endregion #region Usb_node_connection_information [StructLayout (layoutkind.sequential, Pack =
            1)] Internal struct Usb_device_descriptor {public Byte blength;
            Public Byte bDescriptorType;
            Public UInt16 Bcdusb;
            Public Byte Bdeviceclass;
            Public Byte Bdevicesubclass;
            Public Byte Bdeviceprotocol;
            Public Byte bMaxPacketSize0;
            Public UInt16 Idvendor; Public UIntIdproduct;
            Public UInt16 Bcddevice;
            Public Byte Imanufacturer;
            Public Byte iproduct;
            Public Byte Iserialnumber;
        Public Byte bnumconfigurations;
        [StructLayout (layoutkind.sequential, Pack = 1)] internal struct usb_endpoint_descriptor
            {public Byte blength;
            Public Byte bDescriptorType;
            Public Byte bendpointaddress;
            Public Byte bmattributes;
            Public UInt16 wmaxpacketsize;
        Public Byte Binterval; [StructLayout (layoutkind.sequential, Pack = 1)] internal struct Usb_pipe_info {pub
            Lic Usb_endpoint_descriptor Endpointdescriptor;
        Public UInt32 Scheduleoffset;
        [StructLayout (layoutkind.sequential, Pack = 1)] internal struct USB_NODE_CONNECTION_INFORMATION_EX
            {public Int32 connectionindex; Public Usb_device_deScriptor Devicedescriptor;
            Public Byte Currentconfigurationvalue;
            Public Byte Speed;
            Public Byte Deviceishub;
            Public Int16 deviceaddress;
            Public Int32 numberofopenpipes;
            Public Usb_connection_status Connectionstatus;
        [MarshalAs (UnmanagedType.ByValArray, SizeConst =)] public usb_pipe_info[] pipelist; } #endregion #region Usb_descriptor_request [StructLayout (layoutkind.sequential)] Interna
            l struct Usb_setup_packet {public Byte bmrequest;
            Public Byte brequest;
            Public UInt16 Wvalue;
            Public UInt16 Windex;
        Public UInt16 wlength;
        } [StructLayout (layoutkind.sequential, CharSet = charset.auto)] internal struct
            {public Byte blength;
            Public Byte bDescriptorType; [MarshalAs (UnmanagedType.ByValTStr, SizEconst = maximum_usb_string_length)] public STRING bstring; [StructLayout (layoutkind.sequential)] internal struct Usb_descriptor_request {Publ
            IC Int32 Connectionindex;       
            Public Usb_setup_packet Setuppacket;
        Public Usb_string_descriptor Data; #endregion #region Usb_node_connection_driverkey_name [StructLayout (LayoutKind.Sequential, Char Set = CharSet.Auto)] Internal struct Usb_node_connection_driverkey_name {public Int32 Connect
            Ionindex;
            Public Int32 actuallength;
        [MarshalAs (UnmanagedType.ByValTStr, SizeConst = maximum_usb_string_length)] public STRING driverkeyname; #endregion #region DeviceIoControl [DllImport ("kernel32.dll", CharSet = CharSet.Ansi, Setla Sterror = true)] internal static extern Boolean DeviceIoControl (IntPtr hfile, inT32 Dwiocontrolcode, IntPtr lpinbuffer, Int32 ninbuffersize, ref Usb_hcd_driverkey_nam
            E lpOutBuffer, Int32 noutbuffersize, out Int32 nbytesreturned, IntPtr lpoverlapped

        ); [DllImport ("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)] internal static extern Boolean Deviceioco
            Ntrol (IntPtr hfile, Int32 dwiocontrolcode, ref usb_node_information Lpinbuffer,
            Int32 ninbuffersize, ref usb_node_information lpOutBuffer, Int32 noutbuffersize,

        Out Int32 lpbytesreturned, IntPtr lpoverlapped); [DllImport ("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)] internal static extern Boolean Deviceioco Ntrol (IntPtr hfile, Int32 dwiocontrolcode, ref USB_NODE_CONNECTION_INFORMATION_EX LpI Nbuffer, Int32 ninbuffersize, ref USB_NODE_CONNECTION_INFORMATION_EX lpOutBuffer, Int32 noutbuffersize, out Int32

        lpbytesreturned, IntPtr lpoverlapped); [DllImport ("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)] internal static extern Boolean Deviceioco
            Ntrol (IntPtr hfile, Int32 dwiocontrolcode, ref usb_descriptor_request Lpinbuffer,
            Int32 ninbuffersize, ref usb_descriptor_request lpOutBuffer, Int32 noutbuffersize,

        Out Int32 lpbytesreturned, IntPtr lpoverlapped); [DllImport ("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)] internal static extern Boolean Deviceioco Ntrol (IntPtr hfile, Int32 dwiocontrolcode, ref usb_node_connection_driverkey_name LpI
Nbuffer, Int32 ninbuffersize, ref usb_node_connection_driverkey_name lpOutBuffer,            Int32 noutbuffersize, out Int32 lpbytesreturned, IntPtr lpoverlapped);
 #endregion}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.