-
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
4562fa7
commit 6808460
Showing
9 changed files
with
250 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
60 changes: 60 additions & 0 deletions
60
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,60 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
using Xunit; | ||
|
||
namespace DllImportGenerator.IntegrationTests | ||
{ | ||
partial class NativeExportsNE | ||
{ | ||
public partial class SetLastError | ||
{ | ||
[GeneratedDllImport(nameof(NativeExportsNE), EntryPoint = "set_error", SetLastError = true)] | ||
public static partial void SetError(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()); | ||
} | ||
|
||
[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()); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace NativeExports | ||
{ | ||
public static unsafe class Error | ||
{ | ||
private class Kernel32 | ||
{ | ||
[DllImport(nameof(Kernel32))] | ||
public static extern void SetLastError(int error); | ||
|
||
[DllImport(nameof(Kernel32))] | ||
public static extern int GetLastError(); | ||
} | ||
|
||
private class libc | ||
{ | ||
[DllImport(nameof(libc))] | ||
internal static unsafe extern int* __errno_location(); | ||
|
||
[DllImport(nameof(libc))] | ||
internal static unsafe extern int* __error(); | ||
} | ||
|
||
[UnmanagedCallersOnly(EntryPoint = "set_error")] | ||
public static void SetError(int error, byte shouldSetError) | ||
{ | ||
if (shouldSetError != 0) | ||
SetLastError(error); | ||
} | ||
|
||
[UnmanagedCallersOnly(EntryPoint = "set_error_return_string")] | ||
public static ushort* SetErrorReturnString(int error, byte shouldSetError, ushort* errorString) | ||
{ | ||
ushort* ret = (ushort*)Marshal.StringToCoTaskMemUni(new string((char*)errorString)); | ||
|
||
if (shouldSetError != 0) | ||
SetLastError(error); | ||
|
||
return ret; | ||
} | ||
|
||
private static void SetLastError(int error) | ||
{ | ||
if (OperatingSystem.IsWindows()) | ||
{ | ||
Kernel32.SetLastError(error); | ||
} | ||
else if (OperatingSystem.IsMacOS()) | ||
{ | ||
*libc.__error() = error; | ||
} | ||
else if (OperatingSystem.IsLinux()) | ||
{ | ||
*libc.__errno_location() = error; | ||
} | ||
else | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} | ||
} |
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