使用 DLLImport 时异常如何工作

本文关键字:工作 何工作 DLLImport 异常 使用 | 更新日期: 2023-09-27 18:31:23

我正在使用DLLImport从C#应用程序调用GhostScript库。

所以我有一些这样的代码,

[DllImport("gsdll32.dll", EntryPoint = "gsapi_init_with_args")]
private static extern int gsapi_init_with_args(IntPtr instance, int argc, IntPtr argv);
try 
{ 
    intReturn = gsapi_init_with_args(intGSInstanceHandle, intElementCount, intptrArgs); 
}
catch (Exception ex)
{
    throw new ApplicationException(ex.Message, ex);
}

我将查看用 C 或 C++ 编写的 GhostScript 源代码,但总的来说,我想知道如果 GhostScript 代码抛出未经处理的异常会发生什么?会被那里的渔获抓住,还是必须看起来像这样,

catch
{
    // blah
}

使用 DLLImport 时异常如何工作

它不会引发异常,您应该查看返回代码。http://pages.cs.wisc.edu/~ghost/doc/AFPL/7.04/API.htm#return_codes

C 编程的标准方法,返回错误的非零代码,有时后跟第二个 API 调用以检索错误的更多详细信息。