Win32CreateFile.cs
author StephaneLenclud
Sun, 15 Feb 2015 20:28:46 +0100
changeset 62 bc92e2648220
child 76 831ebeeecfdf
permissions -rw-r--r--
Device list now showing usage collection string.
     1 using System;
     2 using System.Runtime.InteropServices;
     3 using Microsoft.Win32.SafeHandles;
     4 
     5 namespace Win32
     6 {
     7 
     8     static partial class Function
     9     {
    10         [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    11         public static extern SafeFileHandle CreateFile(
    12              [MarshalAs(UnmanagedType.LPTStr)] string lpFileName,
    13              [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
    14              [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
    15              IntPtr lpSecurityAttributes, // optional SECURITY_ATTRIBUTES struct or IntPtr.Zero
    16              [MarshalAs(UnmanagedType.U4)] CreationDisposition dwCreationDisposition,
    17              [MarshalAs(UnmanagedType.U4)] FileFlagsAttributes dwFlagsAndAttributes,
    18              IntPtr hTemplateFile);
    19 
    20         [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
    21         public static extern SafeFileHandle CreateFileA(
    22              [MarshalAs(UnmanagedType.LPStr)] string lpFileName,
    23              [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
    24              [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
    25              IntPtr lpSecurityAttributes,
    26              [MarshalAs(UnmanagedType.U4)] CreationDisposition dwCreationDisposition,
    27              [MarshalAs(UnmanagedType.U4)] FileFlagsAttributes dwFlagsAndAttributes,
    28              IntPtr hTemplateFile);
    29 
    30         [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    31         public static extern SafeFileHandle CreateFileW(
    32              [MarshalAs(UnmanagedType.LPWStr)] string lpFileName,
    33              [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
    34              [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
    35              IntPtr lpSecurityAttributes,
    36              [MarshalAs(UnmanagedType.U4)] CreationDisposition dwCreationDisposition,
    37              [MarshalAs(UnmanagedType.U4)] FileFlagsAttributes dwFlagsAndAttributes,
    38              IntPtr hTemplateFile);
    39     }
    40 
    41 
    42     static partial class Macro
    43     {
    44 
    45     }
    46 
    47 
    48 
    49     static partial class Const
    50     {
    51 
    52     }
    53 
    54     [Flags]
    55     enum FileAccess : uint
    56     {
    57         NONE = 0,
    58 
    59         GENERIC_ALL = 0x10000000,
    60         GENERIC_EXECUTE = 0x20000000,
    61         GENERIC_READ = 0x80000000,
    62         GENERIC_WRITE = 0x40000000,
    63 
    64         FILE_READ_DATA = (0x0001),  // file & pipe
    65         FILE_LIST_DIRECTORY = (0x0001),  // directory
    66 
    67         FILE_WRITE_DATA = (0x0002),   // file & pipe
    68         FILE_ADD_FILE = (0x0002),  // directory
    69 
    70         FILE_APPEND_DATA = (0x0004), // file
    71         FILE_ADD_SUBDIRECTORY = (0x0004),  // directory
    72         FILE_CREATE_PIPE_INSTANCE = (0x0004),   // named pipe
    73 
    74         FILE_READ_EA = (0x0008),  // file & directory
    75 
    76         FILE_WRITE_EA = (0x0010),    // file & directory
    77 
    78         FILE_EXECUTE = (0x0020),    // file
    79         FILE_TRAVERSE = (0x0020),    // directory
    80 
    81         FILE_DELETE_CHILD = (0x0040),    // directory
    82 
    83         FILE_READ_ATTRIBUTES = (0x0080),    // all
    84 
    85         FILE_WRITE_ATTRIBUTES = (0x0100),    // all
    86 
    87         FILE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x1FF),
    88 
    89         FILE_GENERIC_READ = (STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE),
    90         FILE_GENERIC_WRITE = (STANDARD_RIGHTS_WRITE | FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | FILE_APPEND_DATA | SYNCHRONIZE),
    91         FILE_GENERIC_EXECUTE = (STANDARD_RIGHTS_EXECUTE | FILE_READ_ATTRIBUTES | FILE_EXECUTE | SYNCHRONIZE),
    92 
    93         DELETE = (0x00010000),
    94         READ_CONTROL = (0x00020000),
    95         WRITE_DAC = (0x00040000),
    96         WRITE_OWNER = (0x00080000),
    97         SYNCHRONIZE = (0x00100000),
    98 
    99         STANDARD_RIGHTS_REQUIRED = (0x000F0000),
   100 
   101         STANDARD_RIGHTS_READ = (READ_CONTROL),
   102         STANDARD_RIGHTS_WRITE = (READ_CONTROL),
   103         STANDARD_RIGHTS_EXECUTE = (READ_CONTROL),
   104 
   105         STANDARD_RIGHTS_ALL = (0x001F0000),
   106 
   107         SPECIFIC_RIGHTS_ALL = (0x0000FFFF),
   108 
   109         ACCESS_SYSTEM_SECURITY = (0x01000000),
   110 
   111         MAXIMUM_ALLOWED = (0x02000000)
   112     }
   113     
   114 
   115 
   116     [Flags]
   117     public enum FileShare : uint
   118     {
   119         /// <summary>
   120         /// Prevents other processes from opening a file or device if they request delete, read, or write access.
   121         /// </summary>
   122         FILE_SHARE_NONE = 0x00000000,
   123         /// <summary>
   124         /// Enables subsequent open operations on an object to request read access.
   125         /// Otherwise, other processes cannot open the object if they request read access.
   126         /// If this flag is not specified, but the object has been opened for read access, the function fails.
   127         /// </summary>
   128         FILE_SHARE_READ = 0x00000001,
   129         /// <summary>
   130         /// Enables subsequent open operations on an object to request write access.
   131         /// Otherwise, other processes cannot open the object if they request write access.
   132         /// If this flag is not specified, but the object has been opened for write access, the function fails.
   133         /// </summary>
   134         FILE_SHARE_WRITE = 0x00000002,
   135         /// <summary>
   136         /// Enables subsequent open operations on an object to request delete access.
   137         /// Otherwise, other processes cannot open the object if they request delete access.
   138         /// If this flag is not specified, but the object has been opened for delete access, the function fails.
   139         /// </summary>
   140         FILE_SHARE_DELETE = 0x00000004
   141     }
   142 
   143     public enum CreationDisposition : uint
   144     {
   145         /// <summary>
   146         /// Creates a new file. The function fails if a specified file exists.
   147         /// </summary>
   148         CREATE_NEW = 1,
   149         /// <summary>
   150         /// Creates a new file, always.
   151         /// If a file exists, the function overwrites the file, clears the existing attributes, combines the specified file attributes,
   152         /// and flags with FILE_ATTRIBUTE_ARCHIVE, but does not set the security descriptor that the SECURITY_ATTRIBUTES structure specifies.
   153         /// </summary>
   154         CREATE_ALWAYS = 2,
   155         /// <summary>
   156         /// Opens a file. The function fails if the file does not exist.
   157         /// </summary>
   158         OPEN_EXISTING = 3,
   159         /// <summary>
   160         /// Opens a file, always.
   161         /// If a file does not exist, the function creates a file as if dwCreationDisposition is CREATE_NEW.
   162         /// </summary>
   163         OPEN_ALWAYS = 4,
   164         /// <summary>
   165         /// Opens a file and truncates it so that its size is 0 (zero) bytes. The function fails if the file does not exist.
   166         /// The calling process must open the file with the GENERIC_WRITE access right.
   167         /// </summary>
   168         TRUNCATE_EXISTING = 5
   169     }
   170 
   171     [Flags]
   172     public enum FileFlagsAttributes : uint
   173     {
   174         FILE_ATTRIBUTE_READONLY = 0x00000001,
   175         FILE_ATTRIBUTE_HIDDEN = 0x00000002,
   176         FILE_ATTRIBUTE_SYSTEM = 0x00000004,
   177         FILE_ATTRIBUTE_DIRECTORY = 0x00000010,
   178         FILE_ATTRIBUTE_ARCHIVE = 0x00000020,
   179         FILE_ATTRIBUTE_DEVICE = 0x00000040,
   180         FILE_ATTRIBUTE_NORMAL = 0x00000080,
   181         FILE_ATTRIBUTE_TEMPORARY = 0x00000100,
   182         FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200,
   183         FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400,
   184         FILE_ATTRIBUTE_COMPRESSED = 0x00000800,
   185         FILE_ATTRIBUTE_OFFLINE = 0x00001000,
   186         FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000,
   187         FILE_ATTRIBUTE_ENCRYPTED = 0x00004000,
   188         FILE_ATTRIBUTE_INTEGRITY_STREAM = 0x00008000,
   189         FILE_ATTRIBUTE_VIRTUAL = 0x00010000,
   190         FILE_ATTRIBUTE_NO_SCRUB_DATA = 0x00020000,
   191         //  These are flags supported through CreateFile (W7) and CreateFile2 (W8 and beyond)
   192         FILE_FLAG_WRITE_THROUGH = 0x80000000,
   193         FILE_FLAG_OVERLAPPED = 0x40000000,
   194         FILE_FLAG_NO_BUFFERING = 0x20000000,
   195         FILE_FLAG_RANDOM_ACCESS = 0x10000000,
   196         FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000,
   197         FILE_FLAG_DELETE_ON_CLOSE = 0x04000000,
   198         FILE_FLAG_BACKUP_SEMANTICS = 0x02000000,
   199         FILE_FLAG_POSIX_SEMANTICS = 0x01000000,
   200         FILE_FLAG_SESSION_AWARE = 0x00800000,
   201         FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000,
   202         FILE_FLAG_OPEN_NO_RECALL = 0x00100000,
   203         FILE_FLAG_FIRST_PIPE_INSTANCE = 0x00080000
   204     }
   205 
   206 
   207 
   208 
   209 }