In the previous articleC #Pure calls in the codeC ++The basic process of the module. 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-for detailed steps of MgdPro, 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 ++ Proexe project), and statically link MgdPro to the project. dll. 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:
Edit recommendations]