Skip to content

Commit

Permalink
Task/use get last p invoke error (#52212)
Browse files Browse the repository at this point in the history
* replacing GetLastWin32Error with GetLastPInvokeError in remaining assemblies

* replace GetLastWin32Error with GetLastPInvokeError in assemblies outside system.private.corelib

* Apply suggestions from code review

Co-authored-by: Jan Kotas <[email protected]>
  • Loading branch information
KZedan and jkotas authored May 9, 2021
1 parent b3278ca commit b08a97a
Show file tree
Hide file tree
Showing 27 changed files with 83 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public partial class Win32Exception : ExternalException, ISerializable
/// Initializes a new instance of the <see cref='System.ComponentModel.Win32Exception'/> class with the last Win32 error
/// that occurred.
/// </summary>
public Win32Exception() : this(Marshal.GetLastWin32Error())
public Win32Exception() : this(Marshal.GetLastPInvokeError())
{
}

Expand All @@ -43,7 +43,7 @@ public Win32Exception(int error, string? message) : base(message)
/// <summary>
/// Initializes a new instance of the Exception class with a specified error message.
/// </summary>
public Win32Exception(string? message) : this(Marshal.GetLastWin32Error(), message)
public Win32Exception(string? message) : this(Marshal.GetLastPInvokeError(), message)
{
}

Expand All @@ -53,7 +53,7 @@ public Win32Exception(string? message) : this(Marshal.GetLastWin32Error(), messa
/// </summary>
public Win32Exception(string? message, Exception? innerException) : base(message, innerException)
{
NativeErrorCode = Marshal.GetLastWin32Error();
NativeErrorCode = Marshal.GetLastPInvokeError();
}

protected Win32Exception(SerializationInfo info, StreamingContext context) : base(info, context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private static bool IsExceptionMessageLong(int errorCode)
null);
if (result == 0)
{
return (Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER);
return (Marshal.GetLastPInvokeError() == ERROR_INSUFFICIENT_BUFFER);
}

return false;
Expand Down
58 changes: 29 additions & 29 deletions src/libraries/System.Console/src/System/ConsolePal.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static void SetConsoleInputEncoding(Encoding enc)
if (enc.CodePage != UnicodeCodePage)
{
if (!Interop.Kernel32.SetConsoleCP(enc.CodePage))
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());
}
}

Expand All @@ -123,7 +123,7 @@ public static void SetConsoleOutputEncoding(Encoding enc)
if (enc.CodePage != UnicodeCodePage)
{
if (!Interop.Kernel32.SetConsoleOutputCP(enc.CodePage))
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());
}
}

Expand Down Expand Up @@ -280,7 +280,7 @@ public static bool KeyAvailable
bool r = Interop.Kernel32.PeekConsoleInput(InputHandle, out ir, 1, out numEventsRead);
if (!r)
{
int errorCode = Marshal.GetLastWin32Error();
int errorCode = Marshal.GetLastPInvokeError();
if (errorCode == Interop.Errors.ERROR_INVALID_HANDLE)
throw new InvalidOperationException(SR.InvalidOperation_ConsoleKeyAvailableOnFile);
throw Win32Marshal.GetExceptionForWin32Error(errorCode, "stdin");
Expand All @@ -295,7 +295,7 @@ public static bool KeyAvailable
r = Interop.Kernel32.ReadConsoleInput(InputHandle, out ir, 1, out numEventsRead);

if (!r)
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());
}
else
{
Expand Down Expand Up @@ -413,7 +413,7 @@ public static bool TreatControlCAsInput

int mode = 0;
if (!Interop.Kernel32.GetConsoleMode(handle, out mode))
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());

return (mode & Interop.Kernel32.ENABLE_PROCESSED_INPUT) == 0;
}
Expand All @@ -425,7 +425,7 @@ public static bool TreatControlCAsInput

int mode = 0;
if (!Interop.Kernel32.GetConsoleMode(handle, out mode))
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());

if (value)
{
Expand All @@ -437,7 +437,7 @@ public static bool TreatControlCAsInput
}

if (!Interop.Kernel32.SetConsoleMode(handle, mode))
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());
}
}

Expand Down Expand Up @@ -534,7 +534,7 @@ public static int CursorSize
{
Interop.Kernel32.CONSOLE_CURSOR_INFO cci;
if (!Interop.Kernel32.GetConsoleCursorInfo(OutputHandle, out cci))
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());

return cci.dwSize;
}
Expand All @@ -546,11 +546,11 @@ public static int CursorSize

Interop.Kernel32.CONSOLE_CURSOR_INFO cci;
if (!Interop.Kernel32.GetConsoleCursorInfo(OutputHandle, out cci))
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());

cci.dwSize = value;
if (!Interop.Kernel32.SetConsoleCursorInfo(OutputHandle, ref cci))
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());
}
}

Expand All @@ -560,19 +560,19 @@ public static bool CursorVisible
{
Interop.Kernel32.CONSOLE_CURSOR_INFO cci;
if (!Interop.Kernel32.GetConsoleCursorInfo(OutputHandle, out cci))
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());

return cci.bVisible;
}
set
{
Interop.Kernel32.CONSOLE_CURSOR_INFO cci;
if (!Interop.Kernel32.GetConsoleCursorInfo(OutputHandle, out cci))
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());

cci.bVisible = value;
if (!Interop.Kernel32.SetConsoleCursorInfo(OutputHandle, ref cci))
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());
}
}

Expand Down Expand Up @@ -604,7 +604,7 @@ public static unsafe string Title

if (result == 0)
{
int error = Marshal.GetLastWin32Error();
int error = Marshal.GetLastPInvokeError();
switch (error)
{
case Interop.Errors.ERROR_INSUFFICIENT_BUFFER:
Expand Down Expand Up @@ -639,7 +639,7 @@ public static unsafe string Title
set
{
if (!Interop.Kernel32.SetConsoleTitle(value))
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());
}
}

Expand Down Expand Up @@ -715,7 +715,7 @@ public static unsafe void MoveBufferArea(int sourceLeft, int sourceTop,
fixed (Interop.Kernel32.CHAR_INFO* pCharInfo = data)
r = Interop.Kernel32.ReadConsoleOutput(OutputHandle, pCharInfo, bufferSize, bufferCoord, ref readRegion);
if (!r)
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());

// Overwrite old section
Interop.Kernel32.COORD writeCoord = default;
Expand All @@ -730,11 +730,11 @@ public static unsafe void MoveBufferArea(int sourceLeft, int sourceTop,
r = Interop.Kernel32.FillConsoleOutputCharacter(OutputHandle, sourceChar, sourceWidth, writeCoord, out numWritten);
Debug.Assert(numWritten == sourceWidth, "FillConsoleOutputCharacter wrote the wrong number of chars!");
if (!r)
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());

r = Interop.Kernel32.FillConsoleOutputAttribute(OutputHandle, attr, sourceWidth, writeCoord, out numWritten);
if (!r)
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());
}

// Write text to new location
Expand Down Expand Up @@ -771,21 +771,21 @@ public static void Clear()
success = Interop.Kernel32.FillConsoleOutputCharacter(hConsole, ' ',
conSize, coordScreen, out numCellsWritten);
if (!success)
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());

// now set the buffer's attributes accordingly

numCellsWritten = 0;
success = Interop.Kernel32.FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
conSize, coordScreen, out numCellsWritten);
if (!success)
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());

// put the cursor at (0, 0)

success = Interop.Kernel32.SetConsoleCursorPosition(hConsole, coordScreen);
if (!success)
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());
}

public static void SetCursorPosition(int left, int top)
Expand All @@ -797,7 +797,7 @@ public static void SetCursorPosition(int left, int top)
if (!Interop.Kernel32.SetConsoleCursorPosition(hConsole, coords))
{
// Give a nice error message for out of range sizes
int errorCode = Marshal.GetLastWin32Error();
int errorCode = Marshal.GetLastPInvokeError();
Interop.Kernel32.CONSOLE_SCREEN_BUFFER_INFO csbi = GetBufferInfo();
if (left >= csbi.dwSize.X)
throw new ArgumentOutOfRangeException(nameof(left), left, SR.ArgumentOutOfRange_ConsoleBufferBoundaries);
Expand Down Expand Up @@ -849,7 +849,7 @@ public static void SetBufferSize(int width, int height)
size.Y = (short)height;
if (!Interop.Kernel32.SetConsoleScreenBufferSize(OutputHandle, size))
{
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());
}
}

Expand Down Expand Up @@ -951,7 +951,7 @@ public static unsafe void SetWindowPosition(int left, int top)

bool r = Interop.Kernel32.SetConsoleWindowInfo(OutputHandle, true, &srWindow);
if (!r)
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());
}

public static unsafe void SetWindowSize(int width, int height)
Expand Down Expand Up @@ -987,7 +987,7 @@ public static unsafe void SetWindowSize(int width, int height)
if (resizeBuffer)
{
if (!Interop.Kernel32.SetConsoleScreenBufferSize(OutputHandle, size))
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError());
}

Interop.Kernel32.SMALL_RECT srWindow = csbi.srWindow;
Expand All @@ -997,7 +997,7 @@ public static unsafe void SetWindowSize(int width, int height)

if (!Interop.Kernel32.SetConsoleWindowInfo(OutputHandle, true, &srWindow))
{
int errorCode = Marshal.GetLastWin32Error();
int errorCode = Marshal.GetLastPInvokeError();

// If we resized the buffer, un-resize it.
if (resizeBuffer)
Expand Down Expand Up @@ -1070,7 +1070,7 @@ private static Interop.Kernel32.CONSOLE_SCREEN_BUFFER_INFO GetBufferInfo(bool th
!Interop.Kernel32.GetConsoleScreenBufferInfo(ErrorHandle, out csbi) &&
!Interop.Kernel32.GetConsoleScreenBufferInfo(InputHandle, out csbi))
{
int errorCode = Marshal.GetLastWin32Error();
int errorCode = Marshal.GetLastPInvokeError();
if (errorCode == Interop.Errors.ERROR_INVALID_HANDLE && !throwOnNoConsole)
return default;
throw Win32Marshal.GetExceptionForWin32Error(errorCode);
Expand Down Expand Up @@ -1178,7 +1178,7 @@ private static unsafe int ReadFileNative(IntPtr hFile, Span<byte> buffer, bool i
// For pipes that are closing or broken, just stop.
// (E.g. ERROR_NO_DATA ("pipe is being closed") is returned when we write to a console that is closing;
// ERROR_BROKEN_PIPE ("pipe was closed") is returned when stdin was closed, which is not an error, but EOF.)
int errorCode = Marshal.GetLastWin32Error();
int errorCode = Marshal.GetLastPInvokeError();
if (errorCode == Interop.Errors.ERROR_NO_DATA || errorCode == Interop.Errors.ERROR_BROKEN_PIPE)
return Interop.Errors.ERROR_SUCCESS;
return errorCode;
Expand Down Expand Up @@ -1218,7 +1218,7 @@ private static unsafe int WriteFileNative(IntPtr hFile, ReadOnlySpan<byte> bytes
// For pipes that are closing or broken, just stop.
// (E.g. ERROR_NO_DATA ("pipe is being closed") is returned when we write to a console that is closing;
// ERROR_BROKEN_PIPE ("pipe was closed") is returned when stdin was closed, which is not an error, but EOF.)
int errorCode = Marshal.GetLastWin32Error();
int errorCode = Marshal.GetLastPInvokeError();
if (errorCode == Interop.Errors.ERROR_NO_DATA || errorCode == Interop.Errors.ERROR_BROKEN_PIPE)
return Interop.Errors.ERROR_SUCCESS;
return errorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public unsafe string VolumeLabel
bool r = Interop.Kernel32.SetVolumeLabel(Name, value);
if (!r)
{
int errorCode = Marshal.GetLastWin32Error();
int errorCode = Marshal.GetLastPInvokeError();
// Provide better message
if (errorCode == Interop.Errors.ERROR_ACCESS_DENIED)
throw new UnauthorizedAccessException(SR.InvalidOperation_SetVolumeLabelFailed);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal static class Error
// An alternative to Win32Marshal with friendlier messages for drives
internal static Exception GetExceptionForLastWin32DriveError(string driveName)
{
int errorCode = Marshal.GetLastWin32Error();
int errorCode = Marshal.GetLastPInvokeError();
return GetExceptionForWin32DriveError(errorCode, driveName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ internal unsafe void Start(CancellationToken cancellationToken)
if (!Interop.EventStream.FSEventStreamStart(_eventStream))
{
// Try to get the Watcher to raise the error event; if we can't do that, just silently exit since the watcher is gone anyway
int error = Marshal.GetLastWin32Error();
int error = Marshal.GetLastPInvokeError();
if (_weakWatcher.TryGetTarget(out FileSystemWatcher? watcher))
{
// An error occurred while trying to start the run loop so fail out
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public static long GetAvailableFreeBytes(string drive)
long userBytes;
if (!DllImports.GetDiskFreeSpaceEx(drive, out userBytes, out ignored, out ignored))
{
throw new IOException("DriveName: " + drive + " ErrorCode:" + Marshal.GetLastWin32Error());
throw new IOException("DriveName: " + drive + " ErrorCode:" + Marshal.GetLastPInvokeError());
}

return userBytes;
Expand All @@ -257,7 +257,7 @@ private static bool IsReady(string drive)
long ignored;
if (!DllImports.GetDiskFreeSpaceEx(drive, out ignored, out ignored, out ignored))
{
return Marshal.GetLastWin32Error() != ERROR_NOT_READY;
return Marshal.GetLastPInvokeError() != ERROR_NOT_READY;
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ public static void Mount(string volumeName, string mountPoint)
StringBuilder sb = new StringBuilder(1024);
r = GetVolumeNameForVolumeMountPoint(volumeName, sb, sb.Capacity);
if (!r)
throw new Exception(string.Format("Win32 error: {0}", Marshal.GetLastWin32Error()));
throw new Exception(string.Format("Win32 error: {0}", Marshal.GetLastPInvokeError()));

string uniqueName = sb.ToString();
Console.WriteLine(string.Format("uniqueName: <{0}>", uniqueName));
r = SetVolumeMountPoint(mountPoint, uniqueName);
if (!r)
throw new Exception(string.Format("Win32 error: {0}", Marshal.GetLastWin32Error()));
throw new Exception(string.Format("Win32 error: {0}", Marshal.GetLastPInvokeError()));
Task.Delay(100).Wait(); // adding sleep for the file system to settle down so that reparse point mounting works
}

Expand All @@ -90,7 +90,7 @@ public static void Unmount(string mountPoint)

bool r = DeleteVolumeMountPoint(mountPoint);
if (!r)
throw new Exception(string.Format("Win32 error: {0}", Marshal.GetLastWin32Error()));
throw new Exception(string.Format("Win32 error: {0}", Marshal.GetLastPInvokeError()));
}

/// For standalone debugging help. Change Main0 to Main
Expand Down
Loading

0 comments on commit b08a97a

Please sign in to comment.