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
Not able to get PInvoke error message on Linux and MacOs #20660
Comments
I believe this is because you are calling |
Yes, definitely. Thank you. Is there a way to get both GetLastWin32Error and GetHRForLastWin32Error? Or get HR based on error. One for special error handling, second one for exception message. |
@DevMk you could inline this. public static int GetHRForLastWin32Error()
{
int dwLastError = GetLastWin32Error();
if ((dwLastError & 0x80000000) == 0x80000000)
return dwLastError;
else
return (dwLastError & 0x0000FFFF) | unchecked((int)0x80070000);
} |
I think this is resolved -- please reopen if not. |
Hmm... this appears to be a difference between desktop and core. This program: using System;
using System.Runtime.InteropServices;
class Program
{
static void Main()
{
CopyFile("doesntexist", "blah", true);
Console.WriteLine(Marshal.GetLastWin32Error());
Console.WriteLine(Marshal.GetLastWin32Error());
}
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
static extern bool CopyFile(string lpExistingFileName, string lpNewFileName, bool bFailIfExists);
} on desktop prints:
but on core prints:
@yizhang82, @jkotas, is this difference by design? |
Ah, nevermind, I realized my example was flawed. This code: using System;
using System.Runtime.InteropServices;
class Program
{
static void Main()
{
CopyFile("doesntexist", "blah", true);
int a = Marshal.GetLastWin32Error();
int b = Marshal.GetLastWin32Error();
Console.WriteLine(a);
Console.WriteLine(b);
}
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
static extern bool CopyFile(string lpExistingFileName, string lpNewFileName, bool bFailIfExists);
} prints out:
on core. The issue in my first example (and in the original example in this issue) is that Console.WriteLine is using a P/Invoke with SetLastError, and the known, by-design difference in core is that SetLastError zeros out the error code when making a P/Invoke (dotnet/coreclr#614). |
Looks like the solution @danmosemsft proposed is not valid. I am using libc |
@lostmsu the GetLastWin32Error is supposed to return windows error codes, not the Unix ones. So the error code 17 is ERROR_NOT_SAME_DEVICE (if the code is in decimal). See https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-. |
@janvorli This issue though is about getting I think the issue should be reopened. |
Hello,
I see that Marshal.GetLastWin32Error() returns proper errno, however Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()) always throws "The operation completed successfully" instead corresponding error.
Actual result
Expected result:
Exception with "No such file or directory" message
The text was updated successfully, but these errors were encountered: