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 / November 2006

Tip: Looking for answers? Try searching our database.

Callbacks in C++, with managed delegates...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
TT (Tom Tempelaere) - 16 Nov 2006 14:56 GMT
Hi all,

I have a problem with the project I am working on. Someone is coding a
driver in plain (unmanaged) C++, and it is required to subscribe a callback
for certain notifications. This driver is supposed to be used from a managed
environment though, so what I want to do is subscribe a managed delegate to a
certain callback in C++.

Suppose this callback in unmanaged C++ is defined like:

 typedef void (__stdcall* CB)( void );

And I would get the unmanaged C++ library with an interface to register such
a callback:

 class C {
   public:
     void Register_CB( CB cb ) { this->_cb = cb; }
   private:
     CB _cb;
     void Invoke_CB( ) { (*_cb)( ); }
 };

How would I go about registering a delegate to it? Any pointers or links
that could help me with this specific issue?

Kind regards,
--
Tom Tempelaere.
TT (Tom Tempelaere) - 16 Nov 2006 16:11 GMT
Hmm I found a way using GCHandle to fix the delegates, and using Marshal
class to do the interop...

My only concern is that if I would subscribe a lot of handlers that way, is
memory fragmentation due to all the pinning...

Regards,
Signature

Tom Tempelaere.

> Hi all,
>
[quoted text clipped - 25 lines]
> --
> Tom Tempelaere.
Marcus Heege - 21 Nov 2006 17:24 GMT
From your descriptions, I assume you are using
Marshal.GetFunctionPointerForDelegate.

In this case there is no need to fix delegates. Neither with GCHandle nor
via pinned pointers. The runtime generates a unmanaged -> managed thunk in
your case. This thunk is passed to the native function, not the delegate.
The only thing you must ensure that you hold a reference to the delegate as
long as the thunk is needed, because the lifetime of the target delegate
controls the lifetime of the thunk.

Marcus Heege

> Hmm I found a way using GCHandle to fix the delegates, and using Marshal
> class to do the interop...
[quoted text clipped - 38 lines]
>> --
>> Tom Tempelaere.

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.