Use managed C ++ to bond C # And C ++ code (2)

Source: Internet
Author: User

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.

 
 
  1. namespace CSharpUI  
  2. {  
  3. public class Program  
  4. {  
  5. public static void InvokeUi()  
  6. {  
  7. //class Form1-----C# UI  
  8. Form1 fm = new Form1();  
  9. fm.ShowDialog();  
  10. }  
  11. }  
  12. }  

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.

 
 
  1. //.h file  
  2. #define DLLIMPEXP __declspec(dllexport)  
  3. class DLLIMPEXP MgdClass  
  4. {  
  5. public:  
  6. static void InvokeCsharpDlg();  
  7. };  
  8.  
  9. //.cpp file  
  10. using namespace CSharpUI;  
  11. void MgdClass::InvokeCsharpDlg()  
  12. {  
  13. 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.

 
 
  1. void CPureCProDlg::OnBnClickedButton1()  
  2. {  
  3. // TODO: Add your control notification handler code here  
  4. //call managed c++ to invoke c# UI  
  5. MgdClass::InvokeCsharpDlg();  
  6. }  

The running interface is as follows:

Edit recommendations]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.