Skip to content

Commit a5e626a

Browse files
authored
Task/use get last p invoke error (#52003)
* remove Marshal.GetLastWin32Error
1 parent 6d02eb2 commit a5e626a

15 files changed

+31
-31
lines changed

src/libraries/System.Private.CoreLib/src/Internal/IO/File.Windows.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal static int FillAttributeInfo(string path, ref Interop.Kernel32.WIN32_FI
3030
{
3131
if (!Interop.Kernel32.GetFileAttributesEx(path, Interop.Kernel32.GET_FILEEX_INFO_LEVELS.GetFileExInfoStandard, ref data))
3232
{
33-
errorCode = Marshal.GetLastWin32Error();
33+
errorCode = Marshal.GetLastPInvokeError();
3434
if (errorCode == Interop.Errors.ERROR_ACCESS_DENIED)
3535
{
3636
// 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
4343
{
4444
if (handle.IsInvalid)
4545
{
46-
errorCode = Marshal.GetLastWin32Error();
46+
errorCode = Marshal.GetLastPInvokeError();
4747
}
4848
else
4949
{

src/libraries/System.Private.CoreLib/src/System/Environment.Variables.Windows.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static partial class Environment
2020
builder.EnsureCapacity((int)length);
2121
}
2222

23-
if (length == 0 && Marshal.GetLastWin32Error() == Interop.Errors.ERROR_ENVVAR_NOT_FOUND)
23+
if (length == 0 && Marshal.GetLastPInvokeError() == Interop.Errors.ERROR_ENVVAR_NOT_FOUND)
2424
{
2525
builder.Dispose();
2626
return null;
@@ -34,7 +34,7 @@ private static void SetEnvironmentVariableCore(string variable, string? value)
3434
{
3535
if (!Interop.Kernel32.SetEnvironmentVariable(variable, value))
3636
{
37-
int errorCode = Marshal.GetLastWin32Error();
37+
int errorCode = Marshal.GetLastPInvokeError();
3838
switch (errorCode)
3939
{
4040
case Interop.Errors.ERROR_ENVVAR_NOT_FOUND:

src/libraries/System.Private.CoreLib/src/System/Environment.Win32.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private static void SetEnvironmentVariableFromRegistry(string variable, string?
5656
fixed (char* lParam = "Environment")
5757
{
5858
IntPtr r = Interop.User32.SendMessageTimeout(new IntPtr(Interop.User32.HWND_BROADCAST), Interop.User32.WM_SETTINGCHANGE, IntPtr.Zero, (IntPtr)lParam, 0, 1000, out IntPtr _);
59-
Debug.Assert(r != IntPtr.Zero, "SetEnvironmentVariable failed: " + Marshal.GetLastWin32Error());
59+
Debug.Assert(r != IntPtr.Zero, "SetEnvironmentVariable failed: " + Marshal.GetLastPInvokeError());
6060
}
6161
}
6262
}
@@ -139,7 +139,7 @@ private static void GetUserName(ref ValueStringBuilder builder)
139139
uint size = 0;
140140
while (Interop.Secur32.GetUserNameExW(Interop.Secur32.NameSamCompatible, ref builder.GetPinnableReference(), ref size) == Interop.BOOLEAN.FALSE)
141141
{
142-
if (Marshal.GetLastWin32Error() == Interop.Errors.ERROR_MORE_DATA)
142+
if (Marshal.GetLastPInvokeError() == Interop.Errors.ERROR_MORE_DATA)
143143
{
144144
builder.EnsureCapacity(checked((int)size));
145145
}
@@ -186,7 +186,7 @@ public static string UserDomainName
186186
while (!Interop.Advapi32.LookupAccountNameW(null, ref builder.GetPinnableReference(), ref MemoryMarshal.GetReference(sid),
187187
ref sidLength, ref domainBuilder.GetPinnableReference(), ref length, out _))
188188
{
189-
int error = Marshal.GetLastWin32Error();
189+
int error = Marshal.GetLastPInvokeError();
190190

191191
// The docs don't call this out clearly, but experimenting shows that the error returned is the following.
192192
if (error != Interop.Errors.ERROR_INSUFFICIENT_BUFFER)

src/libraries/System.Private.CoreLib/src/System/Environment.Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private static string CurrentDirectoryCore
4141
{
4242
if (!Interop.Kernel32.SetCurrentDirectory(value))
4343
{
44-
int errorCode = Marshal.GetLastWin32Error();
44+
int errorCode = Marshal.GetLastPInvokeError();
4545
throw Win32Marshal.GetExceptionForWin32Error(
4646
errorCode == Interop.Errors.ERROR_FILE_NOT_FOUND ? Interop.Errors.ERROR_PATH_NOT_FOUND : errorCode,
4747
value);

src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Nls.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private static unsafe int FindStringOrdinal(
6868
Debug.Assert(ret >= -1 && ret <= source.Length);
6969

7070
// SetLastError is only performed under debug builds.
71-
Debug.Assert(ret >= 0 || Marshal.GetLastWin32Error() == Interop.Errors.ERROR_SUCCESS);
71+
Debug.Assert(ret >= 0 || Marshal.GetLastPInvokeError() == Interop.Errors.ERROR_SUCCESS);
7272

7373
return ret;
7474
}
@@ -300,7 +300,7 @@ private unsafe int FindString(
300300
Debug.Assert(result >= -1 && result <= lpStringSource.Length);
301301

302302
// SetLastError is only performed under debug builds.
303-
Debug.Assert(result >= 0 || Marshal.GetLastWin32Error() == Interop.Errors.ERROR_SUCCESS);
303+
Debug.Assert(result >= 0 || Marshal.GetLastPInvokeError() == Interop.Errors.ERROR_SUCCESS);
304304

305305
return result;
306306
}
@@ -506,7 +506,7 @@ private unsafe int NlsGetSortKey(ReadOnlySpan<char> source, Span<byte> destinati
506506
// to allocate a temporary buffer large enough to hold intermediate state,
507507
// or the destination buffer being too small.
508508

509-
if (Marshal.GetLastWin32Error() == Interop.Errors.ERROR_INSUFFICIENT_BUFFER)
509+
if (Marshal.GetLastPInvokeError() == Interop.Errors.ERROR_INSUFFICIENT_BUFFER)
510510
{
511511
ThrowHelper.ThrowArgumentException_DestinationTooShort();
512512
}

src/libraries/System.Private.CoreLib/src/System/Globalization/IdnMapping.Nls.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private uint NlsFlags
117117
[DoesNotReturn]
118118
private static void ThrowForZeroLength(bool unicode)
119119
{
120-
int lastError = Marshal.GetLastWin32Error();
120+
int lastError = Marshal.GetLastPInvokeError();
121121

122122
throw new ArgumentException(
123123
lastError == Interop.Errors.ERROR_INVALID_NAME ? SR.Argument_IdnIllegalName :

src/libraries/System.Private.CoreLib/src/System/Globalization/Normalization.Nls.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ private static unsafe bool NlsIsNormalized(string strInput, NormalizationForm no
2525
result = Interop.Normaliz.IsNormalizedString(normalizationForm, pInput, strInput.Length);
2626
}
2727

28-
int lastError = Marshal.GetLastWin32Error();
28+
int lastError = Marshal.GetLastPInvokeError();
2929
switch (lastError)
3030
{
3131
case Interop.Errors.ERROR_SUCCESS:
@@ -84,7 +84,7 @@ private static unsafe string NlsNormalize(string strInput, NormalizationForm nor
8484
{
8585
realLength = Interop.Normaliz.NormalizeString(normalizationForm, pInput, strInput.Length, pDest, buffer.Length);
8686
}
87-
int lastError = Marshal.GetLastWin32Error();
87+
int lastError = Marshal.GetLastPInvokeError();
8888

8989
switch (lastError)
9090
{

src/libraries/System.Private.CoreLib/src/System/IO/Strategies/FileStreamHelpers.Windows.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ private static SafeFileHandle ValidateFileHandle(SafeFileHandle fileHandle, stri
176176
// NT5 oddity - when trying to open "C:\" as a Win32FileStream,
177177
// we usually get ERROR_PATH_NOT_FOUND from the OS. We should
178178
// probably be consistent w/ every other directory.
179-
int errorCode = Marshal.GetLastWin32Error();
179+
int errorCode = Marshal.GetLastPInvokeError();
180180

181181
if (errorCode == Interop.Errors.ERROR_PATH_NOT_FOUND && path!.Length == PathInternal.GetRootLength(path))
182182
errorCode = Interop.Errors.ERROR_ACCESS_DENIED;
@@ -229,7 +229,7 @@ internal static long Seek(SafeFileHandle handle, string? path, long offset, Seek
229229

230230
internal static int GetLastWin32ErrorAndDisposeHandleIfInvalid(SafeFileHandle handle)
231231
{
232-
int errorCode = Marshal.GetLastWin32Error();
232+
int errorCode = Marshal.GetLastPInvokeError();
233233

234234
// If ERROR_INVALID_HANDLE is returned, it doesn't suffice to set
235235
// the handle as invalid; the handle must also be closed.
@@ -294,7 +294,7 @@ internal static void ValidateFileTypeForNonExtendedPaths(SafeFileHandle handle,
294294
if (fileType != Interop.Kernel32.FileTypes.FILE_TYPE_DISK)
295295
{
296296
int errorCode = fileType == Interop.Kernel32.FileTypes.FILE_TYPE_UNKNOWN
297-
? Marshal.GetLastWin32Error()
297+
? Marshal.GetLastPInvokeError()
298298
: Interop.Errors.ERROR_SUCCESS;
299299

300300
handle.Dispose();
@@ -333,7 +333,7 @@ internal static unsafe void SetFileLength(SafeFileHandle handle, string? path, l
333333
&eofInfo,
334334
(uint)sizeof(Interop.Kernel32.FILE_END_OF_FILE_INFO)))
335335
{
336-
int errorCode = Marshal.GetLastWin32Error();
336+
int errorCode = Marshal.GetLastPInvokeError();
337337
if (errorCode == Interop.Errors.ERROR_INVALID_PARAMETER)
338338
throw new ArgumentOutOfRangeException(nameof(length), SR.ArgumentOutOfRange_FileLengthTooBig);
339339
throw Win32Marshal.GetExceptionForWin32Error(errorCode, path);

src/libraries/System.Private.CoreLib/src/System/IO/Strategies/Net5CompatFileStreamStrategy.CompletionSource.Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ private static void Cancel(object? state)
200200
if (!completionSource._strategy._fileHandle.IsInvalid &&
201201
!Interop.Kernel32.CancelIoEx(completionSource._strategy._fileHandle, completionSource._overlapped))
202202
{
203-
int errorCode = Marshal.GetLastWin32Error();
203+
int errorCode = Marshal.GetLastPInvokeError();
204204

205205
// ERROR_NOT_FOUND is returned if CancelIoEx cannot find the request to cancel.
206206
// This probably means that the IO operation has completed.

src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,7 @@ public static IntPtr GetFunctionPointerForDelegate<TDelegate>(TDelegate d) where
11731173

11741174
public static int GetHRForLastWin32Error()
11751175
{
1176-
int dwLastError = GetLastWin32Error();
1176+
int dwLastError = GetLastPInvokeError();
11771177
if ((dwLastError & 0x80000000) == 0x80000000)
11781178
{
11791179
return dwLastError;

src/libraries/System.Private.CoreLib/src/System/Security/SecureString.Windows.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private void ProtectMemory()
2626
!_encrypted &&
2727
!Interop.Crypt32.CryptProtectMemory(_buffer, (uint)_buffer.ByteLength, Interop.Crypt32.CRYPTPROTECTMEMORY_SAME_PROCESS))
2828
{
29-
throw new CryptographicException(Marshal.GetLastWin32Error());
29+
throw new CryptographicException(Marshal.GetLastPInvokeError());
3030
}
3131

3232
_encrypted = true;
@@ -41,7 +41,7 @@ private void UnprotectMemory()
4141
_encrypted &&
4242
!Interop.Crypt32.CryptUnprotectMemory(_buffer, (uint)_buffer.ByteLength, Interop.Crypt32.CRYPTPROTECTMEMORY_SAME_PROCESS))
4343
{
44-
throw new CryptographicException(Marshal.GetLastWin32Error());
44+
throw new CryptographicException(Marshal.GetLastPInvokeError());
4545
}
4646

4747
_encrypted = false;

src/libraries/System.Private.CoreLib/src/System/Threading/EventWaitHandle.Windows.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private void CreateEventCore(bool initialState, EventResetMode mode, string? nam
2828

2929
SafeWaitHandle handle = Interop.Kernel32.CreateEventEx(IntPtr.Zero, name, eventFlags, AccessRights);
3030

31-
int errorCode = Marshal.GetLastWin32Error();
31+
int errorCode = Marshal.GetLastPInvokeError();
3232
if (handle.IsInvalid)
3333
{
3434
handle.SetHandleAsInvalid();
@@ -54,7 +54,7 @@ private static OpenExistingResult OpenExistingWorker(string name, out EventWaitH
5454

5555
if (myHandle.IsInvalid)
5656
{
57-
int errorCode = Marshal.GetLastWin32Error();
57+
int errorCode = Marshal.GetLastPInvokeError();
5858

5959
if (errorCode == Interop.Errors.ERROR_FILE_NOT_FOUND || errorCode == Interop.Errors.ERROR_INVALID_NAME)
6060
return OpenExistingResult.NameNotFound;

src/libraries/System.Private.CoreLib/src/System/Threading/LowLevelLifoSemaphore.Windows.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private void Create(int maximumSignalCount)
2626
Interop.Kernel32.CreateIoCompletionPort(new IntPtr(-1), IntPtr.Zero, UIntPtr.Zero, maximumSignalCount);
2727
if (_completionPort == IntPtr.Zero)
2828
{
29-
int error = Marshal.GetLastWin32Error();
29+
int error = Marshal.GetLastPInvokeError();
3030
var exception = new OutOfMemoryException();
3131
exception.HResult = error;
3232
throw exception;
@@ -46,7 +46,7 @@ public bool WaitCore(int timeoutMs)
4646
Debug.Assert(timeoutMs >= -1);
4747

4848
bool success = Interop.Kernel32.GetQueuedCompletionStatus(_completionPort, out int numberOfBytes, out UIntPtr completionKey, out IntPtr pointerToOverlapped, timeoutMs);
49-
Debug.Assert(success || (Marshal.GetLastWin32Error() == WaitHandle.WaitTimeout));
49+
Debug.Assert(success || (Marshal.GetLastPInvokeError() == WaitHandle.WaitTimeout));
5050
return success;
5151
}
5252

@@ -58,7 +58,7 @@ public void ReleaseCore(int count)
5858
{
5959
if (!Interop.Kernel32.PostQueuedCompletionStatus(_completionPort, 1, UIntPtr.Zero, IntPtr.Zero))
6060
{
61-
int lastError = Marshal.GetLastWin32Error();
61+
int lastError = Marshal.GetLastPInvokeError();
6262
var exception = new OutOfMemoryException();
6363
exception.HResult = lastError;
6464
throw exception;

src/libraries/System.Private.CoreLib/src/System/Threading/Mutex.Windows.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ private void CreateMutexCore(bool initiallyOwned, string? name, out bool created
1919
{
2020
uint mutexFlags = initiallyOwned ? Interop.Kernel32.CREATE_MUTEX_INITIAL_OWNER : 0;
2121
SafeWaitHandle mutexHandle = Interop.Kernel32.CreateMutexEx(IntPtr.Zero, name, mutexFlags, AccessRights);
22-
int errorCode = Marshal.GetLastWin32Error();
22+
int errorCode = Marshal.GetLastPInvokeError();
2323

2424
if (mutexHandle.IsInvalid)
2525
{
@@ -60,7 +60,7 @@ private static OpenExistingResult OpenExistingWorker(string name, out Mutex? res
6060

6161
if (myHandle.IsInvalid)
6262
{
63-
int errorCode = Marshal.GetLastWin32Error();
63+
int errorCode = Marshal.GetLastPInvokeError();
6464
#if TARGET_UNIX || TARGET_BROWSER
6565
if (errorCode == Interop.Errors.ERROR_FILENAME_EXCED_RANGE)
6666
{

src/libraries/System.Private.CoreLib/src/System/Threading/Semaphore.Windows.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ private void CreateSemaphoreCore(int initialCount, int maximumCount, string? nam
2929
#endif
3030
SafeWaitHandle myHandle = Interop.Kernel32.CreateSemaphoreEx(IntPtr.Zero, initialCount, maximumCount, name, 0, AccessRights);
3131

32-
int errorCode = Marshal.GetLastWin32Error();
32+
int errorCode = Marshal.GetLastPInvokeError();
3333
if (myHandle.IsInvalid)
3434
{
3535
if (!string.IsNullOrEmpty(name) && errorCode == Interop.Errors.ERROR_INVALID_HANDLE)
@@ -56,7 +56,7 @@ private static OpenExistingResult OpenExistingWorker(string name, out Semaphore?
5656
if (myHandle.IsInvalid)
5757
{
5858
result = null;
59-
int errorCode = Marshal.GetLastWin32Error();
59+
int errorCode = Marshal.GetLastPInvokeError();
6060

6161
if (errorCode == Interop.Errors.ERROR_FILE_NOT_FOUND || errorCode == Interop.Errors.ERROR_INVALID_NAME)
6262
return OpenExistingResult.NameNotFound;

0 commit comments

Comments
 (0)