初始化 dll 之间共享的变量
我都, 我有一个 win32 应用程序和几个必须使用全局变量的 DLL。在我放入的每个 dll
extern MYTYPE* myvariable = NULL;
以及现在的主程序
MYTYPE* myvariable = NULL;
mavariable = new MYTYPE();
....
中,当加载 DLL 时,我的变量为 NULL,我无法使用它。如何与所有DLL共享主程序的实例?
I all,
I have a win32 application and several DLLs that must use a global variable. In each dll I put
extern MYTYPE* myvariable = NULL;
and in the main program I have
MYTYPE* myvariable = NULL;
mavariable = new MYTYPE();
....
now, when the DLLs are loaded myvariable is NULL and I cannot use it. How can I share the instance of the main program with all the DLLs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该对您的程序进行一些更改。如果可能的话,您可以将
myvariable
从 EXE 移动到 DLL 中。然后就可以继续使用导入库了。一般来说,您可以从 EXE 中导出函数或数据,但在大多数情况下,这样做的意义不大。所以你很少能看到这种情况。例如 WinWord.exe 或 Excel.exe 执行此操作。
如果确实需要从 EXE 导出功能或数据并在 DLL 中使用它,您应该使用动态绑定GetProcAddress 和 GetModuleHandle(NULL)。您可以在 DllMain 内部执行此类手动绑定。您可以将 EXE 的
myvariable
地址保存在 DLL 的本地myvariable
中。You should make some changes in your program. If it is possible you can just move
myvariable
from the EXE to one from DLL. Then you can continue to use import libraries.It general you can export functions or data from an EXE, but in the most cases there are less sense to do this. So you can see this very seldom. For example WinWord.exe or Excel.exe do this.
If really need to export frunction or data from EXE and use it in the DLL you should use dynamical binding with respect of GetProcAddress and GetModuleHandle(NULL). You can do such kind of manual binding inside of DllMain. The address of
myvariable
of the EXE you can save in the localmyvariable
of the DLL.也许您正在寻找这个:Windows & C++:外部 & __declspec(dllimport)
maybe you're looking for this: Windows & C++: extern & __declspec(dllimport)