博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
编译生成C++导出函数dll,并在C#工程中测试
阅读量:4658 次
发布时间:2019-06-09

本文共 1169 字,大约阅读时间需要 3 分钟。

编译生成过程:

1.建立dll工程

选择新建visual C++的

 

这两个类型工程,都会出现下面界面,在这里设置生成dll:

2.设置项目:

项目属性中设置:

 

3.相关代码:

由于项目的名称是"TestCPPDLL",因此,会自动生成TestCPPDLL.h和TestCPPDLL.cpp两个文件,.h文件是要导出内容的声明文件,为了能清楚的说明问题,我们将TestCPPDLL.h和TestCPPDLL.cpp两个文件中的所有内容都删除,然后在TestCPPDLL.h中添加如下内容:

头文件:

#define TESTCPPDLL_API __declspec(dllexport)EXTERN_C TESTCPPDLL_API int __stdcall Add(int a, int b);

源文件:

TESTCPPDLL_API int __stdcall Add(int a, int b){    return a + b;}

 

测试过程如下:

新建一个C#空工程项目,修改生成的main文件:

1.添加

using System.Runtime.InteropServices;

2.在静态方法中添加:

[DllImport(@"G:/VS13_Project/TestCPPDLL/Release/TestCPPDLL.dll", EntryPoint = "Add")]extern static int Add(int a, int b); 具体如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Runtime.InteropServices; //加上这行namespace testC{    class Program    {        [DllImport(@"G:/VS13_Project/TestCPPDLL/Release/TestCPPDLL.dll", EntryPoint = "Add")]        extern static int Add(int a, int b);        static void Main(string[] args)        {            int c = Add(1, 2);            Console.Write(c);            Console.Read();        }    }}

 参考:

转载于:https://www.cnblogs.com/hpcpp/p/7530692.html

你可能感兴趣的文章
2018.10.30 NOIp模拟赛 T1 改造二叉树
查看>>
九度oj 题目1074:对称平方数
查看>>
Zookeeper原理 二
查看>>
android之APP+JNI+Drv框架
查看>>
三阶魔方公式
查看>>
BP算法
查看>>
P1855 榨取kkksc03
查看>>
JAVA运行时动态加载类
查看>>
linux ifconfig -a
查看>>
MySql通过数据库文件恢复数据库
查看>>
ASP.NET网站和ASP.NET应用程序的区别
查看>>
Codeforces633G(SummerTrainingDay06-I dfs序+线段树+bitset)
查看>>
iOS判断手机某个App是否存在和常用scheme
查看>>
6 实现微信公众号 自动回复功能
查看>>
51Nod 1212无向图最小生成树
查看>>
hdu 4542 小明系列故事——未知剩余系
查看>>
Symbian UI 架构分类
查看>>
SVM入门(三)线性分类器Part 2
查看>>
mysql 执行 cannot found mac安装mysql的两种方法(含配置)
查看>>
BZOJ 1984: 月下“毛景树”( 树链剖分 )
查看>>