Sign in to follow this  

[.net] unmanaged object in .NET Arraylist (unsolved)

This topic is 4480 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

The parameter for .Add is __gc object, but many C++ libraries are written without managed objects. Is there a common work-around for putting unmanaged objects in a .Net ArrayList? [Edited by - Kelly G on September 1, 2005 8:01:47 PM]

Share this post


Link to post
Share on other sites
If I understand your question properly you need to box unmanged or primitive types. For example the following is an error in Managed C++:

System::Collections::ArrayList* list = new System::Collections::ArrayList();
list->Add( 1 );

But if you box the integer it will work:

System::Collections::ArrayList* list = new System::Collections::ArrayList();
list->Add( __box( 1 ) );

In C# it's box() without the leading double underbars.

Share this post


Link to post
Share on other sites
My apologies, I just remembered that boxing only works for value types not unmanaged types. Sorry for the misleading answer.

Too early in the morning...

Share this post


Link to post
Share on other sites
If your unmanaged object is a pointer, you can just turn it into an IntPtr.

static_cast<IntPtr>(whatever); (note that you have to use static_cast. using IntPtr's constructor has created some weird new pointer-to-a-pointers for me when I used it...)

Share this post


Link to post
Share on other sites
You cannot store unmanaged objects in managed containers. You also cannot do the reverse. However, you can store pointers to unmanaged objects in managed containers (using IntPtr). You can also use marshalling to convert value types to their equivelent .Net value types.

I should note that there is a possibility that future releases of the .Net platform will allow for cross heap allocations (ie Managed objects on the unmanaged heap, and visa versa).

Share this post


Link to post
Share on other sites

This topic is 4480 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this