您的位置:首页 > 编程语言 > C语言/C++

C++、C#互调用之C++ 调用C# dll (转载)

2014-07-10 16:16 375 查看
1、c# 创建dll

建立C#编写的DLL程序AddDll,项目类型为:类库

程序代码:

using System;
using System.Collections.Generic;
using System.Text;

namespace AddDll
{
public class Add
{
public int iadd(int a, int b)
{
int c = a + b;
return c;
}
}
}

2、C++编写调用程序

建立C++的Win32控制台应用程序UseDll,项目类型为:Win32控制台应用程序

配置:右键点击解决方案资源管理器中的UseDll,选择“属性”,将公共语言运行库支持设置为“公共语言运行库支持(/clr)”

程序代码:

#include "stdio.h"

#using "..\debug\AddDll.dll"
using namespace AddDll;

int main()

{
int result;
Add ^add = gcnew Add();   //生成托管类型

//gcnew creates an instance of a managed type (reference or value type) on the garbage

//collected heap. The result of the evaluation of a gcnew expression is a handle (^) to

//the type being created.

result = add->iadd(10,90);
printf("%d",result);
scanf("%s");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: