C# 使用DLLImport时异常是如何工作的
我正在使用DLLImport从C#应用程序调用GhostScript库 我有一些这样的代码C# 使用DLLImport时异常是如何工作的,c#,c++,exception,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(intGSI
[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);
}
catch
{
// blah
}
<>我将查看GST脚本源代码,它是用C或C++编写的,但一般我想知道如果GHESTScript代码抛出了一个未处理的异常会发生什么?那会不会被抓到,还是一定要像这样
[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);
}
catch
{
// blah
}
它不会抛出异常,您应该查看返回代码。
C编程的标准方法,返回错误的非零代码,有时会在第二次API调用后检索错误的更多详细信息。可能的dup-这是.NET 4.0是的。不过,我想知道,在这种情况下,这个问题可能有点毫无意义。我看到GhostScript是用C编写的,所以它不应该抛出任何异常,对吗?不管怎样,最好知道什么是C++异常。即使DLL不能,OS也会抛出异常。我使用不同的DLL,并且在过去的catch(异常E)上得到异常的问题,尽管微软说应该工作。