Skip to content

Commit

Permalink
Change to GetLastPInvokeError where possible (#73980)
Browse files Browse the repository at this point in the history
  • Loading branch information
danmoseley authored Aug 18, 2022
1 parent a33c21e commit 2327a36
Show file tree
Hide file tree
Showing 73 changed files with 230 additions and 230 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private static unsafe void Sysctl(int* name, int name_len, ref byte* value, ref
ret = Sysctl(name, name_len, value, &bytesLength);
if (ret != 0)
{
throw new InvalidOperationException(SR.Format(SR.InvalidSysctl, *name, Marshal.GetLastWin32Error()));
throw new InvalidOperationException(SR.Format(SR.InvalidSysctl, *name, Marshal.GetLastPInvokeError()));
}
value = (byte*)Marshal.AllocHGlobal((int)bytesLength);
}
Expand Down Expand Up @@ -75,7 +75,7 @@ private static unsafe void Sysctl(int* name, int name_len, ref byte* value, ref
{
Marshal.FreeHGlobal((IntPtr)value);
}
throw new InvalidOperationException(SR.Format(SR.InvalidSysctl, *name, Marshal.GetLastWin32Error()));
throw new InvalidOperationException(SR.Format(SR.InvalidSysctl, *name, Marshal.GetLastPInvokeError()));
}

len = (int)bytesLength;
Expand Down
6 changes: 3 additions & 3 deletions src/libraries/Common/src/Interop/Unix/Interop.Errors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal enum Error
{
// These values were defined in src/Native/System.Native/fxerrno.h
//
// They compare against values obtained via Interop.Sys.GetLastError() not Marshal.GetLastWin32Error()
// They compare against values obtained via Interop.Sys.GetLastError() not Marshal.GetLastPInvokeError()
// which obtains the raw errno that varies between unixes. The strong typing as an enum is meant to
// prevent confusing the two. Casting to or from int is suspect. Use GetLastErrorInfo() if you need to
// correlate these to the underlying platform values or obtain the corresponding error message.
Expand Down Expand Up @@ -155,12 +155,12 @@ internal static partial class Sys
{
internal static Error GetLastError()
{
return ConvertErrorPlatformToPal(Marshal.GetLastWin32Error());
return ConvertErrorPlatformToPal(Marshal.GetLastPInvokeError());
}

internal static ErrorInfo GetLastErrorInfo()
{
return new ErrorInfo(Marshal.GetLastWin32Error());
return new ErrorInfo(Marshal.GetLastPInvokeError());
}

internal static unsafe string StrError(int platformErrno)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal static unsafe int ForkAndExecProcess(
setUser ? 1 : 0, userId, groupId, pGroups, groups?.Length ?? 0,
out lpChildPid, out stdinFd, out stdoutFd, out stderrFd);
}
return result == 0 ? 0 : Marshal.GetLastWin32Error();
return result == 0 ? 0 : Marshal.GetLastPInvokeError();
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal enum PriorityWhich : int
internal static int GetPriority(PriorityWhich which, int who, out int priority)
{
priority = GetPriority(which, who);
return Marshal.GetLastWin32Error();
return Marshal.GetLastPInvokeError();
}

internal static System.Diagnostics.ThreadPriorityLevel GetThreadPriorityFromNiceValue(int nice)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ internal static unsafe SafeHandle CryptDecodeObjectToMemory(CryptDecodeObjectStr

if (!CryptDecodeObject(MsgEncodingType.All, (IntPtr)lpszStructType, pbEncoded, cbEncoded, 0, null, ref cbRequired))
{
throw Marshal.GetLastWin32Error().ToCryptographicException();
throw Marshal.GetLastPInvokeError().ToCryptographicException();
}

SafeHandle sh = SafeHeapAllocHandle.Alloc(cbRequired);
if (!CryptDecodeObject(MsgEncodingType.All, (IntPtr)lpszStructType, pbEncoded, cbEncoded, 0, (void*)sh.DangerousGetHandle(), ref cbRequired))
{
Exception e = Marshal.GetLastWin32Error().ToCryptographicException();
Exception e = Marshal.GetLastPInvokeError().ToCryptographicException();
sh.Dispose();
throw e;
}
Expand All @@ -49,13 +49,13 @@ internal static unsafe byte[] CryptEncodeObjectToByteArray(CryptDecodeObjectStru
int cb = 0;
if (!CryptEncodeObject(MsgEncodingType.All, lpszStructType, decoded, null, ref cb))
{
throw Marshal.GetLastWin32Error().ToCryptographicException();
throw Marshal.GetLastPInvokeError().ToCryptographicException();
}

byte[] encoded = new byte[cb];
if (!CryptEncodeObject(MsgEncodingType.All, lpszStructType, decoded, encoded, ref cb))
{
throw Marshal.GetLastWin32Error().ToCryptographicException();
throw Marshal.GetLastPInvokeError().ToCryptographicException();
}

return encoded.Resize(cb);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal static int CopyFile(string src, string dst, bool failIfExists)
int cancel = 0;
if (!Interop.Kernel32.CopyFileEx(src, dst, IntPtr.Zero, IntPtr.Zero, ref cancel, copyFlags))
{
return Marshal.GetLastWin32Error();
return Marshal.GetLastPInvokeError();
}

return Interop.Errors.ERROR_SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private static unsafe partial bool QueryFullProcessImageName(
{
return buffer.Slice(0, (int)length).ToString();
}
else if (Marshal.GetLastWin32Error() != Errors.ERROR_INSUFFICIENT_BUFFER)
else if (Marshal.GetLastPInvokeError() != Errors.ERROR_INSUFFICIENT_BUFFER)
{
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal static int FillAttributeInfo(string? path, ref Interop.Kernel32.WIN32_F
{
if (!Interop.Kernel32.GetFileAttributesEx(path, Interop.Kernel32.GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, ref data))
{
errorCode = Marshal.GetLastWin32Error();
errorCode = Marshal.GetLastPInvokeError();

if (!IsPathUnreachableError(errorCode))
{
Expand All @@ -84,7 +84,7 @@ internal static int FillAttributeInfo(string? path, ref Interop.Kernel32.WIN32_F
{
if (handle.IsInvalid)
{
errorCode = Marshal.GetLastWin32Error();
errorCode = Marshal.GetLastPInvokeError();
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static unsafe void CreateDirectory(string fullPath, byte[]? securityDescr
r = Interop.Kernel32.CreateDirectory(name, &secAttrs);
if (!r && (firstError == 0))
{
int currentError = Marshal.GetLastWin32Error();
int currentError = Marshal.GetLastPInvokeError();
// While we tried to avoid creating directories that don't
// exist above, there are at least two cases that will
// cause us to see ERROR_ALREADY_EXISTS here. FileExists
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/Common/src/System/IO/Win32Marshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal static class Win32Marshal
/// including the specified path in the error message.
/// </summary>
internal static Exception GetExceptionForLastWin32Error(string? path = "")
=> GetExceptionForWin32Error(Marshal.GetLastWin32Error(), path);
=> GetExceptionForWin32Error(Marshal.GetLastPInvokeError(), path);

/// <summary>
/// Converts the specified Win32 error into a corresponding <see cref="Exception"/> object, optionally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class NetworkInformationException : Win32Exception
/// Creates a new instance of the <see cref='System.Net.NetworkInformation.NetworkInformationException'/> class with the default error code.
/// </para>
/// </devdoc>
public NetworkInformationException() : base(Marshal.GetLastWin32Error())
public NetworkInformationException() : base(Marshal.GetLastPInvokeError())
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private static bool IsSupported(AddressFamily af)
socket = Interop.Winsock.WSASocketW(af, SocketType.Stream, 0, IntPtr.Zero, 0, (int)Interop.Winsock.SocketConstructorFlags.WSA_FLAG_NO_HANDLE_INHERIT);
return
socket != INVALID_SOCKET ||
(SocketError)Marshal.GetLastWin32Error() != SocketError.AddressFamilyNotSupported;
(SocketError)Marshal.GetLastPInvokeError() != SocketError.AddressFamilyNotSupported;
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ private unsafe void Initialize()
if (Interop.User32.RegisterClassW(ref windowClass) == 0)
{
_windowProc = null;
Debug.WriteLine("Unable to register broadcast window class: {0}", Marshal.GetLastWin32Error());
Debug.WriteLine("Unable to register broadcast window class: {0}", Marshal.GetLastPInvokeError());
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ out pClientContext
out bufferSize,
IntPtr.Zero
);
if (!f && (bufferSize > 0) && (Marshal.GetLastWin32Error() == 122) /*ERROR_INSUFFICIENT_BUFFER*/)
if (!f && (bufferSize > 0) && (Marshal.GetLastPInvokeError() == 122) /*ERROR_INSUFFICIENT_BUFFER*/)
{
GlobalDebug.WriteLineIf(GlobalDebug.Info, "AuthZSet", "Getting info from ctx (size={0})", bufferSize);

Expand Down Expand Up @@ -155,23 +155,23 @@ out pClientContext
}
else
{
lastError = Marshal.GetLastWin32Error();
lastError = Marshal.GetLastPInvokeError();
}
}
else
{
lastError = Marshal.GetLastWin32Error();
lastError = Marshal.GetLastPInvokeError();
Debug.Fail("With a zero-length buffer, this should have never succeeded");
}
}
else
{
lastError = Marshal.GetLastWin32Error();
lastError = Marshal.GetLastPInvokeError();
}
}
else
{
lastError = Marshal.GetLastWin32Error();
lastError = Marshal.GetLastPInvokeError();
}

if (!f)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ internal static string ConvertSidToSDDL(byte[] sid)
}
else
{
int lastErrorCode = Marshal.GetLastWin32Error();
int lastErrorCode = Marshal.GetLastPInvokeError();

GlobalDebug.WriteLineIf(
GlobalDebug.Warn,
Expand Down Expand Up @@ -354,7 +354,7 @@ internal static IntPtr GetCurrentUserSid()
out tokenHandle
))
{
if ((error = Marshal.GetLastWin32Error()) == 1008) // ERROR_NO_TOKEN
if ((error = Marshal.GetLastPInvokeError()) == 1008) // ERROR_NO_TOKEN
{
Debug.Assert(tokenHandle.IsInvalid);
tokenHandle.Dispose();
Expand All @@ -366,7 +366,7 @@ out tokenHandle
out tokenHandle
))
{
int lastError = Marshal.GetLastWin32Error();
int lastError = Marshal.GetLastPInvokeError();
GlobalDebug.WriteLineIf(GlobalDebug.Error, "Utils", "GetCurrentUserSid: OpenProcessToken failed, gle=" + lastError);

throw new PrincipalOperationException(SR.Format(SR.UnableToOpenToken, lastError));
Expand Down Expand Up @@ -394,7 +394,7 @@ out tokenHandle
out neededBufferSize);

int getTokenInfoError = 0;
if ((getTokenInfoError = Marshal.GetLastWin32Error()) != 122) // ERROR_INSUFFICIENT_BUFFER
if ((getTokenInfoError = Marshal.GetLastPInvokeError()) != 122) // ERROR_INSUFFICIENT_BUFFER
{
GlobalDebug.WriteLineIf(GlobalDebug.Error, "Utils", "GetCurrentUserSid: GetTokenInformation (1st try) failed, gle=" + getTokenInfoError);

Expand All @@ -416,7 +416,7 @@ out tokenHandle

if (!success)
{
int lastError = Marshal.GetLastWin32Error();
int lastError = Marshal.GetLastPInvokeError();
GlobalDebug.WriteLineIf(GlobalDebug.Error,
"Utils",
"GetCurrentUserSid: GetTokenInformation (2nd try) failed, neededBufferSize=" + neededBufferSize + ", gle=" + lastError);
Expand All @@ -437,7 +437,7 @@ out tokenHandle
success = Interop.Advapi32.CopySid(userSidLength, pCopyOfUserSid, pUserSid);
if (!success)
{
int lastError = Marshal.GetLastWin32Error();
int lastError = Marshal.GetLastPInvokeError();
GlobalDebug.WriteLineIf(GlobalDebug.Error,
"Utils",
"GetCurrentUserSid: CopySid failed, errorcode=" + lastError);
Expand Down Expand Up @@ -508,7 +508,7 @@ internal static IntPtr GetMachineDomainSid()
bool success = Interop.Advapi32.CopySid(sidLength, pCopyOfSid, info.DomainSid);
if (!success)
{
int lastError = Marshal.GetLastWin32Error();
int lastError = Marshal.GetLastPInvokeError();
GlobalDebug.WriteLineIf(GlobalDebug.Error,
"Utils",
"GetMachineDomainSid: CopySid failed, errorcode=" + lastError);
Expand Down Expand Up @@ -603,7 +603,7 @@ internal static unsafe int LookupSid(string serverName, NetCred credentials, byt

int f = Interop.Advapi32.LookupAccountSid(serverName, sid, null, ref nameLength, null, ref domainNameLength, out accountUsage);

int lastErr = Marshal.GetLastWin32Error();
int lastErr = Marshal.GetLastPInvokeError();
if (lastErr != 122) // ERROR_INSUFFICIENT_BUFFER
{
GlobalDebug.WriteLineIf(GlobalDebug.Error, "Utils", "LookupSid: LookupAccountSid (1st try) failed, gle=" + lastErr);
Expand All @@ -622,7 +622,7 @@ internal static unsafe int LookupSid(string serverName, NetCred credentials, byt

if (f == 0)
{
lastErr = Marshal.GetLastWin32Error();
lastErr = Marshal.GetLastPInvokeError();
Debug.Assert(lastErr != 0);

GlobalDebug.WriteLineIf(GlobalDebug.Error, "Utils", "LookupSid: LookupAccountSid (2nd try) failed, gle=" + lastErr);
Expand Down Expand Up @@ -752,7 +752,7 @@ internal static bool BeginImpersonation(NetCred credential, out IntPtr hUserToke
// check the result
if (result == 0)
{
int lastError = Marshal.GetLastWin32Error();
int lastError = Marshal.GetLastPInvokeError();
GlobalDebug.WriteLineIf(GlobalDebug.Error, "Utils", "BeginImpersonation: LogonUser failed, gle=" + lastError);

throw new PrincipalOperationException(
Expand All @@ -762,7 +762,7 @@ internal static bool BeginImpersonation(NetCred credential, out IntPtr hUserToke
result = Interop.Advapi32.ImpersonateLoggedOnUser(hToken);
if (result == 0)
{
int lastError = Marshal.GetLastWin32Error();
int lastError = Marshal.GetLastPInvokeError();
GlobalDebug.WriteLineIf(GlobalDebug.Error, "Utils", "BeginImpersonation: ImpersonateLoggedOnUser failed, gle=" + lastError);

// Close the token the was created above....
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,7 @@ private unsafe void GetDomains()
var dsListDomainsInSiteW = (delegate* unmanaged<IntPtr, char*, IntPtr*, int>)global::Interop.Kernel32.GetProcAddress(DirectoryContext.ADHandle, "DsListDomainsInSiteW");
if (dsListDomainsInSiteW == null)
{
throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastPInvokeError());
}

fixed (char* distinguishedName = (string)PropertyManager.GetPropertyValue(context, cachedEntry, PropertyManager.DistinguishedName)!)
Expand Down Expand Up @@ -1347,7 +1347,7 @@ private unsafe void GetDomains()
var dsFreeNameResultW = (delegate* unmanaged<IntPtr, void>)global::Interop.Kernel32.GetProcAddress(DirectoryContext.ADHandle, "DsFreeNameResultW");
if (dsFreeNameResultW == null)
{
throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastPInvokeError());
}

dsFreeNameResultW(info);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ private static void GetLibraryHandle()
IntPtr tempHandle = global::Interop.Kernel32.LoadLibrary(systemPath + "\\ntdsapi.dll");
if (tempHandle == (IntPtr)0)
{
throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastWin32Error());
throw ExceptionHelper.GetExceptionFromErrorCode(Marshal.GetLastPInvokeError());
}
else
{
Expand Down
Loading

0 comments on commit 2327a36

Please sign in to comment.