C# 如何获得最后一个错误(WSAGetLastError)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/765491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get last error (WSAGetLastError)?
提问by Ivan Prodanov
How do I call WSAGetLastError()
from WinAPI so I get the valid text error?
如何WSAGetLastError()
从 WinAPI调用,以便收到有效文本错误?
采纳答案by abatishchev
[DllImport("ws2_32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern Int32 WSAGetLastError();
Also, on pinvoke.netit's said:
另外,在pinvoke.net上说:
You should never PInvoke to GetLastError. Call Marshal.GetLastWin32Error instead!
你永远不应该 PInvoke 到 GetLastError。改为调用 Marshal.GetLastWin32Error !
回答by John Saunders
It doesn't make very much sense to call that function from managed code. It makes sense in unmanaged code because you know the exact last Win32 function that was called, so you know what function must have set the last error. In managed code, you don't know what functions have been called.
从托管代码中调用该函数没有多大意义。这在非托管代码中很有意义,因为您知道最后调用的确切 Win32 函数,因此您知道哪个函数必须设置最后一个错误。在托管代码中,您不知道调用了哪些函数。
You could probably use P/Invoke to call the function; it just wouldn't do you any good. What are you trying to accomplish?
您可能可以使用 P/Invoke 来调用该函数;这对你没有任何好处。你想达到什么目的?
回答by Roger Lipscombe
WSAGetLastError
is just a wrapper for the Win32 GetLastError
function.
WSAGetLastError
只是 Win32GetLastError
函数的包装器。
If you're doing things with P/Invoke, you can use the SetLastError
parameter to the DllImport
attribute. It tells .NET that the imported function will call SetLastError()
, and that the value should be collected.
如果您使用 P/Invoke 进行操作,则可以将SetLastError
参数用于DllImport
属性。它告诉 .NET 导入的函数将调用SetLastError()
,并且应该收集该值。
If the imported function fails, you can get at the last error with Marshal.GetLastWin32Error()
. Alternatively, you can just throw new Win32Exception()
, which uses this value automatically.
如果导入的函数失败,您可以使用Marshal.GetLastWin32Error()
. 或者,您可以只throw new Win32Exception()
使用 ,它会自动使用此值。
If you're not doing things with P/Invoke, you're out of luck: there's no guarantee that the last error value will be preserved long enough to make it back through multiple layers of .NET code. In fact, I'll link to Adam Nathan: never define a PInvoke signature for GetLastError.
如果您不使用 P/Invoke 进行操作,那么您就不走运了:无法保证最后一个错误值将保留足够长的时间以使其通过多层 .NET 代码返回。事实上,我将链接到 Adam Nathan:永远不要为 GetLastError 定义 PInvoke 签名。
回答by David Fort
This is how I saw on the web to put GetLastError into the C# exception mechanismand how to get it back out again...
这就是我在网上看到的将 GetLastError 放入 C# 异常机制以及如何将其重新取出的方式...
try
{
// some p/invoke call that is going to fail with a windows error ...
mHndActivatedDevice = MyNameSpace.Interop.Device.Device.ActivateDevice(
"Drivers\BuiltIn\SomeDriverName", IntPtr.Zero, 0, IntPtr.Zero);
}
catch(System.ComponentModel.Win32Exception exc) // as suggested by John Saunders
{
// you can get the last error like this:
int lastError = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
Console.WriteLine("error:" + lastError.ToString());
// but it is also inside the exception, you can get it like this
Console.WriteLine(exc.NativeErrorCode.ToString());
Console.WriteLine(exc.ToString());
}
where ActivateDevice is defined thus:
其中 ActivateDevice 是这样定义的: