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