Unmanaged pointer in managed collection
-
Hello: I am developping a MC++ dll that reuses tons of code that was made in unmanaged C++. What I am trying to do now is to store unmanaged objects in a managed collection. Let's suppose that I have an unmanaged class (from an unmanaged c++ dll) CMyUnmanaged pUnmanaged* = new CMyUnmanaged; And, on the other hand I have a .net collection, a hashlist in this case: System::Collections::Hashlist* hl = new System::Collections::Hashlist; What I would like to achive is something like this: hl->Add(pUnmanaged->Name, pUnmanaged); CMyUnmanaged pUnmanaged2* = hi[pUnmanaged->Name]; But as .net collections can only store System::Objects, that is managed objects, I cannot do it the straight way. :confused: How can I convert my unmanaged pointers in order to store them in the hashlist? Is there a better way to do this? Thank you very much!
-
Hello: I am developping a MC++ dll that reuses tons of code that was made in unmanaged C++. What I am trying to do now is to store unmanaged objects in a managed collection. Let's suppose that I have an unmanaged class (from an unmanaged c++ dll) CMyUnmanaged pUnmanaged* = new CMyUnmanaged; And, on the other hand I have a .net collection, a hashlist in this case: System::Collections::Hashlist* hl = new System::Collections::Hashlist; What I would like to achive is something like this: hl->Add(pUnmanaged->Name, pUnmanaged); CMyUnmanaged pUnmanaged2* = hi[pUnmanaged->Name]; But as .net collections can only store System::Objects, that is managed objects, I cannot do it the straight way. :confused: How can I convert my unmanaged pointers in order to store them in the hashlist? Is there a better way to do this? Thank you very much!
Jose M Castellanos wrote:
And, on the other hand I have a .net collection, a hashlist in this case: System::Collections::Hashlist* hl = new System::Collections::Hashlist; What I would like to achive is something like this: hl->Add(pUnmanaged->Name, pUnmanaged); CMyUnmanaged pUnmanaged2* = hi[pUnmanaged->Name];
Im not sure if you can do this but even if you could you would not be able to use the collection outside of managed c++ as it contains un-managed classes. Why not just use an STL container?
-
Hello: I am developping a MC++ dll that reuses tons of code that was made in unmanaged C++. What I am trying to do now is to store unmanaged objects in a managed collection. Let's suppose that I have an unmanaged class (from an unmanaged c++ dll) CMyUnmanaged pUnmanaged* = new CMyUnmanaged; And, on the other hand I have a .net collection, a hashlist in this case: System::Collections::Hashlist* hl = new System::Collections::Hashlist; What I would like to achive is something like this: hl->Add(pUnmanaged->Name, pUnmanaged); CMyUnmanaged pUnmanaged2* = hi[pUnmanaged->Name]; But as .net collections can only store System::Objects, that is managed objects, I cannot do it the straight way. :confused: How can I convert my unmanaged pointers in order to store them in the hashlist? Is there a better way to do this? Thank you very much!
Actually, you can use a managed container if you use "IntPtr". The following example is using C++/CLI: using namespace System; using namespace System::Collections::Generic; int main(array ^args) { int* ptr = nullptr; List^ list = gcnew List; try { for (int i = 0; i < 10; ++i) { ptr = new int(i); list->Add(IntPtr(ptr)); } for each (IntPtr iptr in list) { ptr = static_cast(iptr.ToPointer()); Console::WriteLine(*ptr); } } catch (Exception^ e) { Console::WriteLine(e->Message); } finally { for each (IntPtr iptr in list) { delete static_cast(iptr.ToPointer()); } } return 0; }