From 78cbd968cabc2bd6c84bfe6a0a240658e564a6d5 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Wed, 13 Feb 2019 13:43:13 -0500 Subject: [PATCH] Replace Win32Native.cs with Interop files (#22540) * Replace Win32Native.cs with Interop files - Replace Win32Native usage with existing Interop.* calls where they already existed - Moved Win32Native.* to their own files otherwise, and changed call sites - Left a stub Win32Native in place to handle some emitted IL from the runtime. The current infrastructure doesn't support targeting nested types, like Interop.Kernel32, and fixing that would be more involved. * Remove Win32Native.cs Replace emitted calls with ones to Marshal. * Fix incorrectly changed field * Fix Kernel32 -> Ole32 --- .../System.Private.CoreLib.csproj | 10 +- .../Kernel32/Interop.GetCurrentProcessId.cs | 14 ++ .../Windows/Kernel32/Interop.GetCurrentThreadId.cs | 15 ++ .../Windows/Kernel32/Interop.GetStdHandle.cs | 19 +++ .../Windows/Kernel32/Interop.GetVersionExW.cs | 16 -- .../Interop/Windows/Kernel32/Interop.LocalAlloc.cs | 24 +++ .../Windows/Kernel32/Interop.OSVERSIONINFOEX.cs | 27 +++ .../Kernel32/Interop.QueryUnbiasedInterruptTime.cs | 14 ++ .../Kernel32/Interop.VerSetConditionMask.cs | 14 ++ .../Windows/Kernel32/Interop.VerifyVersionExW.cs | 20 +++ .../Windows/Ole32/Interop.CoTaskMemAlloc.cs | 21 +++ .../OleAut32/Interop.SysAllocStringByteLen.cs | 15 ++ .../shared/System.Private.CoreLib.Shared.projitems | 15 +- .../System/Diagnostics/Tracing/EventProvider.cs | 2 +- .../System/Diagnostics/Tracing/EventSource.cs | 4 +- .../System/Diagnostics/Tracing/StubEnvironment.cs | 4 +- .../shared/System/Threading/Mutex.Windows.cs | 1 - src/System.Private.CoreLib/src/Internal/Console.cs | 16 +- .../src/Interop/Unix/Interop.Libraries.cs | 1 + .../src/Microsoft/Win32/UnsafeNativeMethods.cs | 14 +- .../src/Microsoft/Win32/Win32Native.cs | 186 --------------------- .../Diagnostics/Eventing/EventPipeController.cs | 4 +- .../src/System/Environment.CoreCLR.cs | 16 +- .../Runtime/InteropServices/Marshal.CoreCLR.cs | 23 ++- .../Runtime/InteropServices/NativeLibrary.cs | 5 - .../src/System/StubHelpers.cs | 28 ++-- .../src/System/Threading/ThreadPool.CoreCLR.cs | 3 +- .../src/System/Threading/Timer.CoreCLR.cs | 2 +- .../src/System/Threading/WaitHandle.cs | 22 +-- src/vm/ilmarshalers.cpp | 8 +- src/vm/ilmarshalers.h | 12 +- src/vm/mscorlib.h | 5 +- 32 files changed, 271 insertions(+), 309 deletions(-) create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetCurrentProcessId.cs create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetCurrentThreadId.cs create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetStdHandle.cs create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.LocalAlloc.cs create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.OSVERSIONINFOEX.cs create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.QueryUnbiasedInterruptTime.cs create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.VerSetConditionMask.cs create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.VerifyVersionExW.cs create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/Ole32/Interop.CoTaskMemAlloc.cs create mode 100644 src/System.Private.CoreLib/shared/Interop/Windows/OleAut32/Interop.SysAllocStringByteLen.cs delete mode 100644 src/System.Private.CoreLib/src/Microsoft/Win32/Win32Native.cs diff --git a/src/System.Private.CoreLib/System.Private.CoreLib.csproj b/src/System.Private.CoreLib/System.Private.CoreLib.csproj index e785601..df251e5 100644 --- a/src/System.Private.CoreLib/System.Private.CoreLib.csproj +++ b/src/System.Private.CoreLib/System.Private.CoreLib.csproj @@ -118,7 +118,6 @@ - @@ -276,6 +275,15 @@ + + + + + + + + + diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetCurrentProcessId.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetCurrentProcessId.cs new file mode 100644 index 0000000..a042bfe --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetCurrentProcessId.cs @@ -0,0 +1,14 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Kernel32 + { + [DllImport(Libraries.Kernel32)] + internal extern static uint GetCurrentProcessId(); + } +} diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetCurrentThreadId.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetCurrentThreadId.cs new file mode 100644 index 0000000..d456db7 --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetCurrentThreadId.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Kernel32 + { + [DllImport(Libraries.Kernel32, ExactSpelling = true)] + public static extern int GetCurrentThreadId(); + } +} diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetStdHandle.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetStdHandle.cs new file mode 100644 index 0000000..9637ca9 --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetStdHandle.cs @@ -0,0 +1,19 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Kernel32 + { + internal const int STD_INPUT_HANDLE = -10; + internal const int STD_OUTPUT_HANDLE = -11; + internal const int STD_ERROR_HANDLE = -12; + + [DllImport(Libraries.Kernel32, SetLastError = true)] + internal static extern IntPtr GetStdHandle(int nStdHandle); + } +} diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetVersionExW.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetVersionExW.cs index 11c9143..13a997a 100644 --- a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetVersionExW.cs +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.GetVersionExW.cs @@ -10,21 +10,5 @@ internal partial class Interop { [DllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, SetLastError = true)] internal static extern bool GetVersionExW(ref OSVERSIONINFOEX osvi); - - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] - internal unsafe struct OSVERSIONINFOEX - { - public int dwOSVersionInfoSize; - public int dwMajorVersion; - public int dwMinorVersion; - public int dwBuildNumber; - public int dwPlatformId; - public fixed char szCSDVersion[128]; - public ushort wServicePackMajor; - public ushort wServicePackMinor; - public ushort wSuiteMask; - public byte wProductType; - public byte wReserved; - } } } diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.LocalAlloc.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.LocalAlloc.cs new file mode 100644 index 0000000..9c74adb --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.LocalAlloc.cs @@ -0,0 +1,24 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Kernel32 + { + internal const uint LMEM_FIXED = 0x0000; + internal const uint LMEM_MOVEABLE = 0x0002; + + [DllImport(Libraries.Kernel32)] + internal static extern IntPtr LocalAlloc(uint uFlags, UIntPtr uBytes); + + [DllImport(Libraries.Kernel32)] + internal static extern IntPtr LocalReAlloc(IntPtr hMem, IntPtr uBytes, uint uFlags); + + [DllImport(Libraries.Kernel32, SetLastError = true)] + internal static extern IntPtr LocalFree(IntPtr hMem); + } +} diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.OSVERSIONINFOEX.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.OSVERSIONINFOEX.cs new file mode 100644 index 0000000..1bfe00c --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.OSVERSIONINFOEX.cs @@ -0,0 +1,27 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Kernel32 + { + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + internal unsafe struct OSVERSIONINFOEX + { + public int dwOSVersionInfoSize; + public int dwMajorVersion; + public int dwMinorVersion; + public int dwBuildNumber; + public int dwPlatformId; + public fixed char szCSDVersion[128]; + public ushort wServicePackMajor; + public ushort wServicePackMinor; + public ushort wSuiteMask; + public byte wProductType; + public byte wReserved; + } + } +} diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.QueryUnbiasedInterruptTime.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.QueryUnbiasedInterruptTime.cs new file mode 100644 index 0000000..87ca7fd --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.QueryUnbiasedInterruptTime.cs @@ -0,0 +1,14 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Kernel32 + { + [DllImport(Libraries.Kernel32, SetLastError = true)] + internal static extern bool QueryUnbiasedInterruptTime(out ulong UnbiasedTime); + } +} diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.VerSetConditionMask.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.VerSetConditionMask.cs new file mode 100644 index 0000000..385e48a --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.VerSetConditionMask.cs @@ -0,0 +1,14 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Kernel32 + { + [DllImport(Libraries.Kernel32)] + internal static extern ulong VerSetConditionMask(ulong ConditionMask, uint TypeMask, byte Condition); + } +} diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.VerifyVersionExW.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.VerifyVersionExW.cs new file mode 100644 index 0000000..5c2471c --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Kernel32/Interop.VerifyVersionExW.cs @@ -0,0 +1,20 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Kernel32 + { + internal const byte VER_GREATER_EQUAL = 0x3; + internal const uint VER_MAJORVERSION = 0x0000002; + internal const uint VER_MINORVERSION = 0x0000001; + internal const uint VER_SERVICEPACKMAJOR = 0x0000020; + internal const uint VER_SERVICEPACKMINOR = 0x0000010; + + [DllImport(Libraries.Kernel32)] + internal static extern bool VerifyVersionInfoW(ref OSVERSIONINFOEX lpVersionInfo, uint dwTypeMask, ulong dwlConditionMask); + } +} diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/Ole32/Interop.CoTaskMemAlloc.cs b/src/System.Private.CoreLib/shared/Interop/Windows/Ole32/Interop.CoTaskMemAlloc.cs new file mode 100644 index 0000000..9119ee7 --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/Ole32/Interop.CoTaskMemAlloc.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class Ole32 + { + [DllImport(Libraries.Ole32)] + internal static extern IntPtr CoTaskMemAlloc(UIntPtr cb); + + [DllImport(Libraries.Ole32)] + internal static extern IntPtr CoTaskMemRealloc(IntPtr pv, UIntPtr cb); + + [DllImport(Libraries.Ole32)] + internal static extern void CoTaskMemFree(IntPtr ptr); + } +} diff --git a/src/System.Private.CoreLib/shared/Interop/Windows/OleAut32/Interop.SysAllocStringByteLen.cs b/src/System.Private.CoreLib/shared/Interop/Windows/OleAut32/Interop.SysAllocStringByteLen.cs new file mode 100644 index 0000000..5249689 --- /dev/null +++ b/src/System.Private.CoreLib/shared/Interop/Windows/OleAut32/Interop.SysAllocStringByteLen.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System; +using System.Runtime.InteropServices; + +internal partial class Interop +{ + internal partial class OleAut32 + { + [DllImport(Libraries.OleAut32)] + internal static extern IntPtr SysAllocStringByteLen(byte[] str, uint len); + } +} diff --git a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems index 21dc2f9..79be842 100644 --- a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems +++ b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems @@ -991,15 +991,11 @@ - - - - @@ -1029,7 +1025,6 @@ - @@ -1076,19 +1071,25 @@ + + + + - + + - + + diff --git a/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/EventProvider.cs b/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/EventProvider.cs index 1b82e6f..c886758 100644 --- a/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/EventProvider.cs +++ b/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/EventProvider.cs @@ -494,7 +494,7 @@ namespace System.Diagnostics.Tracing var providerInfos = (UnsafeNativeMethods.ManifestEtw.TRACE_GUID_INFO*)buffer; var providerInstance = (UnsafeNativeMethods.ManifestEtw.TRACE_PROVIDER_INSTANCE_INFO*)&providerInfos[1]; - int processId = unchecked((int)Win32Native.GetCurrentProcessId()); + int processId = unchecked((int)Interop.Kernel32.GetCurrentProcessId()); // iterate over the instances of the EventProvider in all processes for (int i = 0; i < providerInfos->InstanceCount; i++) { diff --git a/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/EventSource.cs b/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/EventSource.cs index d653e95..e4247fe 100644 --- a/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/EventSource.cs +++ b/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/EventSource.cs @@ -455,7 +455,7 @@ namespace System.Diagnostics.Tracing { get { - int threadID = Win32Native.GetCurrentThreadId(); + int threadID = Interop.Kernel32.GetCurrentThreadId(); // Managed thread IDs are more aggressively re-used than native thread IDs, // so we'll use the latter... @@ -2742,7 +2742,7 @@ namespace System.Diagnostics.Tracing // for non-BCL EventSource we must assert SecurityPermission new SecurityPermission(PermissionState.Unrestricted).Assert(); #endif - s_currentPid = Win32Native.GetCurrentProcessId(); + s_currentPid = Interop.Kernel32.GetCurrentProcessId(); } } diff --git a/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/StubEnvironment.cs b/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/StubEnvironment.cs index 9773491..981a100 100644 --- a/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/StubEnvironment.cs +++ b/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/StubEnvironment.cs @@ -325,13 +325,13 @@ namespace Microsoft.Reflection } #if ES_BUILD_STANDALONE -namespace Microsoft.Win32 +internal static partial class Interop { using System.Runtime.InteropServices; using System.Security; [SuppressUnmanagedCodeSecurityAttribute()] - internal static class Win32Native + internal static partial class Kernel32 { [DllImport("kernel32.dll", CharSet = CharSet.Auto)] public static extern int GetCurrentThreadId(); diff --git a/src/System.Private.CoreLib/shared/System/Threading/Mutex.Windows.cs b/src/System.Private.CoreLib/shared/System/Threading/Mutex.Windows.cs index 212eb9d..c40ab94 100644 --- a/src/System.Private.CoreLib/shared/System/Threading/Mutex.Windows.cs +++ b/src/System.Private.CoreLib/shared/System/Threading/Mutex.Windows.cs @@ -78,7 +78,6 @@ namespace System.Threading if (Interop.Errors.ERROR_INVALID_HANDLE == errorCode) return OpenExistingResult.NameInvalid; - // this is for passed through Win32Native Errors throw Win32Marshal.GetExceptionForWin32Error(errorCode, name); } diff --git a/src/System.Private.CoreLib/src/Internal/Console.cs b/src/System.Private.CoreLib/src/Internal/Console.cs index a2a323f..ecb751f 100644 --- a/src/System.Private.CoreLib/src/Internal/Console.cs +++ b/src/System.Private.CoreLib/src/Internal/Console.cs @@ -4,8 +4,6 @@ using System; using System.Text; -using System.Security; -using Microsoft.Win32; using Microsoft.Win32.SafeHandles; namespace Internal @@ -18,27 +16,21 @@ namespace Internal public static class Console { private static readonly SafeFileHandle _outputHandle = - new SafeFileHandle(Win32Native.GetStdHandle(Win32Native.STD_OUTPUT_HANDLE), false); + new SafeFileHandle(Interop.Kernel32.GetStdHandle(Interop.Kernel32.STD_OUTPUT_HANDLE), ownsHandle: false); public static unsafe void Write(string s) { byte[] bytes = Encoding.UTF8.GetBytes(s); - fixed (byte* pBytes = bytes) { - int bytesWritten; - Win32Native.WriteFile(_outputHandle, pBytes, bytes.Length, out bytesWritten, IntPtr.Zero); + Interop.Kernel32.WriteFile(_outputHandle, pBytes, bytes.Length, out _, IntPtr.Zero); } } - public static void WriteLine(string s) - { + public static void WriteLine(string s) => Write(s + Environment.NewLine); - } - public static void WriteLine() - { + public static void WriteLine() => Write(Environment.NewLine); - } } } diff --git a/src/System.Private.CoreLib/src/Interop/Unix/Interop.Libraries.cs b/src/System.Private.CoreLib/src/Interop/Unix/Interop.Libraries.cs index 31df8c7..da7cbf2 100644 --- a/src/System.Private.CoreLib/src/Interop/Unix/Interop.Libraries.cs +++ b/src/System.Private.CoreLib/src/Interop/Unix/Interop.Libraries.cs @@ -10,5 +10,6 @@ internal static partial class Interop internal const string User32 = "libcoreclr"; internal const string Ole32 = "libcoreclr"; internal const string OleAut32 = "libcoreclr"; + internal const string Advapi32 = "libcoreclr"; } } diff --git a/src/System.Private.CoreLib/src/Microsoft/Win32/UnsafeNativeMethods.cs b/src/System.Private.CoreLib/src/Microsoft/Win32/UnsafeNativeMethods.cs index 4ddf566..8e0a682 100644 --- a/src/System.Private.CoreLib/src/Microsoft/Win32/UnsafeNativeMethods.cs +++ b/src/System.Private.CoreLib/src/Microsoft/Win32/UnsafeNativeMethods.cs @@ -56,7 +56,7 @@ namespace Microsoft.Win32 // // Registration APIs // - [DllImport(Win32Native.ADVAPI32, ExactSpelling = true, EntryPoint = "EventRegister", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] + [DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, EntryPoint = "EventRegister", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] internal static extern unsafe uint EventRegister( [In] ref Guid providerId, [In]EtwEnableCallback enableCallback, @@ -65,10 +65,10 @@ namespace Microsoft.Win32 ); // - [DllImport(Win32Native.ADVAPI32, ExactSpelling = true, EntryPoint = "EventUnregister", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] + [DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, EntryPoint = "EventUnregister", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] internal static extern uint EventUnregister([In] long registrationHandle); - [DllImport(Win32Native.ADVAPI32, ExactSpelling = true, EntryPoint = "EventWriteString", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] + [DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, EntryPoint = "EventWriteString", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] internal static extern int EventWriteString( [In] long registrationHandle, [In] byte level, @@ -106,7 +106,7 @@ namespace Microsoft.Win32 return HResult; } - [DllImport(Win32Native.ADVAPI32, ExactSpelling = true, EntryPoint = "EventWriteTransfer", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] + [DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, EntryPoint = "EventWriteTransfer", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] private static extern int EventWriteTransfer( [In] long registrationHandle, [In] ref EventDescriptor eventDescriptor, @@ -125,7 +125,7 @@ namespace Microsoft.Win32 EVENT_ACTIVITY_CTRL_CREATE_SET_ID = 5 }; - [DllImport(Win32Native.ADVAPI32, ExactSpelling = true, EntryPoint = "EventActivityIdControl", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] + [DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, EntryPoint = "EventActivityIdControl", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] internal static extern int EventActivityIdControl([In] ActivityControl ControlCode, [In][Out] ref Guid ActivityId); internal enum EVENT_INFO_CLASS @@ -135,7 +135,7 @@ namespace Microsoft.Win32 SetTraits, } - [DllImport(Win32Native.ADVAPI32, ExactSpelling = true, EntryPoint = "EventSetInformation", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] + [DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, EntryPoint = "EventSetInformation", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] internal static extern int EventSetInformation( [In] long registrationHandle, [In] EVENT_INFO_CLASS informationClass, @@ -178,7 +178,7 @@ namespace Microsoft.Win32 public long MatchAllKeyword; }; - [DllImport(Win32Native.ADVAPI32, ExactSpelling = true, EntryPoint = "EnumerateTraceGuidsEx", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] + [DllImport(Interop.Libraries.Advapi32, ExactSpelling = true, EntryPoint = "EnumerateTraceGuidsEx", CharSet = System.Runtime.InteropServices.CharSet.Unicode)] internal static extern int EnumerateTraceGuidsEx( TRACE_QUERY_INFO_CLASS TraceQueryInfoClass, void* InBuffer, diff --git a/src/System.Private.CoreLib/src/Microsoft/Win32/Win32Native.cs b/src/System.Private.CoreLib/src/Microsoft/Win32/Win32Native.cs deleted file mode 100644 index 9d2cc9a..0000000 --- a/src/System.Private.CoreLib/src/Microsoft/Win32/Win32Native.cs +++ /dev/null @@ -1,186 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -/*============================================================ -** -** -** -** Purpose: CoreCLR wrapper for Win32, either the native -** operations or the Unix PAL implementation of them. -** -** -===========================================================*/ -/* - * Notes to PInvoke users: Getting the syntax exactly correct is crucial, and - * more than a little confusing. Here's some guidelines. - * - * For handles, you should use a SafeHandle subclass specific to your handle - * type. For files, we have the following set of interesting definitions: - * - * [DllImport(Interop.Libraries.Kernel32, SetLastError=true, CharSet=CharSet.Auto, BestFitMapping=false)] - * private static extern SafeFileHandle CreateFile(...); - * - * [DllImport(Interop.Libraries.Kernel32, SetLastError=true)] - * internal static extern unsafe int ReadFile(SafeFileHandle handle, ...); - * - * [DllImport(Interop.Libraries.Kernel32, SetLastError=true)] - * internal static extern bool CloseHandle(IntPtr handle); - * - * P/Invoke will create the SafeFileHandle instance for you and assign the - * return value from CreateFile into the handle atomically. When we call - * ReadFile, P/Invoke will increment a ref count, make the call, then decrement - * it (preventing handle recycling vulnerabilities). Then SafeFileHandle's - * ReleaseHandle method will call CloseHandle, passing in the handle field - * as an IntPtr. - * - * If for some reason you cannot use a SafeHandle subclass for your handles, - * then use IntPtr as the handle type (or possibly HandleRef - understand when - * to use GC.KeepAlive). If your code will run in SQL Server (or any other - * long-running process that can't be recycled easily), use a constrained - * execution region to prevent thread aborts while allocating your - * handle, and consider making your handle wrapper subclass - * CriticalFinalizerObject to ensure you can free the handle. As you can - * probably guess, SafeHandle will save you a lot of headaches if your code - * needs to be robust to thread aborts and OOM. - * - * - * If you have a method that takes a native struct, you have two options for - * declaring that struct. You can make it a value type ('struct' in CSharp), - * or a reference type ('class'). This choice doesn't seem very interesting, - * but your function prototype must use different syntax depending on your - * choice. For example, if your native method is prototyped as such: - * - * bool GetVersionEx(OSVERSIONINFO & lposvi); - * - * - * you must use EITHER THIS OR THE NEXT syntax: - * - * [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)] - * internal struct OSVERSIONINFO { ... } - * - * [DllImport(Interop.Libraries.Kernel32, CharSet=CharSet.Auto)] - * internal static extern bool GetVersionEx(ref OSVERSIONINFO lposvi); - * - * OR: - * - * [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)] - * internal class OSVERSIONINFO { ... } - * - * [DllImport(Interop.Libraries.Kernel32, CharSet=CharSet.Auto)] - * internal static extern bool GetVersionEx([In, Out] OSVERSIONINFO lposvi); - * - * Note that classes require being marked as [In, Out] while value types must - * be passed as ref parameters. - * - * Also note the CharSet.Auto on GetVersionEx - while it does not take a String - * as a parameter, the OSVERSIONINFO contains an embedded array of TCHARs, so - * the size of the struct varies on different platforms, and there's a - * GetVersionExA & a GetVersionExW. Also, the OSVERSIONINFO struct has a sizeof - * field so the OS can ensure you've passed in the correctly-sized copy of an - * OSVERSIONINFO. You must explicitly set this using Marshal.SizeOf(Object); - * - * For security reasons, if you're making a P/Invoke method to a Win32 method - * that takes an ANSI String and that String is the name of some resource you've - * done a security check on (such as a file name), you want to disable best fit - * mapping in WideCharToMultiByte. Do this by setting BestFitMapping=false - * in your DllImportAttribute. - */ - -namespace Microsoft.Win32 -{ - using System; - using System.Runtime.InteropServices; - using Microsoft.Win32.SafeHandles; - - /** - * Win32 encapsulation for System.Private.CoreLib. - */ - internal static class Win32Native - { - internal const int LMEM_FIXED = 0x0000; - internal const int LMEM_ZEROINIT = 0x0040; - internal const int LPTR = (LMEM_FIXED | LMEM_ZEROINIT); - - [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] - internal unsafe struct OSVERSIONINFOEX - { - internal int dwOSVersionInfoSize; - internal int dwMajorVersion; - internal int dwMinorVersion; - internal int dwBuildNumber; - internal int dwPlatformId; - internal fixed char szCSDVersion[128]; - internal ushort wServicePackMajor; - internal ushort wServicePackMinor; - internal short wSuiteMask; - internal byte wProductType; - internal byte wReserved; - } - - internal const string ADVAPI32 = "advapi32.dll"; - - [DllImport(Interop.Libraries.Kernel32, EntryPoint = "LocalAlloc")] - internal static extern IntPtr LocalAlloc_NoSafeHandle(int uFlags, UIntPtr sizetdwBytes); - - [DllImport(Interop.Libraries.Kernel32, SetLastError = true)] - internal static extern IntPtr LocalFree(IntPtr handle); - - [DllImport(Interop.Libraries.Kernel32)] - internal static extern IntPtr LocalReAlloc(IntPtr handle, IntPtr sizetcbBytes, int uFlags); - - [DllImport(Interop.Libraries.OleAut32, CharSet = CharSet.Unicode)] - internal static extern IntPtr SysAllocStringLen(string src, int len); // BSTR - - [DllImport(Interop.Libraries.OleAut32)] - internal static extern uint SysStringLen(IntPtr bstr); - - [DllImport(Interop.Libraries.OleAut32)] - internal static extern void SysFreeString(IntPtr bstr); - - [DllImport(Interop.Libraries.OleAut32)] - internal static extern IntPtr SysAllocStringByteLen(byte[] str, uint len); // BSTR - - [DllImport(Interop.Libraries.Kernel32, SetLastError = true)] - internal static extern unsafe int WriteFile(SafeFileHandle handle, byte* bytes, int numBytesToWrite, out int numBytesWritten, IntPtr mustBeZero); - - internal static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1); // WinBase.h - - // Note, these are #defines used to extract handles, and are NOT handles. - internal const int STD_INPUT_HANDLE = -10; - internal const int STD_OUTPUT_HANDLE = -11; - internal const int STD_ERROR_HANDLE = -12; - - [DllImport(Interop.Libraries.Kernel32, SetLastError = true)] - internal static extern IntPtr GetStdHandle(int nStdHandle); // param is NOT a handle, but it returns one! - - [DllImport(Interop.Libraries.Kernel32, CharSet = CharSet.Auto)] - internal static extern int GetCurrentThreadId(); - - [DllImport(Interop.Libraries.Kernel32, CharSet = CharSet.Auto)] - internal static extern uint GetCurrentProcessId(); - - [DllImport(Interop.Libraries.Ole32)] - internal static extern IntPtr CoTaskMemAlloc(UIntPtr cb); - - [DllImport(Interop.Libraries.Ole32)] - internal static extern void CoTaskMemFree(IntPtr ptr); - - [DllImport(Interop.Libraries.Ole32)] - internal static extern IntPtr CoTaskMemRealloc(IntPtr pv, UIntPtr cb); - - [DllImport(Interop.Libraries.Kernel32, SetLastError = true)] - [return: MarshalAs(UnmanagedType.Bool)] - internal static extern bool QueryUnbiasedInterruptTime(out ulong UnbiasedTime); - - internal const byte VER_GREATER_EQUAL = 0x3; - internal const uint VER_MAJORVERSION = 0x0000002; - internal const uint VER_MINORVERSION = 0x0000001; - internal const uint VER_SERVICEPACKMAJOR = 0x0000020; - internal const uint VER_SERVICEPACKMINOR = 0x0000010; - [DllImport("kernel32.dll")] - internal static extern bool VerifyVersionInfoW(ref OSVERSIONINFOEX lpVersionInfo, uint dwTypeMask, ulong dwlConditionMask); - [DllImport("kernel32.dll")] - internal static extern ulong VerSetConditionMask(ulong dwlConditionMask, uint dwTypeBitMask, byte dwConditionMask); - } -} diff --git a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/EventPipeController.cs b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/EventPipeController.cs index ba469f1..21a9b92 100644 --- a/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/EventPipeController.cs +++ b/src/System.Private.CoreLib/src/System/Diagnostics/Eventing/EventPipeController.cs @@ -211,7 +211,7 @@ namespace System.Diagnostics.Tracing { // If set, bail out early if the specified process does not match the current process. int processID = Convert.ToInt32(strProcessID); - if (processID != Win32Native.GetCurrentProcessId()) + if (processID != Interop.Kernel32.GetCurrentProcessId()) { return null; } @@ -293,7 +293,7 @@ namespace System.Diagnostics.Tracing private static string BuildTraceFileName() { - return GetAppName() + "." + Win32Native.GetCurrentProcessId().ToString() + NetPerfFileExtension; + return GetAppName() + "." + Interop.Kernel32.GetCurrentProcessId().ToString() + NetPerfFileExtension; } private static string GetAppName() diff --git a/src/System.Private.CoreLib/src/System/Environment.CoreCLR.cs b/src/System.Private.CoreLib/src/System/Environment.CoreCLR.cs index 8d92a7e..d5fea8a 100644 --- a/src/System.Private.CoreLib/src/System/Environment.CoreCLR.cs +++ b/src/System.Private.CoreLib/src/System/Environment.CoreCLR.cs @@ -116,24 +116,24 @@ namespace System private static bool GetIsWindows8OrAbove() { - ulong conditionMask = Win32Native.VerSetConditionMask(0, Win32Native.VER_MAJORVERSION, Win32Native.VER_GREATER_EQUAL); - conditionMask = Win32Native.VerSetConditionMask(conditionMask, Win32Native.VER_MINORVERSION, Win32Native.VER_GREATER_EQUAL); - conditionMask = Win32Native.VerSetConditionMask(conditionMask, Win32Native.VER_SERVICEPACKMAJOR, Win32Native.VER_GREATER_EQUAL); - conditionMask = Win32Native.VerSetConditionMask(conditionMask, Win32Native.VER_SERVICEPACKMINOR, Win32Native.VER_GREATER_EQUAL); + ulong conditionMask = Interop.Kernel32.VerSetConditionMask(0, Interop.Kernel32.VER_MAJORVERSION, Interop.Kernel32.VER_GREATER_EQUAL); + conditionMask = Interop.Kernel32.VerSetConditionMask(conditionMask, Interop.Kernel32.VER_MINORVERSION, Interop.Kernel32.VER_GREATER_EQUAL); + conditionMask = Interop.Kernel32.VerSetConditionMask(conditionMask, Interop.Kernel32.VER_SERVICEPACKMAJOR, Interop.Kernel32.VER_GREATER_EQUAL); + conditionMask = Interop.Kernel32.VerSetConditionMask(conditionMask, Interop.Kernel32.VER_SERVICEPACKMINOR, Interop.Kernel32.VER_GREATER_EQUAL); // Windows 8 version is 6.2 - var version = new Win32Native.OSVERSIONINFOEX(); + Interop.Kernel32.OSVERSIONINFOEX version = default; unsafe { - version.dwOSVersionInfoSize = sizeof(Win32Native.OSVERSIONINFOEX); + version.dwOSVersionInfoSize = sizeof(Interop.Kernel32.OSVERSIONINFOEX); } version.dwMajorVersion = 6; version.dwMinorVersion = 2; version.wServicePackMajor = 0; version.wServicePackMinor = 0; - return Win32Native.VerifyVersionInfoW(ref version, - Win32Native.VER_MAJORVERSION | Win32Native.VER_MINORVERSION | Win32Native.VER_SERVICEPACKMAJOR | Win32Native.VER_SERVICEPACKMINOR, + return Interop.Kernel32.VerifyVersionInfoW(ref version, + Interop.Kernel32.VER_MAJORVERSION | Interop.Kernel32.VER_MINORVERSION | Interop.Kernel32.VER_SERVICEPACKMAJOR | Interop.Kernel32.VER_SERVICEPACKMINOR, conditionMask); } } diff --git a/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs b/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs index a3c62f9..28c4ec2 100644 --- a/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs +++ b/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs @@ -30,9 +30,6 @@ namespace System.Runtime.InteropServices internal static Guid IID_IUnknown = new Guid(0, 0, 0, 0xC0, 0, 0, 0, 0, 0, 0, 0x46); #endif //FEATURE_COMINTEROP - private const int LMEM_FIXED = 0; - private const int LMEM_MOVEABLE = 2; - [MethodImpl(MethodImplOptions.InternalCall)] internal static extern int SizeOfHelper(Type t, bool throwIfNotMarshalable); @@ -272,7 +269,7 @@ namespace System.Runtime.InteropServices numBytes = new UIntPtr(unchecked((uint)cb.ToInt32())); #endif - IntPtr pNewMem = Win32Native.LocalAlloc_NoSafeHandle(LMEM_FIXED, unchecked(numBytes)); + IntPtr pNewMem = Interop.Kernel32.LocalAlloc(Interop.Kernel32.LMEM_FIXED, unchecked(numBytes)); if (pNewMem == IntPtr.Zero) { throw new OutOfMemoryException(); @@ -285,7 +282,7 @@ namespace System.Runtime.InteropServices { if (!IsWin32Atom(hglobal)) { - if (IntPtr.Zero != Win32Native.LocalFree(hglobal)) + if (IntPtr.Zero != Interop.Kernel32.LocalFree(hglobal)) { ThrowExceptionForHR(GetHRForLastWin32Error()); } @@ -294,7 +291,7 @@ namespace System.Runtime.InteropServices public static IntPtr ReAllocHGlobal(IntPtr pv, IntPtr cb) { - IntPtr pNewMem = Win32Native.LocalReAlloc(pv, cb, LMEM_MOVEABLE); + IntPtr pNewMem = Interop.Kernel32.LocalReAlloc(pv, cb, Interop.Kernel32.LMEM_MOVEABLE); if (pNewMem == IntPtr.Zero) { throw new OutOfMemoryException(); @@ -425,7 +422,7 @@ namespace System.Runtime.InteropServices public static IntPtr AllocCoTaskMem(int cb) { - IntPtr pNewMem = Win32Native.CoTaskMemAlloc(new UIntPtr((uint)cb)); + IntPtr pNewMem = Interop.Ole32.CoTaskMemAlloc(new UIntPtr((uint)cb)); if (pNewMem == IntPtr.Zero) { throw new OutOfMemoryException(); @@ -438,13 +435,13 @@ namespace System.Runtime.InteropServices { if (!IsWin32Atom(ptr)) { - Win32Native.CoTaskMemFree(ptr); + Interop.Ole32.CoTaskMemFree(ptr); } } public static IntPtr ReAllocCoTaskMem(IntPtr pv, int cb) { - IntPtr pNewMem = Win32Native.CoTaskMemRealloc(pv, new UIntPtr((uint)cb)); + IntPtr pNewMem = Interop.Ole32.CoTaskMemRealloc(pv, new UIntPtr((uint)cb)); if (pNewMem == IntPtr.Zero && cb != 0) { throw new OutOfMemoryException(); @@ -455,7 +452,7 @@ namespace System.Runtime.InteropServices internal static IntPtr AllocBSTR(int length) { - IntPtr bstr = Win32Native.SysAllocStringLen(null, length); + IntPtr bstr = Interop.OleAut32.SysAllocStringLen(null, length); if (bstr == IntPtr.Zero) { throw new OutOfMemoryException(); @@ -467,7 +464,7 @@ namespace System.Runtime.InteropServices { if (!IsWin32Atom(ptr)) { - Win32Native.SysFreeString(ptr); + Interop.OleAut32.SysFreeString(ptr); } } @@ -484,7 +481,7 @@ namespace System.Runtime.InteropServices throw new ArgumentOutOfRangeException(nameof(s)); } - IntPtr bstr = Win32Native.SysAllocStringLen(s, s.Length); + IntPtr bstr = Interop.OleAut32.SysAllocStringLen(s, s.Length); if (bstr == IntPtr.Zero) { throw new OutOfMemoryException(); @@ -495,7 +492,7 @@ namespace System.Runtime.InteropServices public static string PtrToStringBSTR(IntPtr ptr) { - return PtrToStringUni(ptr, (int)Win32Native.SysStringLen(ptr)); + return PtrToStringUni(ptr, (int)Interop.OleAut32.SysStringLen(ptr)); } #if FEATURE_COMINTEROP diff --git a/src/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeLibrary.cs b/src/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeLibrary.cs index 00b78d5..ca70d5d 100644 --- a/src/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeLibrary.cs +++ b/src/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeLibrary.cs @@ -2,13 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Collections.Generic; using System.Reflection; -using System.Reflection.Emit; using System.Runtime.CompilerServices; -using System.Runtime.ConstrainedExecution; -using Win32Native = Microsoft.Win32.Win32Native; -using System.Diagnostics; using System.Threading; namespace System.Runtime.InteropServices diff --git a/src/System.Private.CoreLib/src/System/StubHelpers.cs b/src/System.Private.CoreLib/src/System/StubHelpers.cs index 972a30c..90767fa 100644 --- a/src/System.Private.CoreLib/src/System/StubHelpers.cs +++ b/src/System.Private.CoreLib/src/System/StubHelpers.cs @@ -113,7 +113,7 @@ namespace System.StubHelpers internal static void ClearNative(IntPtr pNative) { - Win32Native.CoTaskMemFree(pNative); + Interop.Ole32.CoTaskMemFree(pNative); } } // class CSTRMarshaler @@ -169,7 +169,7 @@ namespace System.StubHelpers { if (pNative != IntPtr.Zero) { - Win32Native.CoTaskMemFree(pNative); + Interop.Ole32.CoTaskMemFree(pNative); } } } @@ -236,20 +236,20 @@ namespace System.StubHelpers // If caller provided a buffer, construct the BSTR manually. The size // of the buffer must be at least (lengthInBytes + 6) bytes. #if DEBUG - uint length = *((uint*)pNativeBuffer.ToPointer()); + uint length = *((uint*)pNativeBuffer); Debug.Assert(length >= lengthInBytes + 6, "BSTR localloc'ed buffer is too small"); #endif // set length - *((uint*)pNativeBuffer.ToPointer()) = lengthInBytes; + *((uint*)pNativeBuffer) = lengthInBytes; - ptrToFirstChar = (byte*)pNativeBuffer.ToPointer() + 4; + ptrToFirstChar = (byte*)pNativeBuffer + 4; } else { // If not provided, allocate the buffer using SysAllocStringByteLen so // that odd-sized strings will be handled as well. - ptrToFirstChar = (byte*)Win32Native.SysAllocStringByteLen(null, lengthInBytes).ToPointer(); + ptrToFirstChar = (byte*)Interop.OleAut32.SysAllocStringByteLen(null, lengthInBytes); if (ptrToFirstChar == null) { @@ -314,7 +314,7 @@ namespace System.StubHelpers if ((length & 1) == 1) { // odd-sized strings need to have the trailing byte saved in their sync block - ret.SetTrailByte(((byte*)bstr.ToPointer())[length - 1]); + ret.SetTrailByte(((byte*)bstr)[length - 1]); } return ret; @@ -325,7 +325,7 @@ namespace System.StubHelpers { if (IntPtr.Zero != pNative) { - Win32Native.SysFreeString(pNative); + Interop.OleAut32.SysFreeString(pNative); } } } // class BSTRMarshaler @@ -385,7 +385,7 @@ namespace System.StubHelpers { if (IntPtr.Zero != pNative) { - Win32Native.CoTaskMemFree((IntPtr)(((long)pNative) - sizeof(uint))); + Interop.Ole32.CoTaskMemFree((IntPtr)(((long)pNative) - sizeof(uint))); } } } // class VBByValStrMarshaler @@ -407,7 +407,7 @@ namespace System.StubHelpers bytes = AnsiCharMarshaler.DoAnsiConversion(strManaged, 0 != (flags & 0xFF), 0 != (flags >> 8), out nb); } - return Win32Native.SysAllocStringByteLen(bytes, (uint)nb); + return Interop.OleAut32.SysAllocStringByteLen(bytes, (uint)nb); } internal static unsafe string ConvertToManaged(IntPtr bstr) @@ -429,7 +429,7 @@ namespace System.StubHelpers { if (IntPtr.Zero != pNative) { - Win32Native.SysFreeString(pNative); + Interop.OleAut32.SysFreeString(pNative); } } } // class AnsiBSTRMarshaler @@ -1099,7 +1099,7 @@ namespace System.StubHelpers // marshal the object as class with layout (UnmanagedType.LPStruct) if (IsIn(dwFlags)) { - StubHelpers.FmtClassUpdateNativeInternal(pManagedHome, (byte*)pNativeHome.ToPointer(), ref cleanupWorkList); + StubHelpers.FmtClassUpdateNativeInternal(pManagedHome, (byte*)pNativeHome, ref cleanupWorkList); } if (IsOut(dwFlags)) { @@ -1172,7 +1172,7 @@ namespace System.StubHelpers case BackPropAction.Layout: { - StubHelpers.FmtClassUpdateCLRInternal(pManagedHome, (byte*)pNativeHome.ToPointer()); + StubHelpers.FmtClassUpdateCLRInternal(pManagedHome, (byte*)pNativeHome); break; } @@ -1221,7 +1221,7 @@ namespace System.StubHelpers // this must happen regardless of BackPropAction Marshal.DestroyStructure(pNativeHome, layoutType); } - Win32Native.CoTaskMemFree(pNativeHome); + Interop.Ole32.CoTaskMemFree(pNativeHome); } StubHelpers.DestroyCleanupList(ref cleanupWorkList); } diff --git a/src/System.Private.CoreLib/src/System/Threading/ThreadPool.CoreCLR.cs b/src/System.Private.CoreLib/src/System/Threading/ThreadPool.CoreCLR.cs index 592f2f6..02dedbf 100644 --- a/src/System.Private.CoreLib/src/System/Threading/ThreadPool.CoreCLR.cs +++ b/src/System.Private.CoreLib/src/System/Threading/ThreadPool.CoreCLR.cs @@ -14,7 +14,6 @@ using System.Runtime.CompilerServices; using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices; -using Microsoft.Win32; namespace System.Threading { @@ -31,7 +30,7 @@ namespace System.Threading internal sealed class RegisteredWaitHandleSafe : CriticalFinalizerObject { - private static IntPtr InvalidHandle => Win32Native.INVALID_HANDLE_VALUE; + private static IntPtr InvalidHandle => new IntPtr(-1); private IntPtr registeredWaitHandle = InvalidHandle; private WaitHandle m_internalWaitObject; private bool bReleaseNeeded = false; diff --git a/src/System.Private.CoreLib/src/System/Threading/Timer.CoreCLR.cs b/src/System.Private.CoreLib/src/System/Threading/Timer.CoreCLR.cs index bc78341..83b13b4 100644 --- a/src/System.Private.CoreLib/src/System/Threading/Timer.CoreCLR.cs +++ b/src/System.Private.CoreLib/src/System/Threading/Timer.CoreCLR.cs @@ -30,7 +30,7 @@ namespace System.Threading { ulong time100ns; - bool result = Win32Native.QueryUnbiasedInterruptTime(out time100ns); + bool result = Interop.Kernel32.QueryUnbiasedInterruptTime(out time100ns); if (!result) throw Marshal.GetExceptionForHR(Marshal.GetLastWin32Error()); diff --git a/src/System.Private.CoreLib/src/System/Threading/WaitHandle.cs b/src/System.Private.CoreLib/src/System/Threading/WaitHandle.cs index 83c5269..c591e1d 100644 --- a/src/System.Private.CoreLib/src/System/Threading/WaitHandle.cs +++ b/src/System.Private.CoreLib/src/System/Threading/WaitHandle.cs @@ -12,18 +12,14 @@ ** =============================================================================*/ +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using Microsoft.Win32.SafeHandles; +using System.Diagnostics.CodeAnalysis; + namespace System.Threading { - using System.Threading; - using System; - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using Microsoft.Win32.SafeHandles; - using System.Runtime.Versioning; - using System.Runtime.ConstrainedExecution; - using System.Diagnostics.CodeAnalysis; - using Win32Native = Microsoft.Win32.Win32Native; - public abstract class WaitHandle : MarshalByRefObject, IDisposable { public const int WaitTimeout = 0x102; @@ -38,11 +34,7 @@ namespace System.Threading internal bool hasThreadAffinity; - private static IntPtr GetInvalidHandle() - { - return Win32Native.INVALID_HANDLE_VALUE; - } - protected static readonly IntPtr InvalidHandle = GetInvalidHandle(); + protected static readonly IntPtr InvalidHandle = new IntPtr(-1); private const int WAIT_OBJECT_0 = 0; private const int WAIT_ABANDONED = 0x80; private const int WAIT_FAILED = 0x7FFFFFFF; diff --git a/src/vm/ilmarshalers.cpp b/src/vm/ilmarshalers.cpp index 1a253ec..2b7f7a6 100644 --- a/src/vm/ilmarshalers.cpp +++ b/src/vm/ilmarshalers.cpp @@ -328,7 +328,7 @@ void ILWSTRMarshaler::EmitClearNative(ILCodeStream* pslILEmit) EmitLoadNativeValue(pslILEmit); // static void CoTaskMemFree(IntPtr ptr) - pslILEmit->EmitCALL(METHOD__WIN32NATIVE__COTASKMEMFREE, 1, 0); + pslILEmit->EmitCALL(METHOD__MARSHAL__FREE_CO_TASK_MEM, 1, 0); } void ILWSTRMarshaler::EmitClearNativeTemp(ILCodeStream* pslILEmit) @@ -1710,7 +1710,7 @@ void ILVBByValStrWMarshaler::EmitClearNative(ILCodeStream* pslILEmit) pslILEmit->EmitLDLOC(m_dwLocalBuffer); pslILEmit->EmitBRFALSE(pExitLabel); pslILEmit->EmitLDLOC(m_dwLocalBuffer); - pslILEmit->EmitCALL(METHOD__WIN32NATIVE__COTASKMEMFREE, 1, 0); + pslILEmit->EmitCALL(METHOD__MARSHAL__FREE_CO_TASK_MEM, 1, 0); pslILEmit->EmitLabel(pExitLabel); } @@ -2361,7 +2361,7 @@ void ILLayoutClassPtrMarshalerBase::EmitClearNative(ILCodeStream* pslILEmit) EmitClearNativeContents(pslILEmit); EmitLoadNativeValue(pslILEmit); - pslILEmit->EmitCALL(METHOD__WIN32NATIVE__COTASKMEMFREE, 1, 0); + pslILEmit->EmitCALL(METHOD__MARSHAL__FREE_CO_TASK_MEM, 1, 0); pslILEmit->EmitLabel(pNullRefLabel); } @@ -3554,7 +3554,7 @@ void ILArrayWithOffsetMarshaler::EmitClearNativeTemp(ILCodeStream* pslILEmit) // CoTaskMemFree EmitLoadNativeValue(pslILEmit); - pslILEmit->EmitCALL(METHOD__WIN32NATIVE__COTASKMEMFREE, 1, 0); + pslILEmit->EmitCALL(METHOD__MARSHAL__FREE_CO_TASK_MEM, 1, 0); pslILEmit->EmitLabel(pDoneLabel); } diff --git a/src/vm/ilmarshalers.h b/src/vm/ilmarshalers.h index 3f2c16b..99a3d8f 100644 --- a/src/vm/ilmarshalers.h +++ b/src/vm/ilmarshalers.h @@ -2032,7 +2032,7 @@ public: }; ILUTF8BufferMarshaler() : - ILOptimizedAllocMarshaler(METHOD__WIN32NATIVE__COTASKMEMFREE) + ILOptimizedAllocMarshaler(METHOD__MARSHAL__FREE_CO_TASK_MEM) { LIMITED_METHOD_CONTRACT; } @@ -2061,7 +2061,7 @@ public: }; ILWSTRBufferMarshaler() : - ILOptimizedAllocMarshaler(METHOD__WIN32NATIVE__COTASKMEMFREE) + ILOptimizedAllocMarshaler(METHOD__MARSHAL__FREE_CO_TASK_MEM) { LIMITED_METHOD_CONTRACT; } @@ -2090,7 +2090,7 @@ public: }; ILCSTRBufferMarshaler() : - ILOptimizedAllocMarshaler(METHOD__WIN32NATIVE__COTASKMEMFREE) + ILOptimizedAllocMarshaler(METHOD__MARSHAL__FREE_CO_TASK_MEM) { LIMITED_METHOD_CONTRACT; } @@ -2391,7 +2391,7 @@ protected: EmitLoadNativeValue(pslILEmit); // static void CoTaskMemFree(IntPtr ptr) - pslILEmit->EmitCALL(METHOD__WIN32NATIVE__COTASKMEMFREE, 1, 0); + pslILEmit->EmitCALL(METHOD__MARSHAL__FREE_CO_TASK_MEM, 1, 0); } virtual void EmitConvertSpaceCLRToNative(ILCodeStream* pslILEmit) @@ -2403,7 +2403,7 @@ protected: pslILEmit->EmitLDC(sizeof(ELEMENT)); pslILEmit->EmitCONV_U(); // static IntPtr CoTaskMemAlloc(UIntPtr cb) - pslILEmit->EmitCALL(METHOD__WIN32NATIVE__COTASKMEMALLOC, 1, 1); + pslILEmit->EmitCALL(METHOD__MARSHAL__ALLOC_CO_TASK_MEM, 1, 1); EmitStoreNativeValue(pslILEmit); } } @@ -3138,7 +3138,7 @@ public: METHOD__MNGD_HIDDEN_LENGTH_ARRAY_MARSHALER__CONVERT_CONTENTS_TO_MANAGED, METHOD__MNGD_HIDDEN_LENGTH_ARRAY_MARSHALER__CONVERT_SPACE_TO_NATIVE, METHOD__MNGD_HIDDEN_LENGTH_ARRAY_MARSHALER__CONVERT_CONTENTS_TO_NATIVE, - METHOD__WIN32NATIVE__COTASKMEMFREE, + METHOD__MARSHAL__FREE_CO_TASK_MEM, METHOD__MNGD_HIDDEN_LENGTH_ARRAY_MARSHALER__CLEAR_NATIVE_CONTENTS, METHOD__NIL) { diff --git a/src/vm/mscorlib.h b/src/vm/mscorlib.h index 56aa94f..3fe9c64 100644 --- a/src/vm/mscorlib.h +++ b/src/vm/mscorlib.h @@ -491,6 +491,7 @@ DEFINE_METHOD(MARSHAL, GET_HR_FOR_EXCEPTION_WINRT, GetHRForE DEFINE_METHOD(MARSHAL, GET_FUNCTION_POINTER_FOR_DELEGATE, GetFunctionPointerForDelegate, SM_Delegate_RetIntPtr) DEFINE_METHOD(MARSHAL, GET_DELEGATE_FOR_FUNCTION_POINTER, GetDelegateForFunctionPointer, SM_IntPtr_Type_RetDelegate) DEFINE_METHOD(MARSHAL, ALLOC_CO_TASK_MEM, AllocCoTaskMem, SM_Int_RetIntPtr) +DEFINE_METHOD(MARSHAL, FREE_CO_TASK_MEM, FreeCoTaskMem, SM_IntPtr_RetVoid) DEFINE_FIELD(MARSHAL, SYSTEM_MAX_DBCS_CHAR_SIZE, SystemMaxDBCSCharSize) DEFINE_CLASS(NATIVELIBRARY, Interop, NativeLibrary) @@ -1148,10 +1149,6 @@ DEFINE_METHOD(ASANY_MARSHALER, CLEAR_NATIVE, ClearNativ DEFINE_CLASS(NATIVEVARIANT, StubHelpers, NativeVariant) -DEFINE_CLASS(WIN32NATIVE, Win32, Win32Native) -DEFINE_METHOD(WIN32NATIVE, COTASKMEMALLOC, CoTaskMemAlloc, SM_UIntPtr_RetIntPtr) -DEFINE_METHOD(WIN32NATIVE, COTASKMEMFREE, CoTaskMemFree, SM_IntPtr_RetVoid) - #ifdef FEATURE_COMINTEROP DEFINE_CLASS(IITERABLE, WinRT, IIterable`1) DEFINE_CLASS(IVECTOR, WinRT, IVector`1) -- 2.7.4