1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/Win32CreateFile.cs Sat Dec 06 12:13:39 2014 +0100
1.3 @@ -0,0 +1,209 @@
1.4 +using System;
1.5 +using System.Runtime.InteropServices;
1.6 +using Microsoft.Win32.SafeHandles;
1.7 +
1.8 +namespace Win32
1.9 +{
1.10 +
1.11 + static partial class Function
1.12 + {
1.13 + [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
1.14 + public static extern SafeFileHandle CreateFile(
1.15 + [MarshalAs(UnmanagedType.LPTStr)] string lpFileName,
1.16 + [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
1.17 + [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
1.18 + IntPtr lpSecurityAttributes, // optional SECURITY_ATTRIBUTES struct or IntPtr.Zero
1.19 + [MarshalAs(UnmanagedType.U4)] CreationDisposition dwCreationDisposition,
1.20 + [MarshalAs(UnmanagedType.U4)] FileFlagsAttributes dwFlagsAndAttributes,
1.21 + IntPtr hTemplateFile);
1.22 +
1.23 + [DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true)]
1.24 + public static extern SafeFileHandle CreateFileA(
1.25 + [MarshalAs(UnmanagedType.LPStr)] string lpFileName,
1.26 + [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
1.27 + [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
1.28 + IntPtr lpSecurityAttributes,
1.29 + [MarshalAs(UnmanagedType.U4)] CreationDisposition dwCreationDisposition,
1.30 + [MarshalAs(UnmanagedType.U4)] FileFlagsAttributes dwFlagsAndAttributes,
1.31 + IntPtr hTemplateFile);
1.32 +
1.33 + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
1.34 + public static extern SafeFileHandle CreateFileW(
1.35 + [MarshalAs(UnmanagedType.LPWStr)] string lpFileName,
1.36 + [MarshalAs(UnmanagedType.U4)] FileAccess dwDesiredAccess,
1.37 + [MarshalAs(UnmanagedType.U4)] FileShare dwShareMode,
1.38 + IntPtr lpSecurityAttributes,
1.39 + [MarshalAs(UnmanagedType.U4)] CreationDisposition dwCreationDisposition,
1.40 + [MarshalAs(UnmanagedType.U4)] FileFlagsAttributes dwFlagsAndAttributes,
1.41 + IntPtr hTemplateFile);
1.42 + }
1.43 +
1.44 +
1.45 + static partial class Macro
1.46 + {
1.47 +
1.48 + }
1.49 +
1.50 +
1.51 +
1.52 + static partial class Const
1.53 + {
1.54 +
1.55 + }
1.56 +
1.57 + [Flags]
1.58 + enum FileAccess : uint
1.59 + {
1.60 + NONE = 0,
1.61 +
1.62 + GENERIC_ALL = 0x10000000,
1.63 + GENERIC_EXECUTE = 0x20000000,
1.64 + GENERIC_READ = 0x80000000,
1.65 + GENERIC_WRITE = 0x40000000,
1.66 +
1.67 + FILE_READ_DATA = (0x0001), // file & pipe
1.68 + FILE_LIST_DIRECTORY = (0x0001), // directory
1.69 +
1.70 + FILE_WRITE_DATA = (0x0002), // file & pipe
1.71 + FILE_ADD_FILE = (0x0002), // directory
1.72 +
1.73 + FILE_APPEND_DATA = (0x0004), // file
1.74 + FILE_ADD_SUBDIRECTORY = (0x0004), // directory
1.75 + FILE_CREATE_PIPE_INSTANCE = (0x0004), // named pipe
1.76 +
1.77 + FILE_READ_EA = (0x0008), // file & directory
1.78 +
1.79 + FILE_WRITE_EA = (0x0010), // file & directory
1.80 +
1.81 + FILE_EXECUTE = (0x0020), // file
1.82 + FILE_TRAVERSE = (0x0020), // directory
1.83 +
1.84 + FILE_DELETE_CHILD = (0x0040), // directory
1.85 +
1.86 + FILE_READ_ATTRIBUTES = (0x0080), // all
1.87 +
1.88 + FILE_WRITE_ATTRIBUTES = (0x0100), // all
1.89 +
1.90 + FILE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0x1FF),
1.91 +
1.92 + FILE_GENERIC_READ = (STANDARD_RIGHTS_READ | FILE_READ_DATA | FILE_READ_ATTRIBUTES | FILE_READ_EA | SYNCHRONIZE),
1.93 + FILE_GENERIC_WRITE = (STANDARD_RIGHTS_WRITE | FILE_WRITE_DATA | FILE_WRITE_ATTRIBUTES | FILE_WRITE_EA | FILE_APPEND_DATA | SYNCHRONIZE),
1.94 + FILE_GENERIC_EXECUTE = (STANDARD_RIGHTS_EXECUTE | FILE_READ_ATTRIBUTES | FILE_EXECUTE | SYNCHRONIZE),
1.95 +
1.96 + DELETE = (0x00010000),
1.97 + READ_CONTROL = (0x00020000),
1.98 + WRITE_DAC = (0x00040000),
1.99 + WRITE_OWNER = (0x00080000),
1.100 + SYNCHRONIZE = (0x00100000),
1.101 +
1.102 + STANDARD_RIGHTS_REQUIRED = (0x000F0000),
1.103 +
1.104 + STANDARD_RIGHTS_READ = (READ_CONTROL),
1.105 + STANDARD_RIGHTS_WRITE = (READ_CONTROL),
1.106 + STANDARD_RIGHTS_EXECUTE = (READ_CONTROL),
1.107 +
1.108 + STANDARD_RIGHTS_ALL = (0x001F0000),
1.109 +
1.110 + SPECIFIC_RIGHTS_ALL = (0x0000FFFF),
1.111 +
1.112 + ACCESS_SYSTEM_SECURITY = (0x01000000),
1.113 +
1.114 + MAXIMUM_ALLOWED = (0x02000000)
1.115 + }
1.116 +
1.117 +
1.118 +
1.119 + [Flags]
1.120 + public enum FileShare : uint
1.121 + {
1.122 + /// <summary>
1.123 + /// Prevents other processes from opening a file or device if they request delete, read, or write access.
1.124 + /// </summary>
1.125 + FILE_SHARE_NONE = 0x00000000,
1.126 + /// <summary>
1.127 + /// Enables subsequent open operations on an object to request read access.
1.128 + /// Otherwise, other processes cannot open the object if they request read access.
1.129 + /// If this flag is not specified, but the object has been opened for read access, the function fails.
1.130 + /// </summary>
1.131 + FILE_SHARE_READ = 0x00000001,
1.132 + /// <summary>
1.133 + /// Enables subsequent open operations on an object to request write access.
1.134 + /// Otherwise, other processes cannot open the object if they request write access.
1.135 + /// If this flag is not specified, but the object has been opened for write access, the function fails.
1.136 + /// </summary>
1.137 + FILE_SHARE_WRITE = 0x00000002,
1.138 + /// <summary>
1.139 + /// Enables subsequent open operations on an object to request delete access.
1.140 + /// Otherwise, other processes cannot open the object if they request delete access.
1.141 + /// If this flag is not specified, but the object has been opened for delete access, the function fails.
1.142 + /// </summary>
1.143 + FILE_SHARE_DELETE = 0x00000004
1.144 + }
1.145 +
1.146 + public enum CreationDisposition : uint
1.147 + {
1.148 + /// <summary>
1.149 + /// Creates a new file. The function fails if a specified file exists.
1.150 + /// </summary>
1.151 + CREATE_NEW = 1,
1.152 + /// <summary>
1.153 + /// Creates a new file, always.
1.154 + /// If a file exists, the function overwrites the file, clears the existing attributes, combines the specified file attributes,
1.155 + /// and flags with FILE_ATTRIBUTE_ARCHIVE, but does not set the security descriptor that the SECURITY_ATTRIBUTES structure specifies.
1.156 + /// </summary>
1.157 + CREATE_ALWAYS = 2,
1.158 + /// <summary>
1.159 + /// Opens a file. The function fails if the file does not exist.
1.160 + /// </summary>
1.161 + OPEN_EXISTING = 3,
1.162 + /// <summary>
1.163 + /// Opens a file, always.
1.164 + /// If a file does not exist, the function creates a file as if dwCreationDisposition is CREATE_NEW.
1.165 + /// </summary>
1.166 + OPEN_ALWAYS = 4,
1.167 + /// <summary>
1.168 + /// Opens a file and truncates it so that its size is 0 (zero) bytes. The function fails if the file does not exist.
1.169 + /// The calling process must open the file with the GENERIC_WRITE access right.
1.170 + /// </summary>
1.171 + TRUNCATE_EXISTING = 5
1.172 + }
1.173 +
1.174 + [Flags]
1.175 + public enum FileFlagsAttributes : uint
1.176 + {
1.177 + FILE_ATTRIBUTE_READONLY = 0x00000001,
1.178 + FILE_ATTRIBUTE_HIDDEN = 0x00000002,
1.179 + FILE_ATTRIBUTE_SYSTEM = 0x00000004,
1.180 + FILE_ATTRIBUTE_DIRECTORY = 0x00000010,
1.181 + FILE_ATTRIBUTE_ARCHIVE = 0x00000020,
1.182 + FILE_ATTRIBUTE_DEVICE = 0x00000040,
1.183 + FILE_ATTRIBUTE_NORMAL = 0x00000080,
1.184 + FILE_ATTRIBUTE_TEMPORARY = 0x00000100,
1.185 + FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200,
1.186 + FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400,
1.187 + FILE_ATTRIBUTE_COMPRESSED = 0x00000800,
1.188 + FILE_ATTRIBUTE_OFFLINE = 0x00001000,
1.189 + FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000,
1.190 + FILE_ATTRIBUTE_ENCRYPTED = 0x00004000,
1.191 + FILE_ATTRIBUTE_INTEGRITY_STREAM = 0x00008000,
1.192 + FILE_ATTRIBUTE_VIRTUAL = 0x00010000,
1.193 + FILE_ATTRIBUTE_NO_SCRUB_DATA = 0x00020000,
1.194 + // These are flags supported through CreateFile (W7) and CreateFile2 (W8 and beyond)
1.195 + FILE_FLAG_WRITE_THROUGH = 0x80000000,
1.196 + FILE_FLAG_OVERLAPPED = 0x40000000,
1.197 + FILE_FLAG_NO_BUFFERING = 0x20000000,
1.198 + FILE_FLAG_RANDOM_ACCESS = 0x10000000,
1.199 + FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000,
1.200 + FILE_FLAG_DELETE_ON_CLOSE = 0x04000000,
1.201 + FILE_FLAG_BACKUP_SEMANTICS = 0x02000000,
1.202 + FILE_FLAG_POSIX_SEMANTICS = 0x01000000,
1.203 + FILE_FLAG_SESSION_AWARE = 0x00800000,
1.204 + FILE_FLAG_OPEN_REPARSE_POINT = 0x00200000,
1.205 + FILE_FLAG_OPEN_NO_RECALL = 0x00100000,
1.206 + FILE_FLAG_FIRST_PIPE_INSTANCE = 0x00080000
1.207 + }
1.208 +
1.209 +
1.210 +
1.211 +
1.212 +}
1.213 \ No newline at end of file