简体   繁体   English

初始化dll之间共享的变量

[英]Initialize variable shared beetween dlls

I all, I have a win32 application and several DLLs that must use a global variable. 所有人,我都有一个win32应用程序和几个必须使用全局变量的DLL。 In each dll I put 在我放的每个dll中

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. 现在,当加载DLL时,myvariable为NULL,我无法使用它。 How can I share the instance of the main program with all the DLLs? 如何与所有DLL共享主程序的实例?

You should make some changes in your program. 您应该在程序中进行一些更改。 If it is possible you can just move myvariable from the EXE to one from DLL. 如果可能的话,您可以将myvariable从EXE移到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. 通常,您可以从EXE导出函数或数据,但是在大多数情况下,这样做的意义不大。 So you can see this very seldom. 因此,您很少看到这种情况。 For example WinWord.exe or Excel.exe do this. 例如WinWord.exe或Excel.exe可以执行此操作。

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) . 如果确实需要从EXE导出函数或数据并在DLL中使用它,则应相对于GetProcAddressGetModuleHandle(NULL)使用动态绑定 You can do such kind of manual binding inside of DllMain . 您可以在DllMain中进行这种手动绑定。 The address of myvariable of the EXE you can save in the local myvariable of the DLL. 您可以将EXE的myvariable地址保存在DLL的本地myvariable中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM