1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 
5 using Microsoft.Win32.SafeHandles;
6 using System;
7 using System.IO;
8 using System.Runtime.InteropServices;
9 
10 internal partial class Interop
11 {
12     internal partial class Kernel32
13     {
14         // https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858.aspx
15         /// <summary>
16         /// WARNING: The private methods do not implicitly handle long paths. Use CreateFile.
17         /// </summary>
18         [DllImport(Libraries.Kernel32, EntryPoint = "CreateFileW", SetLastError = true, CharSet = CharSet.Unicode, BestFitMapping = false, ExactSpelling = true)]
CreateFilePrivate( string lpFileName, int dwDesiredAccess, FileShare dwShareMode, SECURITY_ATTRIBUTES* securityAttrs, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile)19         private unsafe static extern IntPtr CreateFilePrivate(
20             string lpFileName,
21             int dwDesiredAccess,
22             FileShare dwShareMode,
23             SECURITY_ATTRIBUTES* securityAttrs,
24             FileMode dwCreationDisposition,
25             int dwFlagsAndAttributes,
26             IntPtr hTemplateFile);
27 
CreateFile( string lpFileName, int dwDesiredAccess, FileShare dwShareMode, ref SECURITY_ATTRIBUTES securityAttrs, FileMode dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile)28         internal unsafe static SafeFileHandle CreateFile(
29             string lpFileName,
30             int dwDesiredAccess,
31             FileShare dwShareMode,
32             ref SECURITY_ATTRIBUTES securityAttrs,
33             FileMode dwCreationDisposition,
34             int dwFlagsAndAttributes,
35             IntPtr hTemplateFile)
36         {
37             lpFileName = PathInternal.EnsureExtendedPrefixOverMaxPath(lpFileName);
38             fixed (SECURITY_ATTRIBUTES* sa = &securityAttrs)
39             {
40                 IntPtr handle = CreateFilePrivate(lpFileName, dwDesiredAccess, dwShareMode, sa, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
41                 try
42                 {
43                     return new SafeFileHandle(handle, ownsHandle: true);
44                 }
45                 catch
46                 {
47                     CloseHandle(handle);
48                     throw;
49                 }
50             }
51         }
52 
CreateFile( string lpFileName, int dwDesiredAccess, FileShare dwShareMode, FileMode dwCreationDisposition, int dwFlagsAndAttributes)53         internal unsafe static SafeFileHandle CreateFile(
54             string lpFileName,
55             int dwDesiredAccess,
56             FileShare dwShareMode,
57             FileMode dwCreationDisposition,
58             int dwFlagsAndAttributes)
59         {
60             IntPtr handle = CreateFile_IntPtr(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, dwFlagsAndAttributes);
61             try
62             {
63                 return new SafeFileHandle(handle, ownsHandle: true);
64             }
65             catch
66             {
67                 CloseHandle(handle);
68                 throw;
69             }
70         }
71 
CreateFile_IntPtr( string lpFileName, int dwDesiredAccess, FileShare dwShareMode, FileMode dwCreationDisposition, int dwFlagsAndAttributes)72         internal unsafe static IntPtr CreateFile_IntPtr(
73             string lpFileName,
74             int dwDesiredAccess,
75             FileShare dwShareMode,
76             FileMode dwCreationDisposition,
77             int dwFlagsAndAttributes)
78         {
79             lpFileName = PathInternal.EnsureExtendedPrefixOverMaxPath(lpFileName);
80             return CreateFilePrivate(lpFileName, dwDesiredAccess, dwShareMode, null, dwCreationDisposition, dwFlagsAndAttributes, IntPtr.Zero);
81         }
82     }
83 }
84