c++动态库打包为dll文件供C#项目调用

24 篇文章 2 订阅

C++与C#接口交互需要通过DLL库来完成。

编写C++动态库

创建项目:

添加C++测试类:

在MathAPI.h定义接口方法:

#pragma once
class MathAPI
{
public:
	MathAPI();
	~MathAPI();
		static _declspec(dllexport) double Add(double a, double b);
		static _declspec(dllexport) double Subtract(double a, double b);
		static _declspec(dllexport) double Multiply(double a, double b);
		static _declspec(dllexport) double Devide(double a, double b);
};

在MathAPI.cpp实现接口方法:

#include "pch.h"
#include "MathAPI.h"
#include "stdexcept"

MathAPI::MathAPI()
{
}


MathAPI::~MathAPI()
{
}
double MathAPI::Add(double a, double b)
{
	return a + b;
}

double MathAPI::Subtract(double a, double b)
{
	return a - b;
}

double MathAPI::Multiply(double a, double b)
{
	return a * b;
}

double MathAPI::Devide(double a, double b)
{
	if (b == 0)
	{
		throw new _exception;
	}
	return a / b;
}

修改项目的属性,输出为动态库dll:

然后生成即可。

C#调用dll库

利用vs提供的dumpbin.exe工具可以查看dll的入口,在vs安装目录搜索即可找到。

dumpbin的选项指令:

为了快速看到效果,直接将FFmpegDXVA2dll.dll文件复制到dumpbin.exe所在的目录执行以下命令 :

上面输出中?Add@MathAPI@@SANNN@Z 表示ADD函数的入口,我们在C#程序中使用时需要设置这个入口。

创建一个C#控制台程序,并将我们需要的dll库放入其中:

C#中定义dll接口访问:

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

namespace DllImportTest
{
    public class Math
    {
        [DllImport("FFmpegDXVA2dll.dll", EntryPoint = "?Add@MathAPI@@SANNN@Z", CallingConvention = CallingConvention.Cdecl)]
        public static extern double Add(double a, double b);

        [DllImport("FFmpegDXVA2dll.dll", EntryPoint = "?Subtract@MathAPI@@SANNN@Z", CallingConvention = CallingConvention.Cdecl)]
        public static extern double Subtract(double a, double b);

        [DllImport("FFmpegDXVA2dll.dll", EntryPoint = "?Multiply@MathAPI@@SANNN@Z", CallingConvention = CallingConvention.Cdecl)]
        public static extern double Multiply(double a, double b);

        [DllImport("FFmpegDXVA2dll.dll", EntryPoint = "?Devide@MathAPI@@SANNN@Z", CallingConvention = CallingConvention.Cdecl)]
        public static extern double Devide(double a, double b);
    }
}

Program运行:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DllImportTest
{
    class Program
    {
        static void Main(string[] args)
        {
            double a = 20, b = 5;
            Console.WriteLine(Math.Add(a,b));
            Console.WriteLine(Math.Subtract(a, b));
            Console.WriteLine(Math.Multiply(a, b));
            Console.WriteLine(Math.Devide(a, b));
            Console.Read();
        }
    }
}

输出结果:

至此,C#已经成功实现调用C++ 动态dll库的功能了。

参考文章:

https://blog.csdn.net/nini_boom/article/details/78084408

https://blog.csdn.net/dearkundy/article/details/73331514

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值