Skip to content

Commit

Permalink
Added additional GetLastPInvokeErrorMessage() API
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronRobinsonMSFT committed Jun 23, 2022
1 parent c37247c commit f498fcc
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1296,5 +1296,14 @@ public static int GetLastWin32Error()
{
return GetLastPInvokeError();
}

/// <summary>
/// Gets the system error message for the last PInvoke error code.
/// </summary>
/// <returns>The error message associated with the last PInvoke error code.</returns>
public static string GetLastPInvokeErrorMessage()
{
return GetPInvokeErrorMessage(GetLastPInvokeError());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ public static void FreeHGlobal(System.IntPtr hglobal) { }
public static int GetLastPInvokeError() { throw null; }
public static int GetLastSystemError() { throw null; }
public static int GetLastWin32Error() { throw null; }
public static string GetLastPInvokeErrorMessage() { throw null; }
public static string GetPInvokeErrorMessage(int error) { throw null; }
[System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public void PInvokeErrorMessage_Returns_Win32Exception_Message(int error)
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public WindowsIdentity(string sUserPrincipalName)
unsafe
{
if (!Interop.Advapi32.AllocateLocallyUniqueId(&sourceContext.SourceIdentifier))
throw new SecurityException(Marshal.GetPInvokeErrorMessage(Marshal.GetLastPInvokeError()));
throw new SecurityException(Marshal.GetLastPInvokeErrorMessage());

sourceName.AsSpan().CopyTo(new Span<byte>(sourceContext.SourceName, TOKEN_SOURCE.TOKEN_SOURCE_LENGTH));
}
Expand Down Expand Up @@ -264,7 +264,7 @@ private static SafeAccessTokenHandle DuplicateAccessToken(IntPtr accessToken)
true,
Interop.DuplicateHandleOptions.DUPLICATE_SAME_ACCESS))
{
throw new SecurityException(Marshal.GetPInvokeErrorMessage(Marshal.GetLastPInvokeError()));
throw new SecurityException(Marshal.GetLastPInvokeErrorMessage());
}

return duplicateAccessToken;
Expand Down Expand Up @@ -465,15 +465,15 @@ private bool CheckNtTokenForSid(SecurityIdentifier sid)
(uint)TokenImpersonationLevel.Identification,
(uint)TokenType.TokenImpersonation,
ref token))
throw new SecurityException(Marshal.GetPInvokeErrorMessage(Marshal.GetLastPInvokeError()));
throw new SecurityException(Marshal.GetLastPInvokeErrorMessage());
}


// CheckTokenMembership will check if the SID is both present and enabled in the access token.
if (!Interop.Advapi32.CheckTokenMembership((til != TokenImpersonationLevel.None ? _safeTokenHandle : token),
sid.BinaryForm,
ref isMember))
throw new SecurityException(Marshal.GetPInvokeErrorMessage(Marshal.GetLastPInvokeError()));
throw new SecurityException(Marshal.GetLastPInvokeErrorMessage());


}
Expand Down Expand Up @@ -756,7 +756,7 @@ private static void RunImpersonatedInternal(SafeAccessTokenHandle token, Action
delegate
{
if (!Interop.Advapi32.RevertToSelf())
Environment.FailFast(Marshal.GetPInvokeErrorMessage(Marshal.GetLastPInvokeError()));
Environment.FailFast(Marshal.GetLastPInvokeErrorMessage());

s_currentImpersonatedToken.Value = null;

Expand All @@ -776,12 +776,12 @@ private static void CurrentImpersonatedTokenChanged(AsyncLocalValueChangedArgs<S
return; // we handle explicit Value property changes elsewhere.

if (!Interop.Advapi32.RevertToSelf())
Environment.FailFast(Marshal.GetPInvokeErrorMessage(Marshal.GetLastPInvokeError()));
Environment.FailFast(Marshal.GetLastPInvokeErrorMessage());

if (args.CurrentValue != null && !args.CurrentValue.IsInvalid)
{
if (!Interop.Advapi32.ImpersonateLoggedOnUser(args.CurrentValue))
Environment.FailFast(Marshal.GetPInvokeErrorMessage(Marshal.GetLastPInvokeError()));
Environment.FailFast(Marshal.GetLastPInvokeErrorMessage());
}
}

Expand Down Expand Up @@ -899,7 +899,7 @@ private static Interop.LUID GetLogonAuthId(SafeAccessTokenHandle safeTokenHandle
dwLength,
out _);
if (!result)
throw new SecurityException(Marshal.GetPInvokeErrorMessage(Marshal.GetLastPInvokeError()));
throw new SecurityException(Marshal.GetLastPInvokeErrorMessage());
break;
case Interop.Errors.ERROR_INVALID_HANDLE:
throw new ArgumentException(SR.Argument_InvalidImpersonationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public virtual bool IsInRole(SecurityIdentifier sid)
(uint)TokenImpersonationLevel.Identification,
(uint)TokenType.TokenImpersonation,
ref token))
throw new SecurityException(Marshal.GetPInvokeErrorMessage(Marshal.GetLastPInvokeError()));
throw new SecurityException(Marshal.GetLastPInvokeErrorMessage());
}

bool isMember = false;
Expand All @@ -162,7 +162,7 @@ public virtual bool IsInRole(SecurityIdentifier sid)
if (!Interop.Advapi32.CheckTokenMembership((_identity.ImpersonationLevel != TokenImpersonationLevel.None ? _identity.AccessToken : token),
sid.BinaryForm,
ref isMember))
throw new SecurityException(Marshal.GetPInvokeErrorMessage(Marshal.GetLastPInvokeError()));
throw new SecurityException(Marshal.GetLastPInvokeErrorMessage());

token.Dispose();
return isMember;
Expand Down

0 comments on commit f498fcc

Please sign in to comment.