-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Get/SetLastPInvokeError and Get/SetLastSystemError APIs (#51505)
- Loading branch information
1 parent
6ca3d91
commit 7ee226d
Showing
28 changed files
with
352 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/libraries/Common/src/Interop/Unix/System.Native/Interop.ErrNo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.SetLastError.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...em.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/LastErrorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.