Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6216653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:17:13+00:00 2026-05-24T07:17:13+00:00

This is a similar question to this SO post , which I have been

  • 0

This is a similar question to this SO post, which I have been unable to use to solve my problem. I have included some code here, which will hopefully help someone to bring home the message that the other posting was getting at.

I want to write a CLI/C++ method that can take a void pointer as a parameter and return the managed object (whose type I know) that it points to. I have a managed struct:

public ref struct ManagedStruct { double a; double b;};

The method I am trying to write, which takes a void pointer to the managed struct as a parameter and returns the struct.

ManagedStruct^ VoidPointerToObject(void* data)
{   
    Object^ result = Marshal::PtrToStructure(IntPtr(data), Object::typeid);
    return (ManagedStruct^)result;
}

The method is called here:

int main(array<System::String ^> ^args)
{   
    // The instance of the  managed type is created:
    ManagedStruct^ myData = gcnew ManagedStruct();
    myData->a = 1;  myData->b = 2;      

    // Suppose there was a void pointer that pointed to this managed struct
    void* voidPtr = &myData;

    //A method to return the original struct from the void pointer
    Object^ result = VoidPointerToObject(voidPtr);  
    return 0;
}

It crashes in the VoidPointerToObject method on calling PtrToStructure , with the error: The specified structure must be blittable or have layout information

I know this is an odd thing to do, but it is a situation I have encountered a few times, especially when unmanaged code makes a callback to managed code and passes a void* as a parameter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:17:13+00:00Added an answer on May 24, 2026 at 7:17 am

    (original explanation below)

    If you need to pass a managed handle as a void* through native code, you should use

    void* voidPtr = GCHandle::ToIntPtr(GCHandle::Alloc(o)).ToPointer();
    
    // ...
    
    GCHandle h = GCHandle::FromIntPtr(IntPtr(voidPtr));
    Object^ result = h.Target;
    h.Free();
    

    (or use the C++/CLI helper class gcroot)


    Marshal::PtrToStructure works on value types.

    In C++/CLI, that means value class or value struct. You are using ref struct, which is a reference type despite use of the keyword struct.

    A related problem:

    void* voidPtr = &myData;
    

    doesn’t point to the object, it points to the handle.

    In order to create a native pointer to data on the managed heap, you need to use pinning. For this reason, conversion between void* and Object^ isn’t as useful as first glance suggests.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This should be a common problem and possibly similar to some question here but
This is similar to this question, except the code is already written. I have
This is a similar question to this one here . Given a list of
This is similar to a question that has already been asked. However, I am
I have an error very similar to the one addressed in this question .
My question is similar to this following post Render a view of another controller
I have seen this post here (http://stackoverflow.com/questions/151777/how-do-i-save-an-android-applications-state). I am having a similar problem I
I've asked a similar question before, but I've done some more research and this
I know many similar questions have been asked, which is why I hesitated to
This is almost similar question to this one: - Dealing with timezones in PHP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.