-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Implement Marshal.Get(Last)PInvokeErrorMessage() APIs.
#70685
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
Merged
AaronRobinsonMSFT
merged 7 commits into
dotnet:main
from
AaronRobinsonMSFT:runtime_67872
Jun 24, 2022
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
55959cf
Implement Marshal.GetPInvokeErrorMessage() API.
AaronRobinsonMSFT 1308eb2
Invert the implementation for Win32Exception and move the
AaronRobinsonMSFT 45161d3
Add additional testing.
AaronRobinsonMSFT 1979b4c
Handle unknown error codes on linux.
AaronRobinsonMSFT c37247c
Condition usage of new API for NET7+.
AaronRobinsonMSFT f498fcc
Added additional GetLastPInvokeErrorMessage() API
AaronRobinsonMSFT 41872ed
Apply suggestions from code review
AaronRobinsonMSFT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
10 changes: 0 additions & 10 deletions
10
src/libraries/System.Private.CoreLib/src/System/ComponentModel/Win32Exception.Unix.cs
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
src/libraries/System.Private.CoreLib/src/System/ComponentModel/Win32Exception.Windows.cs
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
76 changes: 76 additions & 0 deletions
76
...eropServices.UnitTests/System/Runtime/InteropServices/Marshal/PInvokeErrorMessageTests.cs
This file contains hidden or 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,76 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.ComponentModel; | ||
| using Xunit; | ||
|
|
||
| namespace System.Runtime.InteropServices.Tests | ||
| { | ||
| public class PInvokeErrorMessageTests | ||
| { | ||
| public static IEnumerable<object[]> GetErrorCode_TestData() | ||
| { | ||
| yield return new object[] { 0 }; | ||
| yield return new object[] { 1 }; | ||
|
|
||
| // errno values | ||
| yield return new object[] { 0x10002 }; | ||
| yield return new object[] { 0x10003 }; | ||
| yield return new object[] { 0x10014 }; | ||
| yield return new object[] { 0x1001D }; | ||
|
|
||
| // HRESULT values | ||
| yield return new object[] { unchecked((int)0x80004001) }; | ||
| yield return new object[] { unchecked((int)0x80004005) }; | ||
| yield return new object[] { unchecked((int)0x80070057) }; | ||
| yield return new object[] { unchecked((int)0x8000FFFF) }; | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(GetErrorCode_TestData))] | ||
| public void PInvokeErrorMessage_Returns_Win32Exception_Message(int error) | ||
| { | ||
| // The Win32Exception represents the canonical system exception on | ||
| // all platforms. The GetPInvokeErrorMessage API is about providing | ||
| // this message in a manner that avoid the instantiation of a Win32Exception | ||
| // instance and querying for the message. | ||
| string expected = new Win32Exception(error).Message; | ||
AaronRobinsonMSFT marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Assert.Equal(expected, Marshal.GetPInvokeErrorMessage(error)); | ||
| } | ||
|
|
||
| [Theory] | ||
| [MemberData(nameof(GetErrorCode_TestData))] | ||
| public void LastPInvokeErrorMessage_Returns_Correct_Message(int error) | ||
| { | ||
| string expected = Marshal.GetPInvokeErrorMessage(error); | ||
|
|
||
| Marshal.SetLastPInvokeError(error); | ||
| Assert.Equal(expected, Marshal.GetLastPInvokeErrorMessage()); | ||
| } | ||
|
|
||
| [Fact] | ||
| [PlatformSpecific(TestPlatforms.Windows)] | ||
| public void PInvokeErrorMessage_Returns_UniqueMessage_Windows() | ||
| { | ||
| var err1 = Marshal.GetPInvokeErrorMessage(unchecked((int)0x80070057)); // E_INVALIDARG | ||
| var err2 = Marshal.GetPInvokeErrorMessage(unchecked((int)0x80004001)); // E_NOTIMPL | ||
| Assert.NotNull(err1); | ||
| Assert.NotNull(err2); | ||
| Assert.NotEqual(err1, err2); | ||
| } | ||
|
|
||
| [Fact] | ||
| [PlatformSpecific(TestPlatforms.AnyUnix)] | ||
| public void PInvokeErrorMessage_Returns_UniqueMessage_Unix() | ||
| { | ||
| var err1 = Marshal.GetPInvokeErrorMessage(0x10002); // EACCES | ||
| var err2 = Marshal.GetPInvokeErrorMessage(0x10014); // EEXIST | ||
| Assert.NotNull(err1); | ||
| Assert.NotNull(err2); | ||
| // It is difficult to determine which error codes do or do not have | ||
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // translations on non-Windows. For now, we will just confirm the | ||
| // message is non-null. | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.