Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / March 2006

Tip: Looking for answers? Try searching our database.

Callback

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Martin Venter - 27 Jan 2006 06:13 GMT
Hi All,

I need some help setting up a callback between C and C#.

Here is the C code:

typedef int (*iaxc_event_callback_t)(iaxc_event e);
EXPORT void iaxc_set_event_callback(iaxc_event_callback_t func);

iaxc_event is a struct.

Any help will be appreciated.

Regards,
Martin Venter
Martin Venter - 01 Feb 2006 10:12 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.