DLL 需要访问其应用程序的符号
在 C++ 中,DLL 是否可以访问加载它的应用程序的某些符号? 我有一个加载插件(dll)的应用程序,这些插件需要访问该应用程序的一些API。
是否可以在不创建共享此 API 的新 DLL 的情况下实现此目的?
函数指针结构适合这种情况吗?
示例:主机应用程序中定义的 bool Log(char*) 函数和需要记录某些事件的插件。
In C++, is it possible for a DLL to access some symbols of the application that loaded it ?
I have an application that load plug-ins (dll), and these plug-ins need to access some API of the application.
Is it possible to achieve this without creating a new DLL that share this API ?
Is a struct of function pointers suitable in this situation ?
example: a bool Log(char*) function defined in the host application and a plug-in that need to log some events.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另一次投票赞成将回调接口传递到插件 DLL 中。即回调接口...
DLL 插件接口
主机将加载插件 DLL(如果需要,则动态加载),然后将其自身作为 IHostApplication* 传递给插件,从而启用您可能需要的任何回调。
Another vote for passing a callback interface into the plug-in DLL. I.e. the callback interface...
The DLL plugin interface
The host would load the plug-in DLL (dynamically if necessary), and then pass itself as an IHostApplication* to the plugin, enabling any callback you might want.
这是可能的,但您的设计选择值得怀疑。
EXE 可以像 DLL 一样导出函数,因此您可以按照您熟悉的方式使用 GetProcAddress。
但是,为什么要设计成让插件需要了解主机程序的内部功能呢?应该是相反的。
主机应该要求插件实现一组例程(它们导出),遵守特定的合同。作为接口的一部分,如有必要,主机可以通过指向结构的指针(例如版本信息、功能)等向它们传递特定于主机的信息。
插件不应尝试获取指向主机模块的函数指针。
It's possible, but your design choice is questionable.
An EXE can export functions in the same way a DLL can, so you can use GetProcAddress in the way you're familiar with.
However, why have you designed it so that plugins need to know anything about the host program's internal functions? It should be the other way around.
The host should require that plugins implement a set of routines (which they export), adhering to a particular contract. As part of their interface, the host could pass them host-specific information in a pointer to a struct (say, version info, capabilities) and the like, if necessary.
The plugins should not be attempting to acquire function pointers to the host module.
回调函数指针、回调函数指针列表或回调接口指针 - 我将使用这些选项之一。 Dll 客户端必须实现回调接口并将指向该接口的指针传递给动态创建的 Dll。动态创建的Dll保留这个指针,并在必要时调用它的函数,例如报告一些事件。
Callback function pointer, list of callback function pointers, or pointer to callback interface - I would use one of these options. Dll client must implement callback interface and pass pointer to this interface to dynamically created Dll. Dynalically created Dll keeps this pointer, and calls its functions when necessary, for example, to report some events.