C#USB设备枚举Kernel32的PInvoke_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > C#USB设备枚举Kernel32的PInvoke

C#USB设备枚举Kernel32的PInvoke

 2015/5/13 15:38:00  haishu  程序员俱乐部  我要评论(0)
  • 摘要:usingSystem;usingSystem.Runtime.InteropServices;usingSystem.Security;namespaceSplash{#regionKernel32[SuppressUnmanagedCodeSecurity]internalstaticclassKernel32{///<summary>///无效的文件句柄///</summary>
  • 标签:C# 设备 枚举
    class="dp-c" start="1">
  1. using System;  
  2. using System.Runtime.InteropServices;  
  3. using System.Security;  
  4.   
  5. namespace Splash  
  6. {  
  7.     #region Kernel32  
  8.     [SuppressUnmanagedCodeSecurity]  
  9.     internal static class Kernel32  
  10.     {  
  11.         /// <summary>  
  12.         /// 无效的文件句柄  
  13.         /// </summary>  
  14.         public static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);  
  15.  
  16.         #region CreateFile  
  17.         [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]  
  18.         public static extern IntPtr CreateFile(  
  19.             String fileName,  
  20.             [MarshalAs(UnmanagedType.U4)] NativeFileAccess fileAccess,  
  21.             [MarshalAs(UnmanagedType.U4)] NativeFileShare fileShare,  
  22.             IntPtr securityAttributes,  
  23.             [MarshalAs(UnmanagedType.U4)] NativeFileMode creationDisposition,  
  24.             NativeFileFlag flags,  
  25.             IntPtr template  
  26.             );  
  27.   
  28.         [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]  
  29.         public static extern IntPtr CreateFile(  
  30.             String fileName,  
  31.             [MarshalAs(UnmanagedType.U4)] NativeFileAccess fileAccess,  
  32.             [MarshalAs(UnmanagedType.U4)] NativeFileShare fileShare,  
  33.             IntPtr securityAttributes,  
  34.             [MarshalAs(UnmanagedType.U4)] NativeFileMode creationDisposition,  
  35.             IntPtr flags,  
  36.             IntPtr template  
  37.             );  
  38.         #endregion  
  39.   
  40.         [DllImport("kernel32.dll", SetLastError = true)]  
  41.         [return: MarshalAs(UnmanagedType.Bool)]  
  42.         public static extern Boolean CloseHandle(IntPtr hFile);  
  43.  
  44.         #region ReadFile  
  45.         [DllImport("kernel32.dll", SetLastError = true)]  
  46.         public static extern Boolean ReadFile(  
  47.             IntPtr hFile,  
  48.             [Out] Byte[] lpBuffer,  
  49.             Int32 nNumberOfBytesToRead,  
  50.             out Int32 lpNumberOfBytesRead,  
  51.             [In] ref System.Threading.NativeOverlapped lpOverlapped  
  52.             );  
  53.   
  54.         [DllImport("kernel32.dll", SetLastError = true)]  
  55.         public static extern Boolean ReadFile(  
  56.             IntPtr hFile,  
  57.             [Out] Byte[] lpBuffer,  
  58.             Int32 nNumberOfBytesToRead,  
  59.             IntPtr lpNumberOfBytesRead,  
  60.             [In] ref System.Threading.NativeOverlapped lpOverlapped  
  61.             );  
  62.   
  63.         [DllImport("kernel32.dll", SetLastError = true)]  
  64.         public static extern Boolean ReadFile(  
  65.             IntPtr hFile,  
  66.             [Out] Byte[] lpBuffer,  
  67.             Int32 nNumberOfBytesToRead,  
  68.             out Int32 lpNumberOfBytesRead,  
  69.             IntPtr lpOverlapped  
  70.             );  
  71.         #endregion  
  72.  
  73.         #region WriteFile  
  74.         [DllImport("kernel32.dll")]  
  75.         public static extern Boolean WriteFile(  
  76.             IntPtr hFile,  
  77.             Byte[] lpBuffer,  
  78.             Int32 nNumberOfBytesToWrite,  
  79.             out Int32 lpNumberOfBytesWritten,  
  80.             [In] ref System.Threading.NativeOverlapped lpOverlapped  
  81.             );  
  82.   
  83.         [DllImport("kernel32.dll")]  
  84.         public static extern Boolean WriteFile(  
  85.             IntPtr hFile,  
  86.             Byte[] lpBuffer,  
  87.             Int32 nNumberOfBytesToWrite,  
  88.             IntPtr lpNumberOfBytesWritten,  
  89.             [In] ref System.Threading.NativeOverlapped lpOverlapped  
  90.             );  
  91.   
  92.         [DllImport("kernel32.dll")]  
  93.         public static extern Boolean WriteFile(  
  94.             IntPtr hFile,  
  95.             Byte[] lpBuffer,  
  96.             Int32 nNumberOfBytesToWrite,  
  97.             IntPtr lpNumberOfBytesWritten,  
  98.             IntPtr lpOverlapped  
  99.             );  
  100.         #endregion  
  101.   
  102.         [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]  
  103.         public static extern Boolean GetOverlappedResult(  
  104.             IntPtr hFile,  
  105.             [In] ref System.Threading.NativeOverlapped lpOverlapped,  
  106.             out Int32 lpNumberOfBytesTransferred,  
  107.             Boolean bWait  
  108.             );          
  109.     }  
  110.     #endregion  
  111.  
  112.     #region ENUM  
  113.     [Flags]  
  114.     internal enum NativeFileAccess : uint  
  115.     {  
  116.         GENERIC_READ = (0x80000000),  
  117.         GENERIC_WRITE = (0x40000000),  
  118.         GENERIC_EXECUTE = (0x20000000),  
  119.         GENERIC_ALL = (0x10000000),  
  120.   
  121.         FILE_SPECIAL = 0,  
  122.         FILE_APPEND_DATA = (0x0004),    // 文件  
  123.         FILE_READ_DATA = (0x0001),      // 文件和管道  
  124.         FILE_WRITE_DATA = (0x0002),     // 文件和管道  
  125.         FILE_READ_EA = (0x0008),        // 文件和目录  
  126.         FILE_WRITE_EA = (0x0010),       // 文件和目录  
  127.         FILE_READ_ATTRIBUTES = (0x0080),    // 所有  
  128.         FILE_WRITE_ATTRIBUTES = (0x0100),   // 所有  
  129.         DELETE = 0x00010000,  
  130.         READ_CONTROL = (0x00020000),  
  131.         WRITE_DAC = (0x00040000),  
  132.         WRITE_OWNER = (0x00080000),  
  133.         SYNCHRONIZE = (0x00100000),  
  134.         STANDARD_RIGHTS_REQUIRED = (0x000F0000),  
  135.         STANDARD_RIGHTS_READ = (READ_CONTROL),  
  136.         STANDARD_RIGHTS_WRITE = (READ_CONTROL),  
  137.         STANDARD_RIGHTS_EXECUTE = (READ_CONTROL),  
  138.         STANDARD_RIGHTS_ALL = (0x001F0000),  
  139.         SPECIFIC_RIGHTS_ALL = (0x0000FFFF),  
  140.         FILE_GENERIC_READ = (STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE),  
  141.         FILE_GENERIC_WRITE = (STANDARD_RIGHTS_WRITE | FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | FILE_APPEND_DATA | SYNCHRONIZE),  
  142.         SPECIAL = 0  
  143.     }  
  144.   
  145.     internal enum NativeFileMode : uint  
  146.     {  
  147.         CREATE_NEW = 1,  
  148.         CREATE_ALWAYS = 2,  
  149.         OPEN_EXISTING = 3,  
  150.         OPEN_ALWAYS = 4,  
  151.         TRUNCATE_EXISTING = 5,  
  152.     }  
  153.   
  154.     [Flags]  
  155.     internal enum NativeFileShare : uint  
  156.     {  
  157.         NONE = 0,  
  158.         FILE_SHARE_READ = 0x00000001,  
  159.         FILE_SHARE_WRITE = 0x00000002,  
  160.         FILE_SHARE_DEELETE = 0x00000004,  
  161.     }  
  162.   
  163.     [Flags]  
  164.     internal enum NativeFileFlag : uint  
  165.     {  
  166.         FILE_ATTRIBUTE_READONLY = 0x00000001,  
  167.         FILE_ATTRIBUTE_HIDDEN = 0x00000002,  
  168.         FILE_ATTRIBUTE_SYSTEM = 0x00000004,  
  169.         FILE_ATTRIBUTE_DIRECTORY = 0x00000010,  
  170.         FILE_ATTRIBUTE_ARCHIVE = 0x00000020,  
  171.         FILE_ATTRIBUTE_DEVICE = 0x00000040,  
  172.         FILE_ATTRIBUTE_NORMAL = 0x00000080,  
  173.         FILE_ATTRIBUTE_TEMPORARY = 0x00000100,  
  174.         FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200,  
  175.         FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400,  
  176.         FILE_ATTRIBUTE_COMPRESSED = 0x00000800,  
  177.         FILE_ATTRIBUTE_OFFLINE = 0x00001000,  
  178.         FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000,  
  179.         FILE_ATTRIBUTE_ENCRYPTED = 0x00004000,  
  180.         FILE_FLAG_WRITE_THROUGH = 0x80000000,  
  181.         FILE_FLAG_OVERLAPPED = 0x40000000,  
  182.         FILE_FLAG_NO_BUFFERING = 0x20000000,  
  183.         FILE_FLAG_RANDOM_ACCESS = 0x10000000,  
  184.         FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000,  
  185.         FILE_FLAG_DELETE_ON_CLOSE = 0x04000000,  
  186.         FILE_FLAG_BACKUP_SEMANTICS = 0x02000000,  
  187.         FILE_FLAG_POSIX_SEMANTICS = 0x01000000,  
  188.         FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000,  
  189.         FILE_FLAG_OPEN_NO_RECALL = 0x00100000,  
  190.         FILE_FLAG_FIRST_PIPE_INSTANCE = 0x00080000,  
  191.     }  
  192.     #endregion  
  193. }  
发表评论
用户名: 匿名