-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91ff4be
commit b36b0b8
Showing
9 changed files
with
307 additions
and
33 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
94 changes: 94 additions & 0 deletions
94
DllImportGenerator/DllImportGenerator.IntegrationTests/SetLastErrorTests.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,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 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
Oops, something went wrong.