Interop & Exception

L

Lloyd Dupont

I am writting a language (ObjectiveC) binding.
I am writting a C# layer, as well as a C layer.

When I do an object's method call (in C#), that call the C# messaging
function, which calls the C messaging function and then return.

In the C messaging function I am catching my other language's exception.
I would like, if there is one, to throw a .NET exception from the C code.

I have a peace of code which enable me to do that (pseudo code below) but I
wonder if there is GarbageCollection issue, and if I should use
Marshal.GetFunctionPointerForDelegate() to pass the delegate.


-- C# (2.0) layer --
class Runtime
{
static delegate void ThrowDelegate(string ex);
static ThrowDelegate ThrowIt = ThrowThat;
static void ThrowThat(string msg)
{
throw new ObjectiveCException(msg);
}
[DllImport("myCLayer")]
static void extern Register(ThrowDelegate td); // should it be IntPtr?

public static void Init() // at the begining of the program (in Main) I
call that
{
Register(ThrowIt);
}
}
-- C code --
static void (* netException)(char*) = NULL;
DLL_EXPORT void Register(void (*)(char*) fct)
{
netException = fct;
}
DLL_EXPORT struct call_ret doCall(struct_msg msg)
{
struct call_ret = call(msg);
if(call_ret->exception && netException)
netException(call_ret->exception->sg);
return ret;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top