c# - 使用 DLLImport 时异常如何工作

标签 c# c++ exception

我正在使用 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
}

最佳答案

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

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

关于c# - 使用 DLLImport 时异常如何工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10201848/

相关文章:

c# - URL 查询字符串 - 查找、替换、添加、更新值?

c++ - Microsoft Visual C++ 6.0 破坏项目

c++ - 为什么复制省略不适用于 std::move?

c++ - 尝试使用 DirectXTK 创建 GamePad 时出现错误 LNK2019

c# - 使用空分配修复 'Use of unassigned local variable'。为什么?

c# - 如何在 App.xaml 文件的 Application.Resources 中组合 LocalizedStrings 和 ResourceDictionary 标签

c# - 创建 System.Windows.Controls.Image 会引发异常 - 如何使用调度程序实例化它?

c# - 在 asp :panel in jquery 中找到控件

angularjs - 如何在 AngularJS 中获得更多堆栈跟踪

c++ - 为什么在从 win32 计时器回调中抛出时不调用我的析构函数?