Task/use get last p invoke error (#52212)
authorKareem Zedan <48921114+KZedan@users.noreply.github.com>
Sun, 9 May 2021 19:42:21 +0000 (20:42 +0100)
committerGitHub <noreply@github.com>
Sun, 9 May 2021 19:42:21 +0000 (12:42 -0700)
* 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 <jkotas@microsoft.com>
27 files changed:
src/libraries/Microsoft.Win32.Primitives/src/System/ComponentModel/Win32Exception.cs
src/libraries/Microsoft.Win32.Primitives/tests/Win32ExceptionTests.cs
src/libraries/System.Console/src/System/ConsolePal.Windows.cs
src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/DriveInfo.Windows.cs
src/libraries/System.IO.FileSystem.DriveInfo/src/System/IO/Error.cs
src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.OSX.cs
src/libraries/System.IO.FileSystem/tests/PortedCommon/IOServices.cs
src/libraries/System.IO.FileSystem/tests/PortedCommon/ReparsePointUtilities.cs
src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedFile.Windows.cs
src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/MemoryMappedView.Windows.cs
src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeClientStream.Windows.cs
src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.Win32.cs
src/libraries/System.IO.Pipes/src/System/IO/Pipes/NamedPipeServerStream.Windows.cs
src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeCompletionSource.cs
src/libraries/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Windows.cs
src/libraries/System.IO.Pipes/tests/InteropTest.Windows.cs
src/libraries/System.Net.HttpListener/src/System/Net/HttpListenerException.cs
src/libraries/System.Net.HttpListener/src/System/Net/Windows/HttpListenerSession.Windows.cs
src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs
src/libraries/System.Net.Primitives/src/System/Net/SocketException.Windows.cs
src/libraries/System.Net.Security/src/System/Net/CertificateValidationPal.Windows.cs
src/libraries/System.Net.WebSockets/src/System/Net/WebSockets/WebSocketException.cs
src/libraries/System.Net.WebSockets/tests/WebSocketExceptionTests.cs
src/libraries/System.Runtime.Extensions/tests/System/EnvironmentTests.cs
src/libraries/System.Runtime.Handles/tests/SafeHandle.cs
src/libraries/System.Security.Cryptography.Csp/src/System/Security/Cryptography/CapiHelper.Windows.cs
src/libraries/System.Threading.Overlapped/tests/ThreadPoolBoundHandle_IntegrationTests.cs

index a2b1644..bf78cbc 100644 (file)
@@ -21,7 +21,7 @@ namespace System.ComponentModel
         /// 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())
         {
         }
 
@@ -43,7 +43,7 @@ namespace System.ComponentModel
         /// <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)
         {
         }
 
@@ -53,7 +53,7 @@ namespace System.ComponentModel
         /// </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)
index 04a1dfc..27bdd08 100644 (file)
@@ -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;
index dd74a7d..95cebc1 100644 (file)
@@ -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;
index d361611..bb495b0 100644 (file)
@@ -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);
index 90998a8..bf81410 100644 (file)
@@ -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);
         }
 
index a976a9a..7c75553 100644 (file)
@@ -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
index 3fb3066..27b923d 100644 (file)
@@ -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;
index d2304e1..5423bda 100644 (file)
@@ -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
index ad08669..8bc32eb 100644 (file)
@@ -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);
index cc3300c..c47852b 100644 (file)
@@ -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);
 
index a082a3d..51c1e58 100644 (file)
@@ -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;
index 8039665..172c44c 100644 (file)
@@ -26,7 +26,7 @@ namespace System.IO.Pipes
                 {
                     return new string(userName);
                 }
-                error = Marshal.GetLastWin32Error();
+                error = Marshal.GetLastPInvokeError();
             }
 
             throw WinIOError(error);
index a6ed15e..3abd2fe 100644 (file)
@@ -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)
                 {
index 118463c..39f4724 100644 (file)
@@ -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);
             }
         }
index 928cc5d..d8c4bf2 100644 (file)
@@ -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)
index 087237e..ce6065d 100644 (file)
@@ -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)
index 62d3751..968adb5 100644 (file)
@@ -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);
         }
index 777335b..5c681b7 100644 (file)
@@ -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;
index 3ded687..574d557 100644 (file)
@@ -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)
index 6d81c8e..8952b76 100644 (file)
@@ -9,7 +9,7 @@ namespace System.Net.Sockets
     public partial class SocketException : Win32Exception
     {
         /// <summary>Creates a new instance of the <see cref='System.Net.Sockets.SocketException'/> class with the default error code.</summary>
-        public SocketException() : this(Marshal.GetLastWin32Error())
+        public SocketException() : this(Marshal.GetLastPInvokeError())
         {
         }
 
index 093cd07..ca9494b 100644 (file)
@@ -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)
index b2e2235..d21721f 100644 (file)
@@ -15,7 +15,7 @@ namespace System.Net.WebSockets
         private readonly WebSocketError _webSocketErrorCode;
 
         public WebSocketException()
-            : this(Marshal.GetLastWin32Error())
+            : this(Marshal.GetLastPInvokeError())
         {
         }
 
index 8320435..0d5e4d9 100644 (file)
@@ -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);
index 8415493..f027d68 100644 (file)
@@ -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}");
             }
 
index 4c66318..80d3899 100644 (file)
@@ -113,7 +113,7 @@ public partial class SafeHandleTests
         SetLastError(42);
         handle.DangerousRelease();
 
-        int error = Marshal.GetLastWin32Error();
+        int error = Marshal.GetLastPInvokeError();
         Assert.Equal(42, error);
     }
 }
index 913d839..c78a172 100644 (file)
@@ -768,7 +768,7 @@ namespace Internal.NativeCrypto
         /// <returns>returns the error code</returns>
         internal static int GetErrorCode()
         {
-            return Marshal.GetLastWin32Error();
+            return Marshal.GetLastPInvokeError();
         }
 
         /// <summary>
index 85daba2..008bf7a 100644 (file)
@@ -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