-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Add Get/SetLastPInvokeError and Get/SetLastSystemError APIs #51505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b3ff0e9
0c87b33
9072798
b83d43e
312875e
d4307b6
fec0fc7
65f24ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. | ||
|
|
||
| using System; | ||
| using System.Runtime.InteropServices; | ||
|
|
||
| internal static partial class Interop | ||
| { | ||
| internal unsafe partial class Sys | ||
| { | ||
| [DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_GetErrNo")] | ||
| [SuppressGCTransition] | ||
| internal static extern int GetErrNo(); | ||
|
|
||
| [DllImport(Interop.Libraries.SystemNative, EntryPoint = "SystemNative_SetErrNo")] | ||
| [SuppressGCTransition] | ||
| internal static extern void SetErrNo(int errorCode); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. | ||
|
|
||
| using System.Runtime.InteropServices; | ||
|
|
||
| internal partial class Interop | ||
| { | ||
| internal static partial class Kernel32 | ||
| { | ||
| [DllImport(Libraries.Kernel32)] | ||
| [SuppressGCTransition] | ||
| internal static extern void SetLastError(int errorCode); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1266,5 +1266,10 @@ public static void InitHandle(SafeHandle safeHandle, IntPtr handle) | |
| // To help maximize performance of P/Invokes, don't check if safeHandle is null. | ||
| safeHandle.SetHandle(handle); | ||
| } | ||
|
|
||
| public static int GetLastWin32Error() | ||
| { | ||
| return GetLastPInvokeError(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If GetLastPInvokeError is the recommended API to use moving forward, should we change all of our netcoreapp usage of GetLastWin32Error to instead use GetLastPInvokeError?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think so. I didn't try to do it with this change for adding the new API just because it would touch so much, but the plan would be to do so for 6.0. |
||
| } | ||
|
jkotas marked this conversation as resolved.
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Xunit; | ||
|
|
||
| namespace System.Runtime.InteropServices.Tests | ||
| { | ||
| public class LastErrorTests | ||
| { | ||
| [Fact] | ||
| public void LastPInvokeError_RoundTrip() | ||
| { | ||
| int errorExpected = 123; | ||
| Marshal.SetLastPInvokeError(errorExpected); | ||
| Assert.Equal(errorExpected, Marshal.GetLastPInvokeError()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void LastSystemError_RoundTrip() | ||
| { | ||
| int errorExpected = 123; | ||
| Marshal.SetLastSystemError(errorExpected); | ||
| Assert.Equal(errorExpected, Marshal.GetLastSystemError()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetLastPInvokeError_GetLastWin32Error() | ||
| { | ||
| int errorExpected = 123; | ||
| Marshal.SetLastPInvokeError(errorExpected); | ||
| Assert.Equal(errorExpected, Marshal.GetLastWin32Error()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void SetLastSystemError_PInvokeErrorUnchanged() | ||
| { | ||
| int pinvokeError = 123; | ||
| Marshal.SetLastPInvokeError(pinvokeError); | ||
|
|
||
| int systemError = pinvokeError + 1; | ||
| Marshal.SetLastSystemError(systemError); | ||
|
|
||
| // Setting last system error should not affect the last P/Invoke error | ||
| int pinvokeActual = Marshal.GetLastPInvokeError(); | ||
| Assert.NotEqual(systemError, pinvokeActual); | ||
| Assert.Equal(pinvokeError, pinvokeActual); | ||
|
|
||
| int win32Actual = Marshal.GetLastWin32Error(); | ||
| Assert.NotEqual(systemError, win32Actual); | ||
| Assert.Equal(pinvokeError, win32Actual); | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.