c++ DLL封装和导出:类的封装和导出(非直接导出类而是导出类中的函数)

6 篇文章 0 订阅
3 篇文章 0 订阅

代码链接:https://download.csdn.net/download/wangMaryann/12348492

c++dll可以导出函数/变量/类,但是实际应用中,应避免直接导出变量或者类,原因如下:

  • 直接导出变量,这等于去掉了代码中的一个抽象层,从而使得代码更加难以维护;
  • 直接导出类,除非确定Dll和加载Dll的应用程序,使用的编译器一样,否则应避免从dll中导出类。

如果需要从c++中导出类,可以采用如下方式,导出类中的函数,而不是直接导出类:

实现方式:

1、创建一个基类,一个派生类;

2、导出派生类中的函数;

3、通过采用模块定义(.def)文件声明,规范函数导出的名称。导出的函数,有些编译器会自动更改名字,增加很多限定符号,可以通过def文件规范化。

创建一个基类:base

class base
{
public:
	base();
	~base();

public:
	virtual bool add(int a, int b, int* sum) = 0;
	virtual bool sub(int* diff, int a, int b ) = 0;
};

创建派生类

#include "base.h"

class fun : public base
{
public:
	fun();
	~fun();

public:
	virtual bool add(int a, int b, int* sum);
	virtual bool sub(int* diff, int a, int b = 10);
};

导出函数声明

#if defined(DLL_EXPORTS)
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif

extern "C" DLL_API bool _stdcall add(int a, int b, int* sum);
extern "C" DLL_API bool _stdcall sub(int* diff, int a, int b = 10);

导出函数定义

fun *p = new fun();

bool _stdcall add(int a, int b, int* sum)
{
	return  p->add(a, b, &(*sum));
}

bool _stdcall sub(int* diff, int a, int b)
{
	return p->sub(&(*diff), a, b);
}

def 文件定义

LIBRARY hdll

EXPORTS

add @1
sub @2

后记:

导出后的函数,查看dll的接口函数,名称会变成如下形式,这个就是被编译器更改的,至于符号的具体含义,可以自行百度。名称被编译器改编的原因是,导出时使用了__stdcall关键字(如果混合使用c/c++编程,需要使用extern “C”)。如果想保持和导出的形式一样,有两种方式。

  • 可以使用def文件,并在def文件中包含EXPORTS段

EXPORTS

      add

      sub

  • 可以增加下面的代码 

#pragma comment(linker, "/export:add = _add@12")

#pragma comment(linker, "/export:sub = _sub@12")

                                           图一

                                        图二

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C# 中调用 C++ DLL 中的 std::string 型的方法,需要使用 `StringBuilder` 来接收返回值,同时需要在 C# 中定义与 C++ 中的方法签名相匹配的委托型,并使用 `Marshal.GetDelegateForFunctionPointer` 方法将 DLL 导出函数的指针转换为委托对象。以下是一个示例: C++ DLL 中的和方法: ```cpp #include <string> class MyCppClass { public: std::string MyCppMethod(const std::string& str) { return "Hello " + str + " from C++!"; } }; extern "C" { __declspec(dllexport) MyCppClass* CreateMyCppClass() { return new MyCppClass(); } __declspec(dllexport) void DeleteMyCppClass(MyCppClass* p) { delete p; } __declspec(dllexport) void MyCppClass_MyCppMethod(MyCppClass* p, const char* str, char* buffer, int bufferSize) { std::string s(str); std::string result = p->MyCppMethod(s); strncpy(buffer, result.c_str(), bufferSize); } } ``` C# 代码: ```csharp using System; using System.Runtime.InteropServices; using System.Text; class Program { [DllImport("MyCppDll.dll")] public static extern IntPtr CreateMyCppClass(); [DllImport("MyCppDll.dll")] public static extern void DeleteMyCppClass(IntPtr p); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate void MyCppClass_MyCppMethodDelegate(IntPtr p, string str, StringBuilder sb, int bufferSize); static void Main() { IntPtr p = CreateMyCppClass(); try { MyCppClass_MyCppMethodDelegate MyCppClass_MyCppMethod = (MyCppClass_MyCppMethodDelegate)Marshal.GetDelegateForFunctionPointer( Marshal.GetFunctionPointerForDelegate((MyCppClass_MyCppMethodDelegate)delegate { }), typeof(MyCppClass_MyCppMethodDelegate) ); StringBuilder sb = new StringBuilder(256); MyCppClass_MyCppMethod(p, "World", sb, sb.Capacity); Console.WriteLine(sb.ToString()); } finally { DeleteMyCppClass(p); } } } ``` 在上述代码中,我们首先声明了从 C++ DLL 中导入的函数 `CreateMyCppClass` 和 `DeleteMyCppClass`,它们分别用于创建和销毁 C++ 的实例。接下来,我们定义了一个与 C++ 中的方法签名相匹配的委托型 `MyCppClass_MyCppMethodDelegate`,并使用 `Marshal.GetDelegateForFunctionPointer` 方法将 DLL 导出函数 `MyCppClass_MyCppMethod` 的指针转换为委托对象。 在调用 C++ 的方法之前,我们需要先创建一个 C++ 的实例,并将其封装在 `IntPtr` 对象中。在调用 C++ 的方法时,我们使用上述委托对象来调用 C++ DLL 中的函数 `MyCppClass_MyCppMethod`,并使用 `StringBuilder` 来接收返回值。需要注意的是,在调用 `MyCppClass_MyCppMethod` 之前,我们需要先创建一个足够大的 `StringBuilder` 对象,并将其容量传递给 C++ DLL 中的方法,以确保能够接收到完整的字符串。最后,我们使用 `DeleteMyCppClass` 函数销毁 C++ 的实例。 需要注意的是,在 C++ DLL 中,函数的参数型必须为基本型或指针型,不能使用引用型。另外,在 C++ DLL 中使用的字符串编码格式必须与 C# 中使用的字符串编码格式相同,否则可能会出现乱码问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值