我正在测试一个 C# .NET 4.0 应用程序,它通过 PInvoke 与 C++ 非托管 DLL 交互,我想捕获 dll 引发的任何异常。我在 try/catch 子句中包装了 dll 函数来处理本机异常,但是当它被触发时,它被忽略了。试过:
try { } catch {}
try {} catch (Exception)
try {} catch (SEHException)
try {} catch (Win32Exception)
无济于事
唯一可行的选项是将DllImport SetLastError属性设置为 true 并在调用函数检查后使用:
if (Marshal.GetLastWin32Error() !=0)
这是一个令人满意的解决方案,但我只是想知道为什么其他选项没有任何效果,以及想知道本机异常是由非托管 dll 还是由 Windows API 本身触发的,因为例如异常是:
System.ComponentModel.Win32Exception (0x80004005): There is not enough space on the disk
这是来自 Windows API 本身的通知吗?