Skip to content
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

Rename MarshalEx.SetLastWin32Error -> SetLastPInvokeError #1007

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions DllImportGenerator/Ancillary.Interop/MarshalEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ public static void InitHandle(SafeHandle safeHandle, IntPtr handle)
/// <summary>
/// Set the last platform invoke error on the thread
/// </summary>
public static void SetLastWin32Error(int error)
public static void SetLastPInvokeError(int error)
{
typeof(Marshal).GetMethod("SetLastWin32Error", BindingFlags.NonPublic | BindingFlags.Static)!.Invoke(null, new object[] { error });
MethodInfo? method = typeof(Marshal).GetMethod("SetLastWin32Error", BindingFlags.NonPublic | BindingFlags.Static);
if (method == null)
{
method = typeof(Marshal).GetMethod("SetLastPInvokeError", BindingFlags.Public | BindingFlags.Static);
}

method!.Invoke(null, new object[] { error });
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public SetLastErrorMarshaller(int i)
public int ToManaged()
{
// Explicity set the last error to something else on unmarshalling
MarshalEx.SetLastWin32Error(val * 2);
MarshalEx.SetLastPInvokeError(val * 2);
return val;
}
}
Expand Down Expand Up @@ -54,12 +54,12 @@ public void LastWin32Error_HasExpectedValue(int error)
Assert.Equal(errorString, ret);

// Clear the last error
MarshalEx.SetLastWin32Error(0);
MarshalEx.SetLastPInvokeError(0);

NativeExportsNE.SetLastError.SetError(error, shouldSetError: 1);
Assert.Equal(error, Marshal.GetLastWin32Error());

MarshalEx.SetLastWin32Error(0);
MarshalEx.SetLastPInvokeError(0);

// Custom marshalling sets the last error on unmarshalling.
// Last error should reflect error from native call, not unmarshalling.
Expand All @@ -71,15 +71,15 @@ public void LastWin32Error_HasExpectedValue(int error)
public void ClearPreviousError()
{
int error = 100;
MarshalEx.SetLastWin32Error(error);
MarshalEx.SetLastPInvokeError(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);
MarshalEx.SetLastPInvokeError(error);

// Don't actually set the error in the native call. SetLastError=true should clear any existing error.
NativeExportsNE.SetLastError.SetError(error, shouldSetError: 0);
Expand Down
4 changes: 2 additions & 2 deletions DllImportGenerator/DllImportGenerator/StubCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,13 @@ public override (string managed, string native) GetIdentifiers(TypePositionInfo

if (this.dllImportData.SetLastError && !options.GenerateForwarders())
{
// Marshal.SetLastWin32Error(<lastError>);
// Marshal.SetLastPInvokeError(<lastError>);
allStatements.Add(ExpressionStatement(
InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
ParseName(TypeNames.MarshalEx(options)),
IdentifierName("SetLastWin32Error")),
IdentifierName("SetLastPInvokeError")),
ArgumentList(SingletonSeparatedList(
Argument(IdentifierName(LastErrorIdentifier)))))));
}
Expand Down