Skip to content

Commit

Permalink
replace GetLastWin32Error with GetLastPInvokeError in assemblies outs…
Browse files Browse the repository at this point in the history
…ide system.private.corelib
  • Loading branch information
KZedan committed May 8, 2021
1 parent 3b3c46e commit f814d89
Show file tree
Hide file tree
Showing 40 changed files with 143 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal static partial class Crypt32
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool CryptProtectData(
[In] ref DATA_BLOB pDataIn,
[In] string? szDataDescr,
[In] string szDataDescr,
[In] ref DATA_BLOB pOptionalEntropy,
[In] IntPtr pvReserved,
[In] IntPtr pPromptStruct,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ internal static unsafe string GetMessage(int errorCode, IntPtr moduleHandle)
// We got back an error. If the error indicated that there wasn't enough room to store
// the error message, then call FormatMessage again, but this time rather than passing in
// a buffer, have the method allocate one, which we then need to free.
#if NET6_0
if (Marshal.GetLastPInvokeError() == ERROR_INSUFFICIENT_BUFFER)
#else
if (Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER)
#endif
{
IntPtr nativeMsgPtr = default;
try
Expand Down
6 changes: 1 addition & 5 deletions src/libraries/Common/src/System/IO/Win32Marshal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ internal static class Win32Marshal
/// Converts, resetting it, the last Win32 error into a corresponding <see cref="Exception"/> object, optionally
/// including the specified path in the error message.
/// </summary>
#if NET6_0
internal static Exception GetExceptionForLastWin32Error(string? path = "")
=> GetExceptionForWin32Error(Marshal.GetLastPInvokeError(), path);
#else
internal static Exception GetExceptionForLastWin32Error(string? path = "")
=> GetExceptionForWin32Error(Marshal.GetLastWin32Error(), path);
#endif

/// <summary>
/// Converts the specified Win32 error into a corresponding <see cref="Exception"/> object, optionally
/// including the specified path in the error message.
Expand Down
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
Loading

0 comments on commit f814d89

Please sign in to comment.