gpt4 book ai didi

c++ - 从运行时加载的 Dll 调用主程序的方法

转载 作者:行者123 更新时间:2023-11-30 03:06:08 24 4
gpt4 key购买 nike

我有一个(大而复杂的)C++ 程序,它有自己的类和方法。(显然)

我想在运行时加载一个 dll。我想从主程序中调用传递类实例的 dll 中的一个函数,这个函数将使用此类的方法。

伪代码

主程序:

 class MyClass{
myattr1();
myattr2();
mymethod1();
mymethod2();
}

void main(){
MyClass* object = &(new MyClass())
handle_of_the_dll = *some function that load the dll*
dllfunc = getfunc(handle_of_the_dll, "interesting_function")
dllfunc(object)
[...etc...]

和dll

#ifdef __cplusplus
extern C {
#endif
#ifdef BUILD_DLL
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif

#include all the needed headers from main program, where the class is defined

EXPORT void interesting_functionn(MyClass object){
object.mymethod1();
}
#ifdef __cplusplus
}
#endif

这有效/可行/正确吗?

编辑:

我知道这对于一般程序来说是一个糟糕的设计,但这种方法旨在让用户能够使用他们的自定义 dll 来访问主程序的 api。像 Cpython 模块之类的东西

最佳答案

有效且可行。在 Visual C++ 中生成示例 DLL,代码将涵盖所有基础。将客户端中的静态链接替换为 LoadLibraryGetProcAdress

名称修改可能是个问题,DependencyWalker会在那里帮助你。

在埃默里管理方面保持低调。例如,如果您在一侧使用内存管理器而在另一侧不使用,那您就是在自找麻烦。

关于c++ - 从运行时加载的 Dll 调用主程序的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6917520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com