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 / Languages / Managed C++ / October 2007

Tip: Looking for answers? Try searching our database.

pin_ptr of delegate that spans scope of function

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
john conwell - 19 Oct 2007 19:27 GMT
I have a C++/CLI library and I'm calling a function, RegisterTraceGuids()
that takes a function callback as a parm. I need the callback function to be
a managed delegate.  In order to do this I create a pin_ptr of the delegate
and then pass it to RegisterTraceGuids.

The problem is that pin_ptr's can only be defined within the scope of a
function, and cant be class members.  So once the function that calls
RegisterTraceGuids() is over my pinned delegate gets unpinned.  But my
callback could get called at anytime, long after the function goes out of
scope.  

So how can I pin a delegate instance for a period longer than the scope of
one function?
Ben Voigt [C++ MVP] - 19 Oct 2007 21:03 GMT
>I have a C++/CLI library and I'm calling a function, RegisterTraceGuids()
> that takes a function callback as a parm. I need the callback function to
[quoted text clipped - 11 lines]
> So how can I pin a delegate instance for a period longer than the scope of
> one function?

GCHandle is how you pin _data_ past function exit.

In your case, you need to be using Marshal.GetFunctionPointerForDelegate
instead.
john conwell - 21 Oct 2007 01:31 GMT
Hey Ben,
Thanks for the response.  I am using the
Marshal.GetFunctionPointerForDelegate, but had read that it returns just a
regular pointer, not a pinned pointer.  Do I still need to use GCHandle?  Or
will GetFunctionPointerForDelegate pinn it for me?
Thanks!

Turbo

> GCHandle is how you pin _data_ past function exit.
>
> In your case, you need to be using Marshal.GetFunctionPointerForDelegate
> instead.
Ben Voigt [C++ MVP] - 22 Oct 2007 14:14 GMT
> Hey Ben,
> Thanks for the response.  I am using the
> Marshal.GetFunctionPointerForDelegate, but had read that it returns just a
> regular pointer, not a pinned pointer.  Do I still need to use GCHandle?
> Or
> will GetFunctionPointerForDelegate pinn it for me?

The docs explain that you still need to keep the delegate alive with a
managed reference... however as nearly as I can tell the native function
pointer doesn't need to be pinned in any way.

Hope this helps.

> Thanks!
>
[quoted text clipped - 4 lines]
>> In your case, you need to be using Marshal.GetFunctionPointerForDelegate
>> instead.

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.