From 31125489ee4b9ed48f7f9f34b5c2d16ab615fb71 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Tue, 27 Oct 2020 08:49:57 -0600 Subject: [PATCH 1/2] Add Kernel32.CreateFile non-string overloads --- src/Kernel32/PublicAPI.Shipped.txt | 2 +- src/Kernel32/PublicAPI.Unshipped.txt | 4 ++++ src/Kernel32/storebanned/Kernel32.Helpers.cs | 19 +++++++++++++++++++ src/Kernel32/storebanned/Kernel32.cs | 4 ++-- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Kernel32/PublicAPI.Shipped.txt b/src/Kernel32/PublicAPI.Shipped.txt index 27b838dc..cd50bcdb 100644 --- a/src/Kernel32/PublicAPI.Shipped.txt +++ b/src/Kernel32/PublicAPI.Shipped.txt @@ -1274,7 +1274,7 @@ static extern PInvoke.Kernel32.CloseHandle(System.IntPtr hObject) -> bool static extern PInvoke.Kernel32.ConnectNamedPipe(PInvoke.Kernel32.SafeObjectHandle hNamedPipe, PInvoke.Kernel32.OVERLAPPED* lpOverlapped) -> bool static extern PInvoke.Kernel32.CopyMemory(void* Destination, void* Source, System.IntPtr Length) -> void static extern PInvoke.Kernel32.CreateConsoleScreenBuffer(PInvoke.Kernel32.ACCESS_MASK dwDesiredAccess, PInvoke.Kernel32.FileShare dwShareMode, PInvoke.Kernel32.SECURITY_ATTRIBUTES* lpSecurityAttributes, PInvoke.Kernel32.ConsoleScreenBufferFlag dwFlags, void* lpScreenBufferData) -> System.IntPtr -static extern PInvoke.Kernel32.CreateFile(string filename, PInvoke.Kernel32.ACCESS_MASK access, PInvoke.Kernel32.FileShare share, PInvoke.Kernel32.SECURITY_ATTRIBUTES* securityAttributes, PInvoke.Kernel32.CreationDisposition creationDisposition, PInvoke.Kernel32.CreateFileFlags flagsAndAttributes, PInvoke.Kernel32.SafeObjectHandle templateFile) -> PInvoke.Kernel32.SafeObjectHandle +static PInvoke.Kernel32.CreateFile(string filename, PInvoke.Kernel32.ACCESS_MASK access, PInvoke.Kernel32.FileShare share, PInvoke.Kernel32.SECURITY_ATTRIBUTES* securityAttributes, PInvoke.Kernel32.CreationDisposition creationDisposition, PInvoke.Kernel32.CreateFileFlags flagsAndAttributes, PInvoke.Kernel32.SafeObjectHandle templateFile) -> PInvoke.Kernel32.SafeObjectHandle static extern PInvoke.Kernel32.CreateJobObject(PInvoke.Kernel32.SECURITY_ATTRIBUTES* lpJobAttributes, string lpName) -> PInvoke.Kernel32.SafeObjectHandle static extern PInvoke.Kernel32.CreateMutex(PInvoke.Kernel32.SECURITY_ATTRIBUTES* lpMutexAttributes, bool bInitialOwner, string lpName) -> PInvoke.Kernel32.SafeObjectHandle static extern PInvoke.Kernel32.CreateNamedPipe(string lpName, PInvoke.Kernel32.PipeAccessMode dwOpenMode, PInvoke.Kernel32.PipeMode dwPipeMode, int nMaxInstances, int nOutBufferSize, int nInBufferSize, int nDefaultTimeOut, PInvoke.Kernel32.SECURITY_ATTRIBUTES* lpSecurityAttributes) -> PInvoke.Kernel32.SafeObjectHandle diff --git a/src/Kernel32/PublicAPI.Unshipped.txt b/src/Kernel32/PublicAPI.Unshipped.txt index a9133610..f9db8889 100644 --- a/src/Kernel32/PublicAPI.Unshipped.txt +++ b/src/Kernel32/PublicAPI.Unshipped.txt @@ -211,6 +211,9 @@ override PInvoke.Kernel32.SafePseudoConsoleHandle.IsInvalid.get -> bool override PInvoke.Kernel32.SafePseudoConsoleHandle.ReleaseHandle() -> bool static PInvoke.Kernel32.CONSOLE_READCONSOLE_CONTROL.Create() -> PInvoke.Kernel32.CONSOLE_READCONSOLE_CONTROL static PInvoke.Kernel32.CancelIoEx(PInvoke.Kernel32.SafeObjectHandle hFile, System.Threading.NativeOverlapped* lpOverlapped) -> bool +static PInvoke.Kernel32.CreateFile(System.IntPtr filename, PInvoke.Kernel32.ACCESS_MASK access, PInvoke.Kernel32.FileShare share, System.IntPtr securityAttributes, PInvoke.Kernel32.CreationDisposition creationDisposition, PInvoke.Kernel32.CreateFileFlags flagsAndAttributes, PInvoke.Kernel32.SafeObjectHandle templateFile) -> PInvoke.Kernel32.SafeObjectHandle +static PInvoke.Kernel32.CreateFile(System.ReadOnlySpan filename, PInvoke.Kernel32.ACCESS_MASK access, PInvoke.Kernel32.FileShare share, PInvoke.Kernel32.SECURITY_ATTRIBUTES? securityAttributes, PInvoke.Kernel32.CreationDisposition creationDisposition, PInvoke.Kernel32.CreateFileFlags flagsAndAttributes, PInvoke.Kernel32.SafeObjectHandle templateFile) -> PInvoke.Kernel32.SafeObjectHandle +static PInvoke.Kernel32.CreateFile(char[] filename, PInvoke.Kernel32.ACCESS_MASK access, PInvoke.Kernel32.FileShare share, PInvoke.Kernel32.SECURITY_ATTRIBUTES? securityAttributes, PInvoke.Kernel32.CreationDisposition creationDisposition, PInvoke.Kernel32.CreateFileFlags flagsAndAttributes, PInvoke.Kernel32.SafeObjectHandle templateFile) -> PInvoke.Kernel32.SafeObjectHandle static PInvoke.Kernel32.CreateRemoteThread(System.IntPtr hProcess, PInvoke.Kernel32.SECURITY_ATTRIBUTES? lpThreadAttributes, System.UIntPtr dwStackSize, PInvoke.Kernel32.THREAD_START_ROUTINE lpStartAddress, System.IntPtr lpParameter, PInvoke.Kernel32.CreateThreadFlags dwCreationFlags, ref uint? lpThreadId) -> PInvoke.Kernel32.SafeObjectHandle static PInvoke.Kernel32.CreateRemoteThread(System.IntPtr hProcess, PInvoke.Kernel32.SECURITY_ATTRIBUTES? lpThreadAttributes, System.UIntPtr dwStackSize, PInvoke.Kernel32.THREAD_START_ROUTINE lpStartAddress, void* lpParameter, PInvoke.Kernel32.CreateThreadFlags dwCreationFlags, ref uint? lpThreadId) -> PInvoke.Kernel32.SafeObjectHandle static PInvoke.Kernel32.CreateRemoteThread(System.IntPtr hProcess, System.IntPtr lpThreadAttributes, System.UIntPtr dwStackSize, PInvoke.Kernel32.THREAD_START_ROUTINE lpStartAddress, System.IntPtr lpParameter, PInvoke.Kernel32.CreateThreadFlags dwCreationFlags, System.IntPtr lpThreadId) -> PInvoke.Kernel32.SafeObjectHandle @@ -261,6 +264,7 @@ static PInvoke.Kernel32.SetFilePointer(PInvoke.Kernel32.SafeObjectHandle hFile, static PInvoke.Kernel32.SetFilePointer(PInvoke.Kernel32.SafeObjectHandle hFile, int lDistanceToMove, ref int? lpDistanceToMoveHigh, System.IO.SeekOrigin dwMoveMethod) -> int static PInvoke.Kernel32.SetProcessInformation(PInvoke.Kernel32.SafeObjectHandle hProcess, PInvoke.Kernel32.PROCESS_INFORMATION_CLASS ProcessInformationClass, System.IntPtr ProcessInformation, uint ProcessInformationSize) -> bool static PInvoke.Kernel32.WriteFile(PInvoke.Kernel32.SafeObjectHandle hFile, void* lpBuffer, int nNumberOfBytesToWrite, int* lpNumberOfBytesWritten, System.Threading.NativeOverlapped* lpOverlapped) -> bool +static extern PInvoke.Kernel32.CreateFile(char* filename, PInvoke.Kernel32.ACCESS_MASK access, PInvoke.Kernel32.FileShare share, PInvoke.Kernel32.SECURITY_ATTRIBUTES* securityAttributes, PInvoke.Kernel32.CreationDisposition creationDisposition, PInvoke.Kernel32.CreateFileFlags flagsAndAttributes, PInvoke.Kernel32.SafeObjectHandle templateFile) -> PInvoke.Kernel32.SafeObjectHandle static extern PInvoke.Kernel32.CreatePseudoConsole(PInvoke.COORD size, PInvoke.Kernel32.SafeObjectHandle hInput, PInvoke.Kernel32.SafeObjectHandle hOutput, PInvoke.Kernel32.CreatePseudoConsoleFlags dwFlags, out PInvoke.Kernel32.SafePseudoConsoleHandle phPC) -> PInvoke.HResult static extern PInvoke.Kernel32.CreateRemoteThread(System.IntPtr hProcess, PInvoke.Kernel32.SECURITY_ATTRIBUTES* lpThreadAttributes, System.UIntPtr dwStackSize, PInvoke.Kernel32.THREAD_START_ROUTINE lpStartAddress, void* lpParameter, PInvoke.Kernel32.CreateThreadFlags dwCreationFlags, uint* lpThreadId) -> PInvoke.Kernel32.SafeObjectHandle static extern PInvoke.Kernel32.CreateRemoteThreadEx(System.IntPtr hProcess, PInvoke.Kernel32.SECURITY_ATTRIBUTES* lpThreadAttributes, System.UIntPtr dwStackSize, PInvoke.Kernel32.THREAD_START_ROUTINE lpStartAddress, void* lpParameter, PInvoke.Kernel32.CreateThreadFlags dwCreationFlags, PInvoke.Kernel32.PROC_THREAD_ATTRIBUTE_LIST* lpAttributeList, uint* lpThreadId) -> PInvoke.Kernel32.SafeObjectHandle diff --git a/src/Kernel32/storebanned/Kernel32.Helpers.cs b/src/Kernel32/storebanned/Kernel32.Helpers.cs index 7b954ab6..d9f7534e 100644 --- a/src/Kernel32/storebanned/Kernel32.Helpers.cs +++ b/src/Kernel32/storebanned/Kernel32.Helpers.cs @@ -16,6 +16,25 @@ namespace PInvoke /// public static partial class Kernel32 { + /// + /// + /// This should be removed as part of delivering the string-overload codegen feature. + /// + public static unsafe SafeObjectHandle CreateFile( + string filename, + ACCESS_MASK access, + FileShare share, + [Friendly(FriendlyFlags.In | FriendlyFlags.Optional)] SECURITY_ATTRIBUTES* securityAttributes, + CreationDisposition creationDisposition, + CreateFileFlags flagsAndAttributes, + SafeObjectHandle templateFile) + { + fixed (char* pFileName = filename) + { + return CreateFile(pFileName, access, share, securityAttributes, creationDisposition, flagsAndAttributes, templateFile); + } + } + /// Retrieves information about the first process encountered in a system snapshot. /// /// A handle to the snapshot returned from a previous call to the diff --git a/src/Kernel32/storebanned/Kernel32.cs b/src/Kernel32/storebanned/Kernel32.cs index c904e9e2..ac34c31a 100644 --- a/src/Kernel32/storebanned/Kernel32.cs +++ b/src/Kernel32/storebanned/Kernel32.cs @@ -695,7 +695,7 @@ public static unsafe extern void DeleteProcThreadAttributeList( /// To perform this operation as a transacted operation, which results in a handle that can be used for transacted I/O, use the CreateFileTransacted function. /// /// - /// The name of the file or device to be created or opened. You may use either forward slashes (/) or backslashes (\) in this name. + /// The name of the file or device to be created or opened as a UTF-16, null-terminated array of characters. You may use either forward slashes (/) or backslashes (\) in this name. /// In the ANSI version of this function, the name is limited to characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming Files, Paths, and Namespaces. /// For information on special device names, see Defining an MS-DOS Device Name. /// To create a file stream, specify the name of the file, a colon, and then the name of the stream.For more information, see File Streams. @@ -747,7 +747,7 @@ public static unsafe extern void DeleteProcThreadAttributeList( /// [DllImport(api_ms_win_core_file_l1_2_0, CharSet = CharSet.Unicode, SetLastError = true)] public static extern unsafe SafeObjectHandle CreateFile( - string filename, + [Friendly(FriendlyFlags.In | FriendlyFlags.Array)] char* filename, ACCESS_MASK access, FileShare share, [Friendly(FriendlyFlags.In | FriendlyFlags.Optional)] SECURITY_ATTRIBUTES* securityAttributes, From 21a116831a3dfe5528ae4a39b83cdf634d212ff2 Mon Sep 17 00:00:00 2001 From: Andrew Arnott Date: Tue, 27 Oct 2020 08:50:26 -0600 Subject: [PATCH 2/2] Fix TargetFrameworks that specified `netstandard2.0` twice --- src/DwmApi/DwmApi.csproj | 2 +- src/NCrypt/NCrypt.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DwmApi/DwmApi.csproj b/src/DwmApi/DwmApi.csproj index 300a9669..6c58a75b 100644 --- a/src/DwmApi/DwmApi.csproj +++ b/src/DwmApi/DwmApi.csproj @@ -1,6 +1,6 @@  - $(PlatformAndPortableFrameworks);netstandard2.0 + $(PlatformAndPortableFrameworks) true diff --git a/src/NCrypt/NCrypt.csproj b/src/NCrypt/NCrypt.csproj index 2fe732c6..6d802517 100644 --- a/src/NCrypt/NCrypt.csproj +++ b/src/NCrypt/NCrypt.csproj @@ -1,6 +1,6 @@  - $(PlatformAndPortableFrameworks);netstandard2.0 + $(PlatformAndPortableFrameworks) $(NoWarn);RS0026;RS0027