Hi All,
Just thought I would post my solution just incase someone is interested.
It seems to work. When I put a break point in iaxc_callback it works and I
can look at the properties of iaxc_event.
public delegate int iaxc_event_callback_t(iaxc_event e);
public void iaxc_set_event_callback(iaxc_event_callback_t iaxc_callback)
{
NativeMethods.iaxc_set_event_callback(iaxc_callback);
}
public int iaxc_callback(iaxc_event e)
{
return 1;
}
Regards,
> Hi All,
>
[quoted text clipped - 11 lines]
> Regards,
> Martin Venter
David Brazier - 30 Mar 2006 14:52 GMT
Hi
I've also been trying to use iaxclient from C# - see some discussion on
iaxclient-devel. A couple of points:
1. You don't show the delegate being created:
protected iaxc_event_callback_t CallbackDelegate;
...
// start up IAX client
iaxc_initialize(0,1); // or whatever
CallbackDelegate = new iaxc_event_callback_t(iaxc_callback);
iaxc_set_event_callback(CallbackDelegate);
...
It is important that the delegate is a member variable - if it was just
local to the code, it would get garbage collected and callbacks would fail.
2. I found that there was a problem with the calling convention of the
callback function: .NET defaults to stdcall, but it is called as a cdecl
function. So although it gets called once, the program then crashes. In
.NET 2.0 you can use:
[UnmanagedFunctionPointer(CallingConvention.CDecl)]
public delegate int iaxc_event_callback_t(iaxc_event e);
but in .NET 1.1 you will need either a nasty hack:
http://www.dotnet247.com/247reference/msgs/17/87210.aspx
or add an extra C function to iaxclient.dll which has a callback declared as
stdcall.
David
> public delegate int iaxc_event_callback_t(iaxc_event e);
>
[quoted text clipped - 7 lines]
> return 1;
> }
David Brazier - 30 Mar 2006 15:01 GMT
Hi again
I meant to ask if anyone can advise whether it is sufficient that a delegate
used as a callback from unmanaged code is a member variable, to keep it
alive. Isn't there a possibility that the .NET memory manager might move the
delegate and make the function pointer invalid? Or perhaps the interop
marshalling takes care of that?
David
> It is important that the delegate is a member variable - if it was just
> local to the code, it would get garbage collected and callbacks would fail.