From 842956952e12361390ad92960b6124738500272a Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Fri, 23 Apr 2021 17:16:02 -0700 Subject: Rename MarshalEx.SetLastWin32Error -> SetLastPInvokeError (dotnet/runtimelab#1007) Commit migrated from https://github.com/dotnet/runtimelab/commit/82735dd0fef4886ae2f0886267ef30822e17ee8a --- .../gen/DllImportGenerator/StubCodeGenerator.cs | 4 ++-- .../tests/Ancillary.Interop/MarshalEx.cs | 10 ++++++++-- .../tests/DllImportGenerator.Tests/SetLastErrorTests.cs | 10 +++++----- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/StubCodeGenerator.cs b/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/StubCodeGenerator.cs index 4a5f15ede8d..343f767a0b8 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/StubCodeGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/StubCodeGenerator.cs @@ -321,13 +321,13 @@ namespace Microsoft.Interop if (this.dllImportData.SetLastError && !options.GenerateForwarders()) { - // Marshal.SetLastWin32Error(); + // Marshal.SetLastPInvokeError(); allStatements.Add(ExpressionStatement( InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, ParseName(TypeNames.MarshalEx(options)), - IdentifierName("SetLastWin32Error")), + IdentifierName("SetLastPInvokeError")), ArgumentList(SingletonSeparatedList( Argument(IdentifierName(LastErrorIdentifier))))))); } diff --git a/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/MarshalEx.cs b/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/MarshalEx.cs index 8a53af07090..716dcf28eec 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/MarshalEx.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/Ancillary.Interop/MarshalEx.cs @@ -23,9 +23,15 @@ namespace System.Runtime.InteropServices /// /// Set the last platform invoke error on the thread /// - public static void SetLastWin32Error(int error) + public static void SetLastPInvokeError(int error) { - typeof(Marshal).GetMethod("SetLastWin32Error", BindingFlags.NonPublic | BindingFlags.Static)!.Invoke(null, new object[] { error }); + MethodInfo? method = typeof(Marshal).GetMethod("SetLastWin32Error", BindingFlags.NonPublic | BindingFlags.Static); + if (method == null) + { + method = typeof(Marshal).GetMethod("SetLastPInvokeError", BindingFlags.Public | BindingFlags.Static); + } + + method!.Invoke(null, new object[] { error }); } /// diff --git a/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.Tests/SetLastErrorTests.cs b/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.Tests/SetLastErrorTests.cs index 45407ce0222..04542c7f8f4 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.Tests/SetLastErrorTests.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.Tests/SetLastErrorTests.cs @@ -18,7 +18,7 @@ namespace DllImportGenerator.IntegrationTests public int ToManaged() { // Explicity set the last error to something else on unmarshalling - MarshalEx.SetLastWin32Error(val * 2); + MarshalEx.SetLastPInvokeError(val * 2); return val; } } @@ -54,12 +54,12 @@ namespace DllImportGenerator.IntegrationTests Assert.Equal(errorString, ret); // Clear the last error - MarshalEx.SetLastWin32Error(0); + MarshalEx.SetLastPInvokeError(0); NativeExportsNE.SetLastError.SetError(error, shouldSetError: 1); Assert.Equal(error, Marshal.GetLastWin32Error()); - MarshalEx.SetLastWin32Error(0); + MarshalEx.SetLastPInvokeError(0); // Custom marshalling sets the last error on unmarshalling. // Last error should reflect error from native call, not unmarshalling. @@ -71,7 +71,7 @@ namespace DllImportGenerator.IntegrationTests public void ClearPreviousError() { int error = 100; - MarshalEx.SetLastWin32Error(error); + MarshalEx.SetLastPInvokeError(error); // Don't actually set the error in the native call. SetLastError=true should clear any existing error. string errorString = error.ToString(); @@ -79,7 +79,7 @@ namespace DllImportGenerator.IntegrationTests Assert.Equal(0, Marshal.GetLastWin32Error()); Assert.Equal(errorString, ret); - MarshalEx.SetLastWin32Error(error); + MarshalEx.SetLastPInvokeError(error); // Don't actually set the error in the native call. SetLastError=true should clear any existing error. NativeExportsNE.SetLastError.SetError(error, shouldSetError: 0); -- cgit v1.2.3