diff --git a/src/libraries/System.Private.CoreLib/src/Internal/IO/File.Windows.cs b/src/libraries/System.Private.CoreLib/src/Internal/IO/File.Windows.cs index eed758083f17f..354384be17ec0 100644 --- a/src/libraries/System.Private.CoreLib/src/Internal/IO/File.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/Internal/IO/File.Windows.cs @@ -30,7 +30,7 @@ internal static int FillAttributeInfo(string path, ref Interop.Kernel32.WIN32_FI { if (!Interop.Kernel32.GetFileAttributesEx(path, Interop.Kernel32.GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, ref data)) { - errorCode = Marshal.GetLastWin32Error(); + errorCode = Marshal.GetLastPInvokeError(); if (errorCode == Interop.Errors.ERROR_ACCESS_DENIED) { // Files that are marked for deletion will not let you GetFileAttributes, @@ -43,7 +43,7 @@ internal static int FillAttributeInfo(string path, ref Interop.Kernel32.WIN32_FI { if (handle.IsInvalid) { - errorCode = Marshal.GetLastWin32Error(); + errorCode = Marshal.GetLastPInvokeError(); } else { diff --git a/src/libraries/System.Private.CoreLib/src/System/Environment.Variables.Windows.cs b/src/libraries/System.Private.CoreLib/src/System/Environment.Variables.Windows.cs index 89e4bc7f45554..4531aa133b7ea 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Environment.Variables.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Environment.Variables.Windows.cs @@ -20,7 +20,7 @@ public static partial class Environment builder.EnsureCapacity((int)length); } - if (length == 0 && Marshal.GetLastWin32Error() == Interop.Errors.ERROR_ENVVAR_NOT_FOUND) + if (length == 0 && Marshal.GetLastPInvokeError() == Interop.Errors.ERROR_ENVVAR_NOT_FOUND) { builder.Dispose(); return null; @@ -34,7 +34,7 @@ private static void SetEnvironmentVariableCore(string variable, string? value) { if (!Interop.Kernel32.SetEnvironmentVariable(variable, value)) { - int errorCode = Marshal.GetLastWin32Error(); + int errorCode = Marshal.GetLastPInvokeError(); switch (errorCode) { case Interop.Errors.ERROR_ENVVAR_NOT_FOUND: diff --git a/src/libraries/System.Private.CoreLib/src/System/Environment.Win32.cs b/src/libraries/System.Private.CoreLib/src/System/Environment.Win32.cs index ca769085b173f..a499d8984371e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Environment.Win32.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Environment.Win32.cs @@ -56,7 +56,7 @@ private static void SetEnvironmentVariableFromRegistry(string variable, string? fixed (char* lParam = "Environment") { IntPtr r = Interop.User32.SendMessageTimeout(new IntPtr(Interop.User32.HWND_BROADCAST), Interop.User32.WM_SETTINGCHANGE, IntPtr.Zero, (IntPtr)lParam, 0, 1000, out IntPtr _); - Debug.Assert(r != IntPtr.Zero, "SetEnvironmentVariable failed: " + Marshal.GetLastWin32Error()); + Debug.Assert(r != IntPtr.Zero, "SetEnvironmentVariable failed: " + Marshal.GetLastPInvokeError()); } } } @@ -139,7 +139,7 @@ private static void GetUserName(ref ValueStringBuilder builder) uint size = 0; while (Interop.Secur32.GetUserNameExW(Interop.Secur32.NameSamCompatible, ref builder.GetPinnableReference(), ref size) == Interop.BOOLEAN.FALSE) { - if (Marshal.GetLastWin32Error() == Interop.Errors.ERROR_MORE_DATA) + if (Marshal.GetLastPInvokeError() == Interop.Errors.ERROR_MORE_DATA) { builder.EnsureCapacity(checked((int)size)); } @@ -186,7 +186,7 @@ public static string UserDomainName while (!Interop.Advapi32.LookupAccountNameW(null, ref builder.GetPinnableReference(), ref MemoryMarshal.GetReference(sid), ref sidLength, ref domainBuilder.GetPinnableReference(), ref length, out _)) { - int error = Marshal.GetLastWin32Error(); + int error = Marshal.GetLastPInvokeError(); // The docs don't call this out clearly, but experimenting shows that the error returned is the following. if (error != Interop.Errors.ERROR_INSUFFICIENT_BUFFER) diff --git a/src/libraries/System.Private.CoreLib/src/System/Environment.Windows.cs b/src/libraries/System.Private.CoreLib/src/System/Environment.Windows.cs index b769f59cc2df2..ca9dfc0aa9711 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Environment.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Environment.Windows.cs @@ -41,7 +41,7 @@ private static string CurrentDirectoryCore { if (!Interop.Kernel32.SetCurrentDirectory(value)) { - int errorCode = Marshal.GetLastWin32Error(); + int errorCode = Marshal.GetLastPInvokeError(); throw Win32Marshal.GetExceptionForWin32Error( errorCode == Interop.Errors.ERROR_FILE_NOT_FOUND ? Interop.Errors.ERROR_PATH_NOT_FOUND : errorCode, value); diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Nls.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Nls.cs index 396b3b0c699a5..1dc301b970aa5 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Nls.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Nls.cs @@ -68,7 +68,7 @@ private static unsafe int FindStringOrdinal( Debug.Assert(ret >= -1 && ret <= source.Length); // SetLastError is only performed under debug builds. - Debug.Assert(ret >= 0 || Marshal.GetLastWin32Error() == Interop.Errors.ERROR_SUCCESS); + Debug.Assert(ret >= 0 || Marshal.GetLastPInvokeError() == Interop.Errors.ERROR_SUCCESS); return ret; } @@ -300,7 +300,7 @@ private unsafe int FindString( Debug.Assert(result >= -1 && result <= lpStringSource.Length); // SetLastError is only performed under debug builds. - Debug.Assert(result >= 0 || Marshal.GetLastWin32Error() == Interop.Errors.ERROR_SUCCESS); + Debug.Assert(result >= 0 || Marshal.GetLastPInvokeError() == Interop.Errors.ERROR_SUCCESS); return result; } @@ -506,7 +506,7 @@ private unsafe int NlsGetSortKey(ReadOnlySpan source, Span destinati // to allocate a temporary buffer large enough to hold intermediate state, // or the destination buffer being too small. - if (Marshal.GetLastWin32Error() == Interop.Errors.ERROR_INSUFFICIENT_BUFFER) + if (Marshal.GetLastPInvokeError() == Interop.Errors.ERROR_INSUFFICIENT_BUFFER) { ThrowHelper.ThrowArgumentException_DestinationTooShort(); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/IdnMapping.Nls.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/IdnMapping.Nls.cs index 2e0e664520c04..baaa612cf1fb0 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/IdnMapping.Nls.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/IdnMapping.Nls.cs @@ -117,7 +117,7 @@ private uint NlsFlags [DoesNotReturn] private static void ThrowForZeroLength(bool unicode) { - int lastError = Marshal.GetLastWin32Error(); + int lastError = Marshal.GetLastPInvokeError(); throw new ArgumentException( lastError == Interop.Errors.ERROR_INVALID_NAME ? SR.Argument_IdnIllegalName : diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/Normalization.Nls.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/Normalization.Nls.cs index 76b0983c8f233..3abea57252989 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/Normalization.Nls.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/Normalization.Nls.cs @@ -25,7 +25,7 @@ private static unsafe bool NlsIsNormalized(string strInput, NormalizationForm no result = Interop.Normaliz.IsNormalizedString(normalizationForm, pInput, strInput.Length); } - int lastError = Marshal.GetLastWin32Error(); + int lastError = Marshal.GetLastPInvokeError(); switch (lastError) { case Interop.Errors.ERROR_SUCCESS: @@ -84,7 +84,7 @@ private static unsafe string NlsNormalize(string strInput, NormalizationForm nor { realLength = Interop.Normaliz.NormalizeString(normalizationForm, pInput, strInput.Length, pDest, buffer.Length); } - int lastError = Marshal.GetLastWin32Error(); + int lastError = Marshal.GetLastPInvokeError(); switch (lastError) { diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/FileStreamHelpers.Windows.cs b/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/FileStreamHelpers.Windows.cs index 18079bb923ab6..f33e47b0fe15c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/FileStreamHelpers.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/FileStreamHelpers.Windows.cs @@ -176,7 +176,7 @@ private static SafeFileHandle ValidateFileHandle(SafeFileHandle fileHandle, stri // NT5 oddity - when trying to open "C:\" as a Win32FileStream, // we usually get ERROR_PATH_NOT_FOUND from the OS. We should // probably be consistent w/ every other directory. - int errorCode = Marshal.GetLastWin32Error(); + int errorCode = Marshal.GetLastPInvokeError(); if (errorCode == Interop.Errors.ERROR_PATH_NOT_FOUND && path!.Length == PathInternal.GetRootLength(path)) errorCode = Interop.Errors.ERROR_ACCESS_DENIED; @@ -229,7 +229,7 @@ internal static long Seek(SafeFileHandle handle, string? path, long offset, Seek internal static int GetLastWin32ErrorAndDisposeHandleIfInvalid(SafeFileHandle handle) { - int errorCode = Marshal.GetLastWin32Error(); + int errorCode = Marshal.GetLastPInvokeError(); // If ERROR_INVALID_HANDLE is returned, it doesn't suffice to set // the handle as invalid; the handle must also be closed. @@ -294,7 +294,7 @@ internal static void ValidateFileTypeForNonExtendedPaths(SafeFileHandle handle, if (fileType != Interop.Kernel32.FileTypes.FILE_TYPE_DISK) { int errorCode = fileType == Interop.Kernel32.FileTypes.FILE_TYPE_UNKNOWN - ? Marshal.GetLastWin32Error() + ? Marshal.GetLastPInvokeError() : Interop.Errors.ERROR_SUCCESS; handle.Dispose(); @@ -333,7 +333,7 @@ internal static unsafe void SetFileLength(SafeFileHandle handle, string? path, l &eofInfo, (uint)sizeof(Interop.Kernel32.FILE_END_OF_FILE_INFO))) { - int errorCode = Marshal.GetLastWin32Error(); + int errorCode = Marshal.GetLastPInvokeError(); if (errorCode == Interop.Errors.ERROR_INVALID_PARAMETER) throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_FileLengthTooBig); throw Win32Marshal.GetExceptionForWin32Error(errorCode, path); diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/Net5CompatFileStreamStrategy.CompletionSource.Windows.cs b/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/Net5CompatFileStreamStrategy.CompletionSource.Windows.cs index de5af4778a198..2c8a13feace0c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/Net5CompatFileStreamStrategy.CompletionSource.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/Strategies/Net5CompatFileStreamStrategy.CompletionSource.Windows.cs @@ -200,7 +200,7 @@ private static void Cancel(object? state) if (!completionSource._strategy._fileHandle.IsInvalid && !Interop.Kernel32.CancelIoEx(completionSource._strategy._fileHandle, completionSource._overlapped)) { - int errorCode = Marshal.GetLastWin32Error(); + int errorCode = Marshal.GetLastPInvokeError(); // ERROR_NOT_FOUND is returned if CancelIoEx cannot find the request to cancel. // This probably means that the IO operation has completed. diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs index 4d022792ba7ee..64269561d7e01 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs @@ -1173,7 +1173,7 @@ public static IntPtr GetFunctionPointerForDelegate(TDelegate d) where public static int GetHRForLastWin32Error() { - int dwLastError = GetLastWin32Error(); + int dwLastError = GetLastPInvokeError(); if ((dwLastError & 0x80000000) == 0x80000000) { return dwLastError; diff --git a/src/libraries/System.Private.CoreLib/src/System/Security/SecureString.Windows.cs b/src/libraries/System.Private.CoreLib/src/System/Security/SecureString.Windows.cs index a8067c5f80c22..7dc9104ff4897 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Security/SecureString.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Security/SecureString.Windows.cs @@ -26,7 +26,7 @@ private void ProtectMemory() !_encrypted && !Interop.Crypt32.CryptProtectMemory(_buffer, (uint)_buffer.ByteLength, Interop.Crypt32.CRYPTPROTECTMEMORY_SAME_PROCESS)) { - throw new CryptographicException(Marshal.GetLastWin32Error()); + throw new CryptographicException(Marshal.GetLastPInvokeError()); } _encrypted = true; @@ -41,7 +41,7 @@ private void UnprotectMemory() _encrypted && !Interop.Crypt32.CryptUnprotectMemory(_buffer, (uint)_buffer.ByteLength, Interop.Crypt32.CRYPTPROTECTMEMORY_SAME_PROCESS)) { - throw new CryptographicException(Marshal.GetLastWin32Error()); + throw new CryptographicException(Marshal.GetLastPInvokeError()); } _encrypted = false; diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/EventWaitHandle.Windows.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/EventWaitHandle.Windows.cs index 73256bca70bca..20cb741435f0c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/EventWaitHandle.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/EventWaitHandle.Windows.cs @@ -28,7 +28,7 @@ private void CreateEventCore(bool initialState, EventResetMode mode, string? nam SafeWaitHandle handle = Interop.Kernel32.CreateEventEx(IntPtr.Zero, name, eventFlags, AccessRights); - int errorCode = Marshal.GetLastWin32Error(); + int errorCode = Marshal.GetLastPInvokeError(); if (handle.IsInvalid) { handle.SetHandleAsInvalid(); @@ -54,7 +54,7 @@ private static OpenExistingResult OpenExistingWorker(string name, out EventWaitH if (myHandle.IsInvalid) { - int errorCode = Marshal.GetLastWin32Error(); + int errorCode = Marshal.GetLastPInvokeError(); if (errorCode == Interop.Errors.ERROR_FILE_NOT_FOUND || errorCode == Interop.Errors.ERROR_INVALID_NAME) return OpenExistingResult.NameNotFound; diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelLifoSemaphore.Windows.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelLifoSemaphore.Windows.cs index 48564580ae126..c49e9dce80c38 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelLifoSemaphore.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelLifoSemaphore.Windows.cs @@ -26,7 +26,7 @@ private void Create(int maximumSignalCount) Interop.Kernel32.CreateIoCompletionPort(new IntPtr(-1), IntPtr.Zero, UIntPtr.Zero, maximumSignalCount); if (_completionPort == IntPtr.Zero) { - int error = Marshal.GetLastWin32Error(); + int error = Marshal.GetLastPInvokeError(); var exception = new OutOfMemoryException(); exception.HResult = error; throw exception; @@ -46,7 +46,7 @@ public bool WaitCore(int timeoutMs) Debug.Assert(timeoutMs >= -1); bool success = Interop.Kernel32.GetQueuedCompletionStatus(_completionPort, out int numberOfBytes, out UIntPtr completionKey, out IntPtr pointerToOverlapped, timeoutMs); - Debug.Assert(success || (Marshal.GetLastWin32Error() == WaitHandle.WaitTimeout)); + Debug.Assert(success || (Marshal.GetLastPInvokeError() == WaitHandle.WaitTimeout)); return success; } @@ -58,7 +58,7 @@ public void ReleaseCore(int count) { if (!Interop.Kernel32.PostQueuedCompletionStatus(_completionPort, 1, UIntPtr.Zero, IntPtr.Zero)) { - int lastError = Marshal.GetLastWin32Error(); + int lastError = Marshal.GetLastPInvokeError(); var exception = new OutOfMemoryException(); exception.HResult = lastError; throw exception; diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Mutex.Windows.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Mutex.Windows.cs index cc444faf68a3f..48f23264a7f6c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Mutex.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Mutex.Windows.cs @@ -19,7 +19,7 @@ private void CreateMutexCore(bool initiallyOwned, string? name, out bool created { uint mutexFlags = initiallyOwned ? Interop.Kernel32.CREATE_MUTEX_INITIAL_OWNER : 0; SafeWaitHandle mutexHandle = Interop.Kernel32.CreateMutexEx(IntPtr.Zero, name, mutexFlags, AccessRights); - int errorCode = Marshal.GetLastWin32Error(); + int errorCode = Marshal.GetLastPInvokeError(); if (mutexHandle.IsInvalid) { @@ -60,7 +60,7 @@ private static OpenExistingResult OpenExistingWorker(string name, out Mutex? res if (myHandle.IsInvalid) { - int errorCode = Marshal.GetLastWin32Error(); + int errorCode = Marshal.GetLastPInvokeError(); #if TARGET_UNIX || TARGET_BROWSER if (errorCode == Interop.Errors.ERROR_FILENAME_EXCED_RANGE) { diff --git a/src/libraries/System.Private.CoreLib/src/System/Threading/Semaphore.Windows.cs b/src/libraries/System.Private.CoreLib/src/System/Threading/Semaphore.Windows.cs index 9cef42fea0f5a..d92e9c9f88771 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Threading/Semaphore.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Threading/Semaphore.Windows.cs @@ -29,7 +29,7 @@ private void CreateSemaphoreCore(int initialCount, int maximumCount, string? nam #endif SafeWaitHandle myHandle = Interop.Kernel32.CreateSemaphoreEx(IntPtr.Zero, initialCount, maximumCount, name, 0, AccessRights); - int errorCode = Marshal.GetLastWin32Error(); + int errorCode = Marshal.GetLastPInvokeError(); if (myHandle.IsInvalid) { if (!string.IsNullOrEmpty(name) && errorCode == Interop.Errors.ERROR_INVALID_HANDLE) @@ -56,7 +56,7 @@ private static OpenExistingResult OpenExistingWorker(string name, out Semaphore? if (myHandle.IsInvalid) { result = null; - int errorCode = Marshal.GetLastWin32Error(); + int errorCode = Marshal.GetLastPInvokeError(); if (errorCode == Interop.Errors.ERROR_FILE_NOT_FOUND || errorCode == Interop.Errors.ERROR_INVALID_NAME) return OpenExistingResult.NameNotFound;