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.
-
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:
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?
-
private: System::Void button1_Click(/* args */)
-
{
-
UnmanClass unman();
-
unman.someFunction();
-
(...)
-
}
-
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?
-
(...)
-
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.html
or here:
http://support.microsoft.com/kb/311259
and last but not least, in case of c++/cli syntax issues, check here, quite a good tutorial:
http://www.functionx.com/cppcli/index.htm
I hope I was at least a little helpful;)
M.