Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Unmanaged pointer in managed collection

Unmanaged pointer in managed collection

Scheduled Pinned Locked Moved Managed C++/CLI
questioncsharpc++
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jose M Castellanos
    wrote on last edited by
    #1

    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!

    L G 2 Replies Last reply
    0
    • J Jose M Castellanos

      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!

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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?

      1 Reply Last reply
      0
      • J Jose M Castellanos

        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!

        G Offline
        G Offline
        George L Jackson
        wrote on last edited by
        #3

        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; }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups