c++调用 c#的dll

来源:互联网 发布:e盾网络验证容易破解吗 编辑:程序博客网 时间:2024/05/29 18:28

http://my.oschina.net/u/929434/blog/98649

以vs2012为例

1 用c#输出dll

   (1)打开vs2012

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

   (3)输入代码

view source
print?
01using System;
02using System.Collections.Generic;
03using System.Linq;
04using System.Text;
05using System.Threading.Tasks;
06using System.Windows.Forms;
07  
08  
09namespace ClassLibrary1
10{
11    publicclass Class1
12    {
13        publicvoid ShowMessage()
14        {
15            Console.WriteLine("成功调用了dll");
16            Console.ReadLine();
17        }
18  
19        publicvoid ShowMessageBox()
20        {
21            MessageBox.Show("调用了Dll","调用",
22            MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
23        }
24  
25        publicint ShowArr(double[] a,intn)
26        {
27            for(int i = 0; i <= n; i++)
28            {
29                Console.WriteLine("a[{0}]={1}\n", i, a[i]);
30            }
31            return0;
32        }
33    }
34}


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

   (5)产生了一个dll文件

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

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

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

view source
print?
01// ConsoleApplicationdll.cpp : 定义控制台应用程序的入口点。
02//
03  
04#include "stdafx.h"
05#using "ClassLibrary1.dll"
06using namespace ClassLibrary1;
07  
08int _tmain(int argc, _TCHAR* argv[])
09{
10    Class1 ^c=gcnew Class1();
11    cli::array<double,1>^a=gcnew cli::array<double>(5);
12    for(inti=0;i<5;i++){
13        a[i]=i+0.1;
14    }
15    intn=4;
16    //c->ShowMessage();
17    c->ShowMessageBox();
18    c->ShowArr(a,n);
19    return0;
20}

0 0
原创粉丝点击