none
Working with Managed and Unmanaged Objects RRS feed

  • Question

  • Hello,

    I'm working on an application where Managed objects I create need to have an unmanaged counterpart. This is because the managed side is C#/WPF for GUI/general object management, and the other side has huge amount of raw data, which needs to be processed efficiently (hence the need for C++/unmanaged code)

    So I know I can access an unmanaged DLL using PInvoke / DLLImport, but really this equates to calling a 'Function' on the unmanaged side. Surely that function can create objects etc... on the unmanaged side, but how would I go about creating an unmanaged 'object' when creating a managed one? Should I call the unmanaged 'constructor/destructors' in the managed constructor/destructors as 'functions' (using DLLImport)?

    The reason I ask is because I'm a bit confused about DLLImport works: I can call an unmanaged function of an unmanaged object with DLLImport without even creating the object, as if the function was not part of an object. It sounds a bit odd to me that I can call a function of an unmanaged object without creating it, so somehow it must get created in the process or an object function is acting just like an independent (non-object) function.

    I want to be able to managed those objects and their lifetime, and I'm not sure how that all plays with DLLImport.

    Can someone explain how this works?

    Thanks

    Wednesday, November 10, 2010 12:23 AM

Answers

  • You can use C++ Interop to have a native pointer pointing to an object on the native heap in a managed object that reside on the managed heap. You just need to delete the native object when the managed object is disposed.

    DLL import is functional programming and do not have a concept of class. You can pass a block of memory to a native function regardless whether it is on the managed heap or native heap, though if it is on the managed heap it needs to be pinned. You need to manage the memory lifetime by yourself (e.g. export a deletebuffer function).

     



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked as answer by SamAgain Friday, November 19, 2010 5:14 AM
    Wednesday, November 10, 2010 1:11 AM
  • Calling Native Functions from Managed Code lists different interop methods and pro/con for each.



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked as answer by SamAgain Friday, November 19, 2010 5:14 AM
    Wednesday, November 10, 2010 1:20 AM

All replies

  • You can use C++ Interop to have a native pointer pointing to an object on the native heap in a managed object that reside on the managed heap. You just need to delete the native object when the managed object is disposed.

    DLL import is functional programming and do not have a concept of class. You can pass a block of memory to a native function regardless whether it is on the managed heap or native heap, though if it is on the managed heap it needs to be pinned. You need to manage the memory lifetime by yourself (e.g. export a deletebuffer function).

     



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked as answer by SamAgain Friday, November 19, 2010 5:14 AM
    Wednesday, November 10, 2010 1:11 AM
  • I only heard of DLL Import / Platform Invoke to communicate between managed and unmanaged code. How does C++ Interop work then? Do you have a quick example?

    Thanks for help

    Wednesday, November 10, 2010 1:15 AM
  • Calling Native Functions from Managed Code lists different interop methods and pro/con for each.



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    • Marked as answer by SamAgain Friday, November 19, 2010 5:14 AM
    Wednesday, November 10, 2010 1:20 AM
  • I see... but you're talking about using C++/CLI with native C++, while I'm using WPF/C# for the GUI/system management, so does that mean my only option is DllImport?
    Wednesday, November 10, 2010 1:24 AM
  • actually now you got me thinking...

    I'm following MVVM pattern, so I guess I could use C++/CLI for the ViewModel side of my API, and reference this into a WPF GUI, right? is that possible?, or is there any major issues using C++/CLI assemblies in WPF ?

    Wednesday, November 10, 2010 1:59 AM
  • oops... No Intellisense in C++/CLR? How would anyone work with this!

     

    Any news of when it is coming?

    Wednesday, November 10, 2010 2:54 AM