NativeC++通过CLI调用C#的Form


一 调用方法

      Native C++的project调用C#的DLL,一般有3中方法:
      1)通过COM封装
      2)通过CLI/C++的Wrapper
      3)  在VS中可以直接修改NativeC++的project或是部分文件为使用CLR来调用C#的DLL

二 实例
1)C#的一个MyForm类,有public函数ShowMyForm()
using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Windows.Forms;

namespace  CsharpDLL
{
    
public class MyForm : Form
    
{
        
private Button button1;

        
public void ShowMyForm()
        
{
            
this.ShowDialog();
        }

        
public MyForm()
        
{
            InitializeComponent();
            
this.Text = "MyForm";
            
this.StartPosition = FormStartPosition.CenterScreen;
        }

        
private void InitializeComponent()
        
{
            
this.button1 = new System.Windows.Forms.Button();
            
this.SuspendLayout();
            
// 
            
// button1
            
// 
            this.button1.Location = new System.Drawing.Point(11074);
            
this.button1.Name = "button1";
            
this.button1.Size = new System.Drawing.Size(7523);
            
this.button1.TabIndex = 0;
            
this.button1.Text = "TestMessageBox";
            
this.button1.UseVisualStyleBackColor = true;
            
this.button1.Click += new System.EventHandler(this.button1_Click);
            
// 
            
// MyForm
            
// 
            this.ClientSize = new System.Drawing.Size(292266);
            
this.Controls.Add(this.button1);
            
this.Name = "MyForm";
            
this.ResumeLayout(false);

        }


        
private void button1_Click(object sender, EventArgs e)
        
{
            MessageBox.Show(
"Hello, i am a Csharp Form!");
        }

    }

}


2)C#的exe调用
using  System;
using  System.Collections.Generic;
using  System.Text;

namespace  CsharpTest
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            System.Console.WriteLine(
"Csharp main funtion start:");
            CsharpDLL.MyForm myForm 
= new CsharpDLL.MyForm();
            myForm.ShowMyForm();
            System.Console.WriteLine(
"Csharp main function end!");
        }

    }

}


3)CLI/C++的warpper,DLLexport函数CallCsharpForm(),此函数中调用C#的MyForm
CPPCLIPROXYDLLFORCSHARPDLL_API  void  CallCsharpForm();

#
using   " ../debug/CsharpDll.dll "

CPPCLIPROXYDLLFORCSHARPDLL_API 
void  CallCsharpForm()
{
    CsharpDLL::MyForm
^ myForm = gcnew CsharpDLL::MyForm();
    myForm
->ShowMyForm();
}

4)NativeC++的exe调用CLI的Wrapper来间接的调用MyForm
#include  " stdafx.h "
#include 
< iostream >
#pragma comment(lib, 
" ../debug/CppCLIProxyDLLForCsharpDLL.lib " )

void  CallCsharpForm();

int  _tmain( int  argc, _TCHAR *  argv[])
{
    std::cout
<<" Cpp Main function start:"<<std::endl;
    CallCsharpForm();
    std::cout
<<" Cpp Main function end!"<<std::endl;
    
return 0;
}

三 总结
要调试最好修改编译选项为Mixed。

代码下载: http://www.cppblog.com/Files/mzty/CppCallCsharpByCLI.rar
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值