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