Question about managed and unmanaged codes in VC++.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • querry
    New Member
    • Feb 2007
    • 45

    Question about managed and unmanaged codes in VC++.NET

    Hi all,

    I am developing a Windows Form Application in VC++. I am using using Visual Studio 2005. My project requires me to use the Boost Libraries [for more info visit www.boost.org] These boost libraries are unmanaged code, where as my application itself is managed code. I have a seperae set of unmanaged classes doing the bulk of the work. Now I want to use these classes form the managed class of my application.

    Let me give a example... Say I have a form and a button on it. I want to call a perticular function form the unmanaged code when the button is clicked. How do I do that? Or say when the button is clicked and if at all, I am able to call the function which is returning, say a array. How do I get the values of the elements in the array populaed into a ListBox?

    Please help me...
    I am new to VC++...
    Thanks
  • misza
    New Member
    • Feb 2008
    • 13

    #2
    Hi,

    I also started using managed c++ not so long ago, so maybe someone more experienced could help you better;

    but for a good start;) :

    - you can use unmanaged objects in your managed class, even as members, e.g.
    Code:
    public ref class Example
    {
      public:
         UnmanagedClass* m_member; // here it's important to use a pointer, otherwise the code won't compile
    };
    - as for using return values from unmanaged, check marshaling:
    http://msdn2.microsoft .com/en-us/library/system.runtime. interopservices .marshal.aspx

    ...and examples you asked:

    Originally posted by querry
    Say I have a form and a button on it. I want to call a perticular function form the unmanaged code when the button is clicked. How do I do that?
    Code:
    private: System::Void button1_Click(/* args */)
    {
       UnmanClass unman();
       unman.someFunction();
       (...)
    }
    Originally posted by querry
    Or say when the button is clicked and if at all, I am able to call the function which is returning, say a array. How do I get the values of the elements in the array populaed into a ListBox?
    Code:
    (...)
    int[] returned = unman.getArray(length);
    lstBox->Items->Clear(); // assuming you have a lstBox member of type ListBox^
    for(int i=0; i< length; i++)
    {
      lstBox->Items->Add(returned[i]); // for simple types (like int) - you don't have to convert them from unmanaged to managed, for other check the marshaling
    }
    (...)
    in the second example I didn't use std::string or char* (which is more logical for a listbox;) - to see how to convert string to System::String and back, check here:
    http://www.thescripts. com/forum/thread281603.ht ml
    or here:
    http://support.microso ft.com/kb/311259

    and last but not least, in case of c++/cli syntax issues, check here, quite a good tutorial:
    http://www.functionx.c om/cppcli/index.htm

    I hope I was at least a little helpful;)

    M.

    Comment

    • querry
      New Member
      • Feb 2007
      • 45

      #3
      Thanks misza,

      I will try that out. Thanks a lot.

      Comment

      Working...