-
Notifications
You must be signed in to change notification settings - Fork 215
Handle SetLastError=true #360
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
elinor-fung
merged 3 commits into
dotnet:feature/DllImportGenerator
from
elinor-fung:setLastError
Nov 30, 2020
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
94 changes: 94 additions & 0 deletions
94
DllImportGenerator/DllImportGenerator.IntegrationTests/SetLastErrorTests.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,94 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
using Xunit; | ||
|
||
namespace DllImportGenerator.IntegrationTests | ||
{ | ||
[BlittableType] | ||
public struct SetLastErrorMarshaller | ||
{ | ||
public int val; | ||
|
||
public SetLastErrorMarshaller(int i) | ||
{ | ||
val = i; | ||
} | ||
|
||
public int ToManaged() | ||
{ | ||
// Explicity set the last error to something else on unmarshalling | ||
MarshalEx.SetLastWin32Error(val * 2); | ||
return val; | ||
} | ||
} | ||
|
||
partial class NativeExportsNE | ||
{ | ||
public partial class SetLastError | ||
{ | ||
[GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "set_error", SetLastError = true)] | ||
public static partial int SetError(int error, byte shouldSetError); | ||
|
||
[GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "set_error_return_string", SetLastError = true)] | ||
[return: MarshalUsing(typeof(SetLastErrorMarshaller))] | ||
public static partial int SetError_CustomMarshallingSetsError(int error, byte shouldSetError); | ||
|
||
[GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "set_error_return_string", SetLastError = true)] | ||
[return: MarshalAs(UnmanagedType.LPWStr)] | ||
public static partial string SetError_NonBlittableSignature(int error, [MarshalAs(UnmanagedType.U1)] bool shouldSetError, [MarshalAs(UnmanagedType.LPWStr)] string errorString); | ||
} | ||
} | ||
|
||
public class SetLastErrorTests | ||
{ | ||
[Theory] | ||
[InlineData(0)] | ||
[InlineData(2)] | ||
[InlineData(-5)] | ||
public void LastWin32Error_HasExpectedValue(int error) | ||
{ | ||
string errorString = error.ToString(); | ||
string ret = NativeExportsNE.SetLastError.SetError_NonBlittableSignature(error, shouldSetError: true, errorString); | ||
Assert.Equal(error, Marshal.GetLastWin32Error()); | ||
Assert.Equal(errorString, ret); | ||
|
||
// Clear the last error | ||
MarshalEx.SetLastWin32Error(0); | ||
|
||
NativeExportsNE.SetLastError.SetError(error, shouldSetError: 1); | ||
Assert.Equal(error, Marshal.GetLastWin32Error()); | ||
|
||
MarshalEx.SetLastWin32Error(0); | ||
|
||
// Custom marshalling sets the last error on unmarshalling. | ||
// Last error should reflect error from native call, not unmarshalling. | ||
NativeExportsNE.SetLastError.SetError_CustomMarshallingSetsError(error, shouldSetError: 1); | ||
Assert.Equal(error, Marshal.GetLastWin32Error()); | ||
} | ||
|
||
[Fact] | ||
public void ClearPreviousError() | ||
{ | ||
int error = 100; | ||
MarshalEx.SetLastWin32Error(error); | ||
|
||
// Don't actually set the error in the native call. SetLastError=true should clear any existing error. | ||
string errorString = error.ToString(); | ||
string ret = NativeExportsNE.SetLastError.SetError_NonBlittableSignature(error, shouldSetError: false, errorString); | ||
Assert.Equal(0, Marshal.GetLastWin32Error()); | ||
Assert.Equal(errorString, ret); | ||
|
||
MarshalEx.SetLastWin32Error(error); | ||
|
||
// Don't actually set the error in the native call. SetLastError=true should clear any existing error. | ||
NativeExportsNE.SetLastError.SetError(error, shouldSetError: 0); | ||
Assert.Equal(0, Marshal.GetLastWin32Error()); | ||
|
||
// Don't actually set the error in the native call. Custom marshalling still sets the last error. | ||
// SetLastError=true should clear any existing error and ignore error set by custom marshalling. | ||
NativeExportsNE.SetLastError.SetError_CustomMarshallingSetsError(error, shouldSetError: 0); | ||
Assert.Equal(0, Marshal.GetLastWin32Error()); | ||
} | ||
} | ||
} |
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
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.