C++ Programmer's Cookbook

{C++ 基础} {C++ 高级} {C#界面,C++核心算法} {设计模式} {C#基础}

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

posted on 2007-12-25 10:51 梦在天涯 阅读(8184) 评论(2)  编辑 收藏 引用 所属分类: CPlusPlusC#/.NETManage c++ /CLI

评论

# re: NativeC++通过CLI调用C#的Form 2008-09-12 20:43 vvvxxx

用NativeC++->C++/CLI->C#  回复  更多评论   

# re: NativeC++通过CLI调用C#的Form 2009-05-19 14:00 我不清楚是谁

不错...  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

EMail:itech001#126.com

导航

统计

  • 随笔 - 461
  • 文章 - 4
  • 评论 - 746
  • 引用 - 0

常用链接

随笔分类

随笔档案

收藏夹

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

积分与排名

  • 积分 - 1783591
  • 排名 - 5

最新评论

阅读排行榜