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 / September 2003

Tip: Looking for answers? Try searching our database.

How To: Correctly Pass a pointer to a function from .NET to C++?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
- 09 Sep 2003 18:54 GMT
hey all.

I am passing a pointer to function from managed code
to unmanaged code.
More specifically; from C#.NET to C++.COM.

The UnManaged C++ invokes the C#.NET Managed function.
However, after a couple of minutes it crashes;
(Execution Engine Exception).

My theory:
1. The managed engine, moves the Managed object along with its function
around,
2. The C++ pointer is not updated as to the new location of the function
3. The unmanged code commands the .NET engine to invoke the function
4. However, the function has moved out to LA LA land

My Question:
How to tell .NET not to move my function around!
Can this be done?

Please help me.
I am desperate.

assaf
Mattias Sj?gren - 09 Sep 2003 18:54 GMT
All you have to do is make sure the delegate is alive (i.e. hold a
reference to it to prevent garbage collection) as long as you use the
pointer.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

- 09 Sep 2003 21:08 GMT
hey mattias.

doesn't the .NET move objects around?
don't i need to prevent this motion?

tnx

> All you have to do is make sure the delegate is alive (i.e. hold a
> reference to it to prevent garbage collection) as long as you use the
> pointer.
>
> Mattias
Mattias Sj?gren - 09 Sep 2003 22:11 GMT
>doesn't the .NET move objects around?

Yes it does (sometimes).

>don't i need to prevent this motion?

No, the pointer the unmanaged code gets points to a stub that isn't
moved by the GC, and that will be valid as long as the delegate is.
The stub code knows how to locate the delegate, even if it has been
moved.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.


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.