RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
C#调用C++编译的Dll

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 输出路径,设置方式如下:
右键项目->属性->属性配置->常规->输出目录。


当前名称:C#调用C++编译的Dll
当前网址:http://scyingshan.cn/article/pdgsei.html