C#調(diào)用C++封裝的SDK庫(dll動(dòng)態(tài)庫)——下
一、說明
上一篇我們相當(dāng)于封裝的是C語言風(fēng)格的動(dòng)態(tài)dll庫,供C#來調(diào)用的。
C#調(diào)用C++封裝的SDK庫(dll動(dòng)態(tài)庫)——上
如果我們要封裝的是下面的類呢?我們?cè)撛趺崔k?大家先思考下。
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);
};
?二、思路
不知道大家還記得設(shè)計(jì)模式中的單例模式嗎?
我們可以采用類似的處理方式,通過創(chuàng)建一個(gè)實(shí)例函數(shù)指針的方式,來通過這個(gè)實(shí)例作為一個(gè)參數(shù)來調(diào)用累的成員函數(shù)。
如下所示:
Calculation* getCalculation();
int add(Calculation* pCalculation, int a, int b);
三、創(chuàng)建動(dòng)態(tài)DLL
1、我們創(chuàng)建一個(gè)DLL的動(dòng)態(tài)庫工程,命名CalculationDLL。
在項(xiàng)目中添加一個(gè)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++->預(yù)處理器->預(yù)處理器定義,添加DLLCALCULATION_EXPORTS
?四、創(chuàng)建C#控制臺(tái)調(diào)用項(xiàng)目
創(chuàng)建一個(gè)C#的控制臺(tái)項(xiàng)目:
?在Program.cs中輸入下面的調(diào)用代碼:
// 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();
}
}
}
好了,你可以運(yùn)行看看效果了。
上一篇:C#調(diào)用C++封裝的SDK庫(dll動(dòng)態(tài)庫)——上文章來源:http://www.zghlxwxcb.cn/news/detail-424898.html
本文原創(chuàng)作者:馮一川(ifeng12358@163.com),未經(jīng)作者授權(quán)同意,請(qǐng)勿轉(zhuǎn)載。文章來源地址http://www.zghlxwxcb.cn/news/detail-424898.html
到了這里,關(guān)于C#調(diào)用C++封裝的SDK庫(dll動(dòng)態(tài)庫)——下的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!