From: Kareem Zedan <48921114+KZedan@users.noreply.github.com> Date: Sun, 9 May 2021 19:42:21 +0000 (+0100) Subject: Task/use get last p invoke error (#52212) X-Git-Tag: submit/tizen/20210909.063632~1509 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b08a97afb8270511c39fe26d37038877bb63a09b;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Task/use get last p invoke error (#52212) * 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 --- 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 a2b1644..bf78cbc 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 @@ namespace System.ComponentModel /// 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 @@ namespace System.ComponentModel /// /// 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 @@ namespace System.ComponentModel /// 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 04a1dfc..27bdd08 100644 --- a/src/libraries/Microsoft.Win32.Primitives/tests/Win32ExceptionTests.cs +++ b/src/libraries/Microsoft.Win32.Primitives/tests/Win32ExceptionTests.cs @@ -38,7 +38,7 @@ namespace System.ComponentModel.Tests 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 dd74a7d..95cebc1 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 @@ namespace System if (enc.CodePage != UnicodeCodePage) { if (!Interop.Kernel32.SetConsoleCP(enc.CodePage)) - throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); + throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError()); } } @@ -123,7 +123,7 @@ namespace System if (enc.CodePage != UnicodeCodePage) { if (!Interop.Kernel32.SetConsoleOutputCP(enc.CodePage)) - throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); + throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError()); } } @@ -280,7 +280,7 @@ namespace System 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 @@ namespace System 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 @@ namespace System 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 @@ namespace System 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 @@ namespace System } if (!Interop.Kernel32.SetConsoleMode(handle, mode)) - throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); + throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError()); } } @@ -534,7 +534,7 @@ namespace System { 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 @@ namespace System 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 @@ namespace System { 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 @@ namespace System { 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 @@ namespace System if (result == 0) { - int error = Marshal.GetLastWin32Error(); + int error = Marshal.GetLastPInvokeError(); switch (error) { case Interop.Errors.ERROR_INSUFFICIENT_BUFFER: @@ -639,7 +639,7 @@ namespace System set { if (!Interop.Kernel32.SetConsoleTitle(value)) - throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); + throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError()); } } @@ -715,7 +715,7 @@ namespace System 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 @@ namespace System 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 @@ namespace System 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 @@ namespace System 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 @@ namespace System 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 @@ namespace System size.Y = (short)height; if (!Interop.Kernel32.SetConsoleScreenBufferSize(OutputHandle, size)) { - throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); + throw Win32Marshal.GetExceptionForWin32Error(Marshal.GetLastPInvokeError()); } } @@ -951,7 +951,7 @@ namespace System 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 @@ namespace System 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 @@ namespace System 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 @@ namespace System !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 @@ namespace System // 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 @@ namespace System // 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 d361611..bb495b0 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 @@ namespace System.IO 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 90998a8..bf81410 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 @@ namespace System.IO // 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 a976a9a..7c75553 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 @@ -350,7 +350,7 @@ namespace System.IO 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 3fb3066..27b923d 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 @@ internal class IOServices 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 @@ internal class IOServices 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 d2304e1..5423bda 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 class MountHelper 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 class MountHelper 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 ad08669..8bc32eb 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 @@ namespace System.IO.MemoryMappedFiles 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 @@ namespace System.IO.MemoryMappedFiles 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 @@ namespace System.IO.MemoryMappedFiles 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 cc3300c..c47852b 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 @@ namespace System.IO.MemoryMappedFiles 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 @@ namespace System.IO.MemoryMappedFiles // 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 @@ namespace System.IO.MemoryMappedFiles 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 a082a3d..51c1e58 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 @@ namespace System.IO.Pipes 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 @@ namespace System.IO.Pipes 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 @@ namespace System.IO.Pipes 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 @@ namespace System.IO.Pipes 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 8039665..172c44c 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 @@ namespace System.IO.Pipes { 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 a6ed15e..3abd2fe 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 @@ namespace System.IO.Pipes { 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 @@ namespace System.IO.Pipes 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 @@ namespace System.IO.Pipes } else { - execHelper._impersonateErrorCode = Marshal.GetLastWin32Error(); + execHelper._impersonateErrorCode = Marshal.GetLastPInvokeError(); } if (execHelper._mustRevert) @@ -272,7 +272,7 @@ namespace System.IO.Pipes { if (!Interop.Advapi32.RevertToSelf()) { - execHelper._revertImpersonateErrorCode = Marshal.GetLastWin32Error(); + execHelper._revertImpersonateErrorCode = Marshal.GetLastPInvokeError(); } } } @@ -306,7 +306,7 @@ namespace System.IO.Pipes 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 118463c..39f4724 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 @@ namespace System.IO.Pipes // 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 928cc5d..d8c4bf2 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 @@ namespace System.IO.Pipes // 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 @@ namespace System.IO.Pipes 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 @@ namespace System.IO.Pipes 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 @@ namespace System.IO.Pipes } 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 @@ namespace System.IO.Pipes 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 @@ namespace System.IO.Pipes 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 @@ namespace System.IO.Pipes if (r == 0) { - errorCode = Marshal.GetLastWin32Error(); + errorCode = Marshal.GetLastPInvokeError(); return -1; } else @@ -638,7 +638,7 @@ namespace System.IO.Pipes 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 087237e..ce6065d 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 @@ namespace System.IO.Pipes.Tests 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 62d3751..968adb5 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 777335b..5c681b7 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 @@ namespace System.Net 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 3ded687..574d557 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 @@ namespace System.Net.NetworkInformation 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 6d81c8e..8952b76 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 093cd07..ca9494b 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 @@ namespace System.Net 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 b2e2235..d21721f 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 @@ namespace System.Net.WebSockets 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 8320435..0d5e4d9 100644 --- a/src/libraries/System.Net.WebSockets/tests/WebSocketExceptionTests.cs +++ b/src/libraries/System.Net.WebSockets/tests/WebSocketExceptionTests.cs @@ -34,7 +34,7 @@ namespace System.Net.WebSockets.Tests [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 8415493..f027d68 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 @@ namespace System.Tests 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 4c66318..80d3899 100644 --- a/src/libraries/System.Runtime.Handles/tests/SafeHandle.cs +++ b/src/libraries/System.Runtime.Handles/tests/SafeHandle.cs @@ -113,7 +113,7 @@ public partial class SafeHandleTests 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 913d839..c78a172 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 @@ namespace Internal.NativeCrypto /// 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 85daba2..008bf7a 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 partial class ThreadPoolBoundHandleTests 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 partial class ThreadPoolBoundHandleTests if (retval == 0) { - Assert.Equal(DllImport.ERROR_IO_PENDING, Marshal.GetLastWin32Error()); + Assert.Equal(DllImport.ERROR_IO_PENDING, Marshal.GetLastPInvokeError()); } @@ -87,7 +87,7 @@ public partial class ThreadPoolBoundHandleTests 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 partial class ThreadPoolBoundHandleTests if (retval == 0) { - Assert.Equal(DllImport.ERROR_IO_PENDING, Marshal.GetLastWin32Error()); + Assert.Equal(DllImport.ERROR_IO_PENDING, Marshal.GetLastPInvokeError()); } @@ -150,7 +150,7 @@ public partial class ThreadPoolBoundHandleTests 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 partial class ThreadPoolBoundHandleTests 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