diff --git a/src/libraries/Microsoft.Win32.Primitives/src/System/ComponentModel/Win32Exception.cs b/src/libraries/Microsoft.Win32.Primitives/src/System/ComponentModel/Win32Exception.cs index a2b16441080988..bf78cbc03c926a 100644 --- a/src/libraries/Microsoft.Win32.Primitives/src/System/ComponentModel/Win32Exception.cs +++ b/src/libraries/Microsoft.Win32.Primitives/src/System/ComponentModel/Win32Exception.cs @@ -21,7 +21,7 @@ public partial class Win32Exception : ExternalException, ISerializable /// Initializes a new instance of the class with the last Win32 error /// that occurred. /// - public Win32Exception() : this(Marshal.GetLastWin32Error()) + public Win32Exception() : this(Marshal.GetLastPInvokeError()) { } @@ -43,7 +43,7 @@ public Win32Exception(int error, string? message) : base(message) /// /// Initializes a new instance of the Exception class with a specified error message. /// - public Win32Exception(string? message) : this(Marshal.GetLastWin32Error(), message) + public Win32Exception(string? message) : this(Marshal.GetLastPInvokeError(), message) { } @@ -53,7 +53,7 @@ public Win32Exception(string? message) : this(Marshal.GetLastWin32Error(), messa /// public Win32Exception(string? message, Exception? innerException) : base(message, innerException) { - NativeErrorCode = Marshal.GetLastWin32Error(); + NativeErrorCode = Marshal.GetLastPInvokeError(); } protected Win32Exception(SerializationInfo info, StreamingContext context) : base(info, context) diff --git a/src/libraries/Microsoft.Win32.Primitives/tests/Win32ExceptionTests.cs b/src/libraries/Microsoft.Win32.Primitives/tests/Win32ExceptionTests.cs index 04a1dfca13be14..27bdd0860086b9 100644 --- a/src/libraries/Microsoft.Win32.Primitives/tests/Win32ExceptionTests.cs +++ b/src/libraries/Microsoft.Win32.Primitives/tests/Win32ExceptionTests.cs @@ -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; diff --git a/src/libraries/System.Console/src/System/ConsolePal.Windows.cs b/src/libraries/System.Console/src/System/ConsolePal.Windows.cs index dd74a7d826e97a..95cebc12c4887e 100644 --- a/src/libraries/System.Console/src/System/ConsolePal.Windows.cs +++ b/src/libraries/System.Console/src/System/ConsolePal.Windows.cs @@ -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()); } } @@ -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()); } } @@ -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"); @@ -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 { @@ -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; } @@ -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) { @@ -437,7 +437,7 @@ public static bool TreatControlCAsInput } if (!Interop.Kernel32.SetConsoleMode(handle, mode)) - throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); + throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError()); } } @@ -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; } @@ -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()); } } @@ -560,7 +560,7 @@ 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; } @@ -568,11 +568,11 @@ 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()); cci.bVisible = value; if (!Interop.Kernel32.SetConsoleCursorInfo(OutputHandle, ref cci)) - throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); + throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError()); } } @@ -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: @@ -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()); } } @@ -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; @@ -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 @@ -771,7 +771,7 @@ 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 @@ -779,13 +779,13 @@ public static void Clear() 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) @@ -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); @@ -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()); } } @@ -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) @@ -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; @@ -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) @@ -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); @@ -1178,7 +1178,7 @@ private static unsafe int ReadFileNative(IntPtr hFile, Span 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; @@ -1218,7 +1218,7 @@ private static unsafe int WriteFileNative(IntPtr hFile, ReadOnlySpan 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; diff --git a/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Windows.cs b/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Windows.cs index d361611377ed15..bb495b09c13100 100644 --- a/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Windows.cs +++ b/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Windows.cs @@ -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); diff --git a/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/Error.cs b/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/Error.cs index 90998a89f81cfc..bf8141073883ea 100644 --- a/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/Error.cs +++ b/src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/Error.cs @@ -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); } diff --git a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.OSX.cs b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.OSX.cs index 2081e679cbc176..1bbc4648bf8322 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.OSX.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.OSX.cs @@ -349,7 +349,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 diff --git a/src/libraries/System.IO.FileSystem/tests/PortedCommon/IOServices.cs b/src/libraries/System.IO.FileSystem/tests/PortedCommon/IOServices.cs index 3fb3066d17f542..27b923dbd39c22 100644 --- a/src/libraries/System.IO.FileSystem/tests/PortedCommon/IOServices.cs +++ b/src/libraries/System.IO.FileSystem/tests/PortedCommon/IOServices.cs @@ -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; @@ -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; diff --git a/src/libraries/System.IO.FileSystem/tests/PortedCommon/ReparsePointUtilities.cs b/src/libraries/System.IO.FileSystem/tests/PortedCommon/ReparsePointUtilities.cs index d2304e1213fa02..5423bda6814b3b 100644 --- a/src/libraries/System.IO.FileSystem/tests/PortedCommon/ReparsePointUtilities.cs +++ b/src/libraries/System.IO.FileSystem/tests/PortedCommon/ReparsePointUtilities.cs @@ -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 } @@ -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 diff --git a/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.Windows.cs b/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.Windows.cs index ad08669e8e008c..8bc32eb666ef8a 100644 --- a/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.Windows.cs +++ b/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.Windows.cs @@ -47,7 +47,7 @@ private static SafeMemoryMappedFileHandle CreateCore( Interop.CreateFileMapping(fileHandle, ref secAttrs, GetPageAccess(access) | (int)options, capacity, mapName) : Interop.CreateFileMapping(new IntPtr(-1), ref secAttrs, GetPageAccess(access) | (int)options, capacity, mapName); - int errorCode = Marshal.GetLastWin32Error(); + int errorCode = Marshal.GetLastPInvokeError(); if (!handle.IsInvalid) { if (errorCode == Interop.Errors.ERROR_ALREADY_EXISTS) @@ -126,7 +126,7 @@ private static SafeMemoryMappedFileHandle CreateOrOpenCore( else { handle.Dispose(); - int createErrorCode = Marshal.GetLastWin32Error(); + int createErrorCode = Marshal.GetLastPInvokeError(); if (createErrorCode != Interop.Errors.ERROR_ACCESS_DENIED) { throw Win32Marshal.GetExceptionForWin32Error(createErrorCode); @@ -146,7 +146,7 @@ private static SafeMemoryMappedFileHandle CreateOrOpenCore( else { handle.Dispose(); - int openErrorCode = Marshal.GetLastWin32Error(); + int openErrorCode = Marshal.GetLastPInvokeError(); if (openErrorCode != Interop.Errors.ERROR_FILE_NOT_FOUND) { throw Win32Marshal.GetExceptionForWin32Error(openErrorCode); diff --git a/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedView.Windows.cs b/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedView.Windows.cs index cc3300c12c9fe0..c47852bbb01409 100644 --- a/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedView.Windows.cs +++ b/src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedView.Windows.cs @@ -62,7 +62,7 @@ public static MemoryMappedView CreateView(SafeMemoryMappedFileHandle memMappedFi IntPtr tempHandle = Interop.VirtualAlloc( viewHandle, (UIntPtr)(nativeSize != MemoryMappedFile.DefaultSize ? nativeSize : viewSize), Interop.Kernel32.MemOptions.MEM_COMMIT, MemoryMappedFile.GetPageAccess(access)); - int lastError = Marshal.GetLastWin32Error(); + int lastError = Marshal.GetLastPInvokeError(); if (viewHandle.IsInvalid) { viewHandle.Dispose(); @@ -110,7 +110,7 @@ public unsafe void Flush(UIntPtr capacity) // increasing intervals. Eventually, however, we need to give up. In ad-hoc tests // this strategy successfully flushed the view after no more than 3 retries. - int error = Marshal.GetLastWin32Error(); + int error = Marshal.GetLastPInvokeError(); if (error != Interop.Errors.ERROR_LOCK_VIOLATION) throw Win32Marshal.GetExceptionForWin32Error(error); @@ -125,7 +125,7 @@ public unsafe void Flush(UIntPtr capacity) if (Interop.Kernel32.FlushViewOfFile((IntPtr)firstPagePtr, capacity)) return; - error = Marshal.GetLastWin32Error(); + error = Marshal.GetLastPInvokeError(); if (error != Interop.Errors.ERROR_LOCK_VIOLATION) throw Win32Marshal.GetExceptionForWin32Error(error); diff --git a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeClientStream.Windows.cs b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeClientStream.Windows.cs index a082a3da863972..51c1e58eea8131 100644 --- a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeClientStream.Windows.cs +++ b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeClientStream.Windows.cs @@ -55,7 +55,7 @@ private bool TryConnect(int timeout, CancellationToken cancellationToken) if (handle.IsInvalid) { - int errorCode = Marshal.GetLastWin32Error(); + int errorCode = Marshal.GetLastPInvokeError(); if (errorCode != Interop.Errors.ERROR_PIPE_BUSY && errorCode != Interop.Errors.ERROR_FILE_NOT_FOUND) @@ -65,7 +65,7 @@ private bool TryConnect(int timeout, CancellationToken cancellationToken) if (!Interop.Kernel32.WaitNamedPipe(_normalizedPipePath, timeout)) { - errorCode = Marshal.GetLastWin32Error(); + errorCode = Marshal.GetLastPInvokeError(); // Server is not yet created or a timeout occurred before a pipe instance was available. if (errorCode == Interop.Errors.ERROR_FILE_NOT_FOUND || @@ -88,7 +88,7 @@ private bool TryConnect(int timeout, CancellationToken cancellationToken) if (handle.IsInvalid) { - errorCode = Marshal.GetLastWin32Error(); + errorCode = Marshal.GetLastPInvokeError(); // Handle the possible race condition of someone else connecting to the server // between our calls to WaitNamedPipe & CreateFile. @@ -123,7 +123,7 @@ public unsafe int NumberOfServerInstances uint numInstances; if (!Interop.Kernel32.GetNamedPipeHandleStateW(InternalHandle!, null, &numInstances, null, null, null, 0)) { - throw WinIOError(Marshal.GetLastWin32Error()); + throw WinIOError(Marshal.GetLastPInvokeError()); } return (int)numInstances; diff --git a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.Win32.cs b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.Win32.cs index 8039665ac0df54..172c44c3885a2a 100644 --- a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.Win32.cs +++ b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.Win32.cs @@ -26,7 +26,7 @@ private unsafe string HandleGetImpersonationUserNameError(int error, uint userNa { return new string(userName); } - error = Marshal.GetLastWin32Error(); + error = Marshal.GetLastPInvokeError(); } throw WinIOError(error); diff --git a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.Windows.cs b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.Windows.cs index a6ed15e1cd379e..3abd2fed0451c9 100644 --- a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.Windows.cs +++ b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.Windows.cs @@ -146,7 +146,7 @@ public void WaitForConnection() { if (!Interop.Kernel32.ConnectNamedPipe(InternalHandle!, IntPtr.Zero)) { - int errorCode = Marshal.GetLastWin32Error(); + int errorCode = Marshal.GetLastPInvokeError(); if (errorCode != Interop.Errors.ERROR_PIPE_CONNECTED) { @@ -211,7 +211,7 @@ public unsafe string GetImpersonationUserName() return new string(userName); } - return HandleGetImpersonationUserNameError(Marshal.GetLastWin32Error(), UserNameMaxLength, userName); + return HandleGetImpersonationUserNameError(Marshal.GetLastPInvokeError(), UserNameMaxLength, userName); } // This method calls a delegate while impersonating the client. Note that we will not have @@ -254,7 +254,7 @@ private static void ImpersonateAndTryCode(object? helper) } else { - execHelper._impersonateErrorCode = Marshal.GetLastWin32Error(); + execHelper._impersonateErrorCode = Marshal.GetLastPInvokeError(); } if (execHelper._mustRevert) @@ -272,7 +272,7 @@ private static void RevertImpersonationOnBackout(object? helper, bool exceptionT { if (!Interop.Advapi32.RevertToSelf()) { - execHelper._revertImpersonateErrorCode = Marshal.GetLastWin32Error(); + execHelper._revertImpersonateErrorCode = Marshal.GetLastPInvokeError(); } } } @@ -306,7 +306,7 @@ private unsafe Task WaitForConnectionCoreAsync(CancellationToken cancellationTok if (!Interop.Kernel32.ConnectNamedPipe(InternalHandle!, completionSource.Overlapped)) { - int errorCode = Marshal.GetLastWin32Error(); + int errorCode = Marshal.GetLastPInvokeError(); switch (errorCode) { diff --git a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeCompletionSource.cs b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeCompletionSource.cs index 118463c35d0f7f..39f47249a691e5 100644 --- a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeCompletionSource.cs +++ b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeCompletionSource.cs @@ -147,7 +147,7 @@ private void Cancel() // This case should not have any consequences although // it will be easier to debug if there exists any special case // we are not aware of. - int errorCode = Marshal.GetLastWin32Error(); + int errorCode = Marshal.GetLastPInvokeError(); Debug.WriteLine("CancelIoEx finished with error code {0}.", errorCode); } } diff --git a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Windows.cs b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Windows.cs index 928cc5d1a081f6..d8c4bf2f8543de 100644 --- a/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Windows.cs +++ b/src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Windows.cs @@ -403,7 +403,7 @@ public void WaitForPipeDrain() // Block until other end of the pipe has read everything. if (!Interop.Kernel32.FlushFileBuffers(_handle!)) { - throw WinIOError(Marshal.GetLastWin32Error()); + throw WinIOError(Marshal.GetLastPInvokeError()); } } @@ -420,7 +420,7 @@ public unsafe virtual PipeTransmissionMode TransmissionMode uint pipeFlags; if (!Interop.Kernel32.GetNamedPipeInfo(_handle!, &pipeFlags, null, null, null)) { - throw WinIOError(Marshal.GetLastWin32Error()); + throw WinIOError(Marshal.GetLastPInvokeError()); } if ((pipeFlags & Interop.Kernel32.PipeOptions.PIPE_TYPE_MESSAGE) != 0) { @@ -453,7 +453,7 @@ public unsafe virtual int InBufferSize uint inBufferSize; if (!Interop.Kernel32.GetNamedPipeInfo(_handle!, null, null, &inBufferSize, null)) { - throw WinIOError(Marshal.GetLastWin32Error()); + throw WinIOError(Marshal.GetLastPInvokeError()); } return (int)inBufferSize; @@ -483,7 +483,7 @@ public unsafe virtual int OutBufferSize } else if (!Interop.Kernel32.GetNamedPipeInfo(_handle!, null, &outBufferSize, null, null)) { - throw WinIOError(Marshal.GetLastWin32Error()); + throw WinIOError(Marshal.GetLastPInvokeError()); } return (int)outBufferSize; @@ -519,7 +519,7 @@ public virtual PipeTransmissionMode ReadMode int pipeReadType = (int)value << 1; if (!Interop.Kernel32.SetNamedPipeHandleState(_handle!, &pipeReadType, IntPtr.Zero, IntPtr.Zero)) { - throw WinIOError(Marshal.GetLastWin32Error()); + throw WinIOError(Marshal.GetLastPInvokeError()); } else { @@ -554,7 +554,7 @@ private unsafe int ReadFileNative(SafePipeHandle handle, Span buffer, Nati if (r == 0) { // In message mode, the ReadFile can inform us that there is more data to come. - errorCode = Marshal.GetLastWin32Error(); + errorCode = Marshal.GetLastPInvokeError(); return errorCode == Interop.Errors.ERROR_MORE_DATA ? numBytesRead : -1; @@ -590,7 +590,7 @@ private unsafe int WriteFileNative(SafePipeHandle handle, ReadOnlySpan buf if (r == 0) { - errorCode = Marshal.GetLastWin32Error(); + errorCode = Marshal.GetLastPInvokeError(); return -1; } else @@ -638,7 +638,7 @@ private unsafe void UpdateReadMode() uint flags; if (!Interop.Kernel32.GetNamedPipeHandleStateW(SafePipeHandle, &flags, null, null, null, null, 0)) { - throw WinIOError(Marshal.GetLastWin32Error()); + throw WinIOError(Marshal.GetLastPInvokeError()); } if ((flags & Interop.Kernel32.PipeOptions.PIPE_READMODE_MESSAGE) != 0) diff --git a/src/libraries/System.IO.Pipes/tests/InteropTest.Windows.cs b/src/libraries/System.IO.Pipes/tests/InteropTest.Windows.cs index 087237e2352c6b..ce6065d6795740 100644 --- a/src/libraries/System.IO.Pipes/tests/InteropTest.Windows.cs +++ b/src/libraries/System.IO.Pipes/tests/InteropTest.Windows.cs @@ -27,7 +27,7 @@ internal static unsafe bool TryGetImpersonationUserName(SafePipeHandle handle, o return true; } - return TryHandleGetImpersonationUserNameError(handle, Marshal.GetLastWin32Error(), UserNameMaxLength, userName, out impersonationUserName); + return TryHandleGetImpersonationUserNameError(handle, Marshal.GetLastPInvokeError(), UserNameMaxLength, userName, out impersonationUserName); } internal static unsafe bool TryGetNumberOfServerInstances(SafePipeHandle handle, out uint numberOfServerInstances) diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/HttpListenerException.cs b/src/libraries/System.Net.HttpListener/src/System/Net/HttpListenerException.cs index 62d3751da7482c..968adb53345d0b 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/HttpListenerException.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/HttpListenerException.cs @@ -11,7 +11,7 @@ namespace System.Net [System.Runtime.CompilerServices.TypeForwardedFrom("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] public class HttpListenerException : Win32Exception { - public HttpListenerException() : base(Marshal.GetLastWin32Error()) + public HttpListenerException() : base(Marshal.GetLastPInvokeError()) { if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(this, NativeErrorCode.ToString() + ":" + Message); } diff --git a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerSession.Windows.cs b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerSession.Windows.cs index 777335be91a983..5c681b745a2f39 100644 --- a/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerSession.Windows.cs +++ b/src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerSession.Windows.cs @@ -52,7 +52,7 @@ public unsafe HttpListenerSession(HttpListener listener) Interop.Kernel32.FileCompletionNotificationModes.SkipCompletionPortOnSuccess | Interop.Kernel32.FileCompletionNotificationModes.SkipSetEventOnHandle)) { - throw new HttpListenerException(Marshal.GetLastWin32Error()); + throw new HttpListenerException(Marshal.GetLastPInvokeError()); } RequestQueueHandle = requestQueueHandle; diff --git a/src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs b/src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs index 3ded687bcf4a3a..574d5577a2cccc 100644 --- a/src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs +++ b/src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs @@ -82,7 +82,7 @@ private Task DoSendPingCore(IPAddress address, byte[] buffer, int tim if (error == 0) { - error = Marshal.GetLastWin32Error(); + error = Marshal.GetLastPInvokeError(); // Only skip Async IO Pending error value. if (!isAsync || error != Interop.IpHlpApi.ERROR_IO_PENDING) diff --git a/src/libraries/System.Net.Primitives/src/System/Net/SocketException.Windows.cs b/src/libraries/System.Net.Primitives/src/System/Net/SocketException.Windows.cs index 6d81c8ef97fe6b..8952b76109403a 100644 --- a/src/libraries/System.Net.Primitives/src/System/Net/SocketException.Windows.cs +++ b/src/libraries/System.Net.Primitives/src/System/Net/SocketException.Windows.cs @@ -9,7 +9,7 @@ namespace System.Net.Sockets public partial class SocketException : Win32Exception { /// Creates a new instance of the class with the default error code. - public SocketException() : this(Marshal.GetLastWin32Error()) + public SocketException() : this(Marshal.GetLastPInvokeError()) { } diff --git a/src/libraries/System.Net.Security/src/System/Net/CertificateValidationPal.Windows.cs b/src/libraries/System.Net.Security/src/System/Net/CertificateValidationPal.Windows.cs index 093cd0719dd091..ca9494b3b023b7 100644 --- a/src/libraries/System.Net.Security/src/System/Net/CertificateValidationPal.Windows.cs +++ b/src/libraries/System.Net.Security/src/System/Net/CertificateValidationPal.Windows.cs @@ -27,7 +27,7 @@ internal static SslPolicyErrors VerifyCertificateProperties( if (!chainBuildResult // Build failed on handle or on policy. && chain.SafeHandle!.DangerousGetHandle() == IntPtr.Zero) // Build failed to generate a valid handle. { - throw new CryptographicException(Marshal.GetLastWin32Error()); + throw new CryptographicException(Marshal.GetLastPInvokeError()); } if (checkCertName) diff --git a/src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/WebSocketException.cs b/src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/WebSocketException.cs index b2e22356410608..d21721fc25420d 100644 --- a/src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/WebSocketException.cs +++ b/src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/WebSocketException.cs @@ -15,7 +15,7 @@ public sealed class WebSocketException : Win32Exception private readonly WebSocketError _webSocketErrorCode; public WebSocketException() - : this(Marshal.GetLastWin32Error()) + : this(Marshal.GetLastPInvokeError()) { } diff --git a/src/libraries/System.Net.WebSockets/tests/WebSocketExceptionTests.cs b/src/libraries/System.Net.WebSockets/tests/WebSocketExceptionTests.cs index 83204359bfd62a..0d5e4d90490c1b 100644 --- a/src/libraries/System.Net.WebSockets/tests/WebSocketExceptionTests.cs +++ b/src/libraries/System.Net.WebSockets/tests/WebSocketExceptionTests.cs @@ -34,7 +34,7 @@ public sealed partial class WebSocketExceptionTests [Fact] public void ConstructorTests_Parameterless_Success() { - int lastError = Marshal.GetLastWin32Error(); + int lastError = Marshal.GetLastPInvokeError(); var wse = new WebSocketException(); Assert.Equal(wse.NativeErrorCode, lastError); Assert.Equal(wse.ErrorCode, lastError); diff --git a/src/libraries/System.Runtime.Extensions/tests/System/EnvironmentTests.cs b/src/libraries/System.Runtime.Extensions/tests/System/EnvironmentTests.cs index 8415493fbf0e17..f027d6859a8172 100644 --- a/src/libraries/System.Runtime.Extensions/tests/System/EnvironmentTests.cs +++ b/src/libraries/System.Runtime.Extensions/tests/System/EnvironmentTests.cs @@ -475,7 +475,7 @@ private void AssertDirectoryExists(string path) FileAttributes attributes = GetFileAttributesW(path); if (attributes == (FileAttributes)(-1)) { - int error = Marshal.GetLastWin32Error(); + int error = Marshal.GetLastPInvokeError(); Assert.False(true, $"error {error} getting attributes for {path}"); } diff --git a/src/libraries/System.Runtime.Handles/tests/SafeHandle.cs b/src/libraries/System.Runtime.Handles/tests/SafeHandle.cs index 4c663184d4d640..80d38992e6d66d 100644 --- a/src/libraries/System.Runtime.Handles/tests/SafeHandle.cs +++ b/src/libraries/System.Runtime.Handles/tests/SafeHandle.cs @@ -113,7 +113,7 @@ public static void SafeHandle_DangerousReleasePreservesLastError() SetLastError(42); handle.DangerousRelease(); - int error = Marshal.GetLastWin32Error(); + int error = Marshal.GetLastPInvokeError(); Assert.Equal(42, error); } } diff --git a/src/libraries/System.Security.Cryptography.Csp/src/System/Security/Cryptography/CapiHelper.Windows.cs b/src/libraries/System.Security.Cryptography.Csp/src/System/Security/Cryptography/CapiHelper.Windows.cs index 913d8397e9f3ae..c78a1721c72a3f 100644 --- a/src/libraries/System.Security.Cryptography.Csp/src/System/Security/Cryptography/CapiHelper.Windows.cs +++ b/src/libraries/System.Security.Cryptography.Csp/src/System/Security/Cryptography/CapiHelper.Windows.cs @@ -768,7 +768,7 @@ internal static SafeKeyHandle GetKeyPairHelper( /// returns the error code internal static int GetErrorCode() { - return Marshal.GetLastWin32Error(); + return Marshal.GetLastPInvokeError(); } /// diff --git a/src/libraries/System.Threading.Overlapped/tests/ThreadPoolBoundHandle_IntegrationTests.cs b/src/libraries/System.Threading.Overlapped/tests/ThreadPoolBoundHandle_IntegrationTests.cs index 85daba2b48e5ad..008bf7aa2a5712 100644 --- a/src/libraries/System.Threading.Overlapped/tests/ThreadPoolBoundHandle_IntegrationTests.cs +++ b/src/libraries/System.Threading.Overlapped/tests/ThreadPoolBoundHandle_IntegrationTests.cs @@ -32,7 +32,7 @@ public unsafe void SingleOperationOverSingleHandle() if (retval == 0) { - Assert.Equal(DllImport.ERROR_IO_PENDING, Marshal.GetLastWin32Error()); + Assert.Equal(DllImport.ERROR_IO_PENDING, Marshal.GetLastPInvokeError()); } // Wait for overlapped operation to complete @@ -77,7 +77,7 @@ public unsafe void MultipleOperationsOverSingleHandle() if (retval == 0) { - Assert.Equal(DllImport.ERROR_IO_PENDING, Marshal.GetLastWin32Error()); + Assert.Equal(DllImport.ERROR_IO_PENDING, Marshal.GetLastPInvokeError()); } @@ -87,7 +87,7 @@ public unsafe void MultipleOperationsOverSingleHandle() if (retval == 0) { - Assert.Equal(DllImport.ERROR_IO_PENDING, Marshal.GetLastWin32Error()); + Assert.Equal(DllImport.ERROR_IO_PENDING, Marshal.GetLastPInvokeError()); } // Wait for overlapped operations to complete @@ -142,7 +142,7 @@ public unsafe void MultipleOperationsOverMultipleHandles() if (retval == 0) { - Assert.Equal(DllImport.ERROR_IO_PENDING, Marshal.GetLastWin32Error()); + Assert.Equal(DllImport.ERROR_IO_PENDING, Marshal.GetLastPInvokeError()); } @@ -150,7 +150,7 @@ public unsafe void MultipleOperationsOverMultipleHandles() if (retval == 0) { - Assert.Equal(DllImport.ERROR_IO_PENDING, Marshal.GetLastWin32Error()); + Assert.Equal(DllImport.ERROR_IO_PENDING, Marshal.GetLastPInvokeError()); } // Wait for overlapped operations to complete @@ -209,7 +209,7 @@ public unsafe void FlowsAsyncLocalsToCallback() if (retval == 0) { - Assert.Equal(DllImport.ERROR_IO_PENDING, Marshal.GetLastWin32Error()); + Assert.Equal(DllImport.ERROR_IO_PENDING, Marshal.GetLastPInvokeError()); } // Wait for overlapped operation to complete