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