c++调用 c#的dll

以vs2012为例 c++

1 用c#输出dll c#

   (1)打开vs2012 spa

   (2)文件->新建->项目->c#->类库 code

   (3)输入代码 orm

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace ClassLibrary1
{
    public class Class1
    {
        public void ShowMessage()
        {
            Console.WriteLine("成功调用了dll");
            Console.ReadLine();
        }

        public void ShowMessageBox()
        {
            MessageBox.Show("调用了Dll", "调用",
            MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        public int ShowArr(double[] a,int n)
        {
            for (int i = 0; i <= n; i++)
            {
                Console.WriteLine("a[{0}]={1}\n", i, a[i]);
            }
            return 0;
        }
    }
}


   (4)生成(B)->生成 ...(U) it

   (5)产生了一个dll文件 io

   (6)新建一个c++项目,项目属性中 将 公共语言运行支持 选为 公共运行时支持(/clr) class

   (7)VC++目录中 将dll文件所在目录 添加到 可执行文件目录 包含目录 引用目录 库目录
cli

   (8)在VC++中写入代码 引用

// ConsoleApplicationdll.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#using "ClassLibrary1.dll"
using namespace ClassLibrary1;

int _tmain(int argc, _TCHAR* argv[])
{
	Class1 ^c=gcnew Class1();
	cli::array<double,1>^a=gcnew cli::array<double>(5);
	for(int i=0;i<5;i++){
		a[i]=i+0.1;
	}
	int n=4;
	//c->ShowMessage();
	c->ShowMessageBox();
	c->ShowArr(a,n);
	return 0;
}
相关文章
相关标签/搜索