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