上一篇我们相当于封装的是C语言风格的动态dll库,供C#来调用的。
C#调用C++封装的SDK库(dll动态库)——上
如果我们要封装的是下面的类呢?我们该怎么办?大家先思考下。
class Calculation
{
public:
Calculation();
int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);
int divide(int a, int b);
};
不知道大家还记得设计模式中的单例模式吗?
我们可以采用类似的处理方式,通过创建一个实例函数指针的方式,来通过这个实例作为一个参数来调用累的成员函数。
如下所示:
Calculation* getCalculation();
int add(Calculation* pCalculation, int a, int b);
1、我们创建一个DLL的动态库工程,命名CalculationDLL。
在项目中添加一个Calculation类。
Calculation.h头文件添加如下代码:
#pragma once
#ifdef DLLCALCULATION_EXPORTS
#define DLLCALCULATION_API __declspec(dllexport)
#else
#define DLLCALCULATION_API __declspec(dllimport)
#endif // DLLCALCULATION_EXPORTS
class Calculation
{
public:
Calculation();
int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);
int divide(int a, int b);
};
EXTERN_C DLLCALCULATION_API Calculation* getCalculation();
EXTERN_C DLLCALCULATION_API void destructorCalculation(Calculation* pCalculation);
EXTERN_C DLLCALCULATION_API int add(Calculation* pCalculation, int a, int b);
EXTERN_C DLLCALCULATION_API int subtract(Calculation* pCalculation, int a, int b);
EXTERN_C DLLCALCULATION_API int multiply(Calculation* pCalculation, int a, int b);
EXTERN_C DLLCALCULATION_API int divide(Calculation* pCalculation, int a, int b);
Calculation.cpp文件添加如下代码:
#include "pch.h"
#include "Calculation.h"
Calculation::Calculation()
{
}
int Calculation::add(int a, int b)
{
return (a + b);
}
int Calculation::subtract(int a, int b)
{
return (a - b);
}
int Calculation::multiply(int a, int b)
{
return (a * b);
}
int Calculation::divide(int a, int b)
{
return (a / b);
}
DLLCALCULATION_API Calculation* getCalculation()
{
return new Calculation();
}
DLLCALCULATION_API void destructorCalculation(Calculation* pCalculation)
{
if( pCalculation )
{
delete pCalculation;
pCalculation = NULL;
}
}
DLLCALCULATION_API int add(Calculation* pCalculation, int a, int b)
{
return pCalculation->add(a,b);
}
DLLCALCULATION_API int subtract(Calculation* pCalculation, int a, int b)
{
return pCalculation->subtract(a,b);
}
DLLCALCULATION_API int multiply(Calculation* pCalculation, int a, int b)
{
return pCalculation->multiply(a,b);
}
DLLCALCULATION_API int divide(Calculation* pCalculation, int a, int b)
{
return pCalculation->divide(a,b);
}
在属性页,C/C++->预处理器->预处理器定义,添加DLLCALCULATION_EXPORTS
创建一个C#的控制台项目:
在Program.cs中输入下面的调用代码:
// See https://aka.ms/new-console-template for more information
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.Runtime.InteropServices;
namespace ConsoleApp1
{
class Program
{
[DllImport("C:\\My_vs2022Project\\CalculationDLL\\x64\\Release\\CalculationDLL.dll", CallingConvention = CallingConvention.Cdecl)]
public extern static IntPtr getCalculation();
[DllImport("C:\\My_vs2022Project\\CalculationDLL\\x64\\Release\\CalculationDLL.dll", CallingConvention = CallingConvention.Cdecl)]
public extern static void destructorCalculation(IntPtr pCalculation);
[DllImport("C:\\My_vs2022Project\\CalculationDLL\\x64\\Release\\CalculationDLL.dll", CallingConvention = CallingConvention.Cdecl)]
public extern static int add(IntPtr pCalculation, int a, int b);
[DllImport("C:\\My_vs2022Project\\CalculationDLL\\x64\\Release\\CalculationDLL.dll", CallingConvention = CallingConvention.Cdecl)]
public extern static int subtract(IntPtr pCalculation, int a, int b);
[DllImport("C:\\My_vs2022Project\\CalculationDLL\\x64\\Release\\CalculationDLL.dll", CallingConvention = CallingConvention.Cdecl)]
public extern static int multiply(IntPtr pCalculation, int a, int b);
[DllImport("C:\\My_vs2022Project\\CalculationDLL\\x64\\Release\\CalculationDLL.dll", CallingConvention = CallingConvention.Cdecl)]
public extern static int divide(IntPtr pCalculation, int a, int b);
static void Main(string[] args)
{
IntPtr pCalculation = getCalculation();
int a = 10;
int b = 2;
Console.WriteLine("a+b= " + add(pCalculation, a, b));
Console.WriteLine("a-b= " + subtract(pCalculation, a, b));
Console.WriteLine("a*b= " + multiply(pCalculation, a, b));
Console.WriteLine("a/b= " + divide(pCalculation, a, b));
destructorCalculation(pCalculation);
Console.WriteLine("calc end.");
Console.ReadKey();
}
}
}
好了,你可以运行看看效果了。
上一篇:C#调用C++封装的SDK库(dll动态库)——上
本文原创作者:冯一川([email protected]),未经作者授权同意,请勿转载。