Jump to content
  • Advertisement
Sign in to follow this  
SOS

How to correctly move pointer between managed and umanaged mode in mixed-mode C++?

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm doing some interop and passing a structure to managed code via an IntPtr. I convert the structure to the IntPtr as follows: System::IntPtr func(Event &e) { return __nogc new System::IntPtr(&e); } I'm not too confident about my mixed-mode C++ skills, but I think that's right... right? However, the data I get back from PtrToStructure (with the IntPtr returned from func() as the first parameter, and the type of a managed replica of the Event structure as the second parameter) gives me complete gibberish. It's not a member alignment problem either, because the data is supposed to be mostly nulls but I get numbers in the millions (Event contains mostly pointers to other structures). What am I doing wrong?

Share this post


Link to post
Share on other sites
Advertisement
Ok, I solved it.

In case anyone wants to know, the problem was caused by IntPtr.
Basically, IntPtr(void*) does NOT wrap a pointer into a managed pointer. Instead, it creates a managed pointer TO an unmanaged pointer. Very wrong.

The solution was to simply use static_cast to convert the umanaged pointer into a managed one.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!