The previous article explains the basic process of calling a pure C ++ module in the C # code. In this article, we will introduce how C ++ code calls C # code. I construct a simple and intuitive example: trigger the C # UI through the C ++ UI.
First, create a C # project class library project ---- csharpui
Add a form interface for the project and add a C # function -- invokeui () to construct and display the interface.
Namespace csharpui
{
Public class Program
{
Public static void invokeui ()
{
// Class form1 ----- C # UI
Form1 fm = new form1 ();
FM. showdialog ();
}
}
}
Create a managed Dynamic Link Library Project-mgdpro (for detailed steps, refer to the previous article ).
In the mgdpro project, code is used to encapsulate the call to the above C # function, and the packaged class is exported from the DLL. The prerequisite is reference csharpui. dll.
//. H file
# Define dllimpexp _ declspec (dllexport)
Class dllimpexp mgdclass
{
Public:
Static void invokecsharpdlg ();
};
//. Cpp File
Using namespace csharpui;
Void mgdclass: invokecsharpdlg ()
{
Program: invokeui ();
}
Finally, create a dialog Based C ++ project ---- purec ++ Pro (exe project ),
Static link mgdpro. dll in the project. Call the code to trigger the C # interface in the trigger function of the invoke button.
Void cpurecprodlg: onbnclickedbutton1 ()
{
// Todo: add your control notification handler code here
// Call managed C ++ to invoke C # UI
Mgdclass: invokecsharpdlg ();
}
The running interface is as follows:
Article Source: http://www.diybl.com/course/4_webprogram/asp.net/netjs/20071020/78426_2.html