Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task/use get last p invoke error #52003

Merged
merged 2 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -506,7 +506,7 @@ private unsafe int NlsGetSortKey(ReadOnlySpan<char> source, Span<byte> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,7 @@ public static IntPtr GetFunctionPointerForDelegate<TDelegate>(TDelegate d) where

public static int GetHRForLastWin32Error()
{
int dwLastError = GetLastWin32Error();
int dwLastError = GetLastPInvokeError();
if ((dwLastError & 0x80000000) == 0x80000000)
{
return dwLastError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down