温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C# 调用 C++编译的Dll

发布时间:2020-07-17 23:46:26 来源:网络 阅读:444 作者:followMyH 栏目:编程语言

1.创建一个C++动态链接库(通过VS图形引导界面)
2.添加C++类
CallC.cpp

// CallC.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"

extern "C" __declspec(dllexport) int Add(int a , int b)
{
    return a+b;
}

extern "C" __declspec(dllexport) int Sub(int a ,int b)
 {
     return a-b;
 }

3.在相同解决方案下创建一个C#工程(WinForm就可以),再添加一个调用C++类库的函数转换类,
CallCFunc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
    class CallCFunc
    {
        [DllImport("CallC.dll", EntryPoint = "Add", CallingConvention = CallingConvention.Cdecl)]
        public static extern int Add(int a,int b);
    }
}

注:需要将C++编译好的Dll 拷贝到C#的bin/Debug下(或Release),也可以一次性设置好C++类库的Dll 输出路径,设置方式如下:
右键项目->属性->属性配置->常规->输出目录。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI