C#调用C++写的代码(VS2015)

来源:互联网 发布:nginx rtmp 点播配置 编辑:程序博客网 时间:2024/06/06 09:01

将C++代码编译成dl的时候,新建控制台程序
注意的是建立dll工程之后,在属性中选择“动态链接库和在静态库中使用MFC”这一项,编译出来的dll文件不需要引入库文件(.lib)。
将dll文件放在C#工程的bin文件夹下(和C#的exe程序在一个目录下)就可以调用。
C++dll中编写的代码:

extern "C" __declspec(dllexport) int Aera(int argv)

C#的引用代码:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Runtime.InteropServices;//需要加上的一句namespace cv{    public partial class Form1 : Form    {        [DllImport("Aera_Caculate_Dll.dll")]        public static extern int Aera(int argv);        //后面就可以直接使用Aera函数。

当出现“调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。请检查 PInvoke 签名的调用约定和参数与非托管的目标签名是否匹配。”的问题时,加入“CallingConvention = CallingConvention.Cdecl”属性

[DllImport("Aera_Caculate_Dll.dll",CallingConvention = CallingConvention.Cdecl)]
阅读全文
0 0
原创粉丝点击