Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do Exceptions Work When Using DLLImport

Tags:

c++

c#

exception

I am using DLLImport to call the GhostScript library from a C# application.

So I have some code like this,

[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);
}

I am going to look at the GhostScript source code, which is written in C or C++, but in general I was wondering what would happen if the GhostScript code threw an unhandled exception? Would that be caught by the catch there, or would it have to look like this,

catch
{
    // blah
}
like image 786
peter Avatar asked Apr 18 '12 02:04

peter


1 Answers

It won't throw an exception, you're meant to look at the return codes. http://pages.cs.wisc.edu/~ghost/doc/AFPL/7.04/API.htm#return_codes

Pretty standard method for C programming, return code of non zero for an error, sometimes followed by a 2nd API call to retrieve more details of the error.

like image 156
M Afifi Avatar answered Oct 03 '22 15:10

M Afifi