如何模拟“应用程序中的全局变量的多个实例”静态库的行为但是使用DLL? [英] How to mimic the "multiple instances of global variables within the application" behaviour of a static library but using a DLL?

查看:277
本文介绍了如何模拟“应用程序中的全局变量的多个实例”静态库的行为但是使用DLL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个用C / C ++编写的应用程序,它被分成单个EXE和多个DLL。每个这些DLL使用相同的静态库( utilities.lib )。



实用程序静态库将在应用程序的运行时实际上具有多个实例。将 utilities.lib 已链接到


$ b $每个模块的全局变量的一个副本(即DLL或EXE) b

(这是所有已知的和好的,但是值得讨论静态库在DLL的上下文中的行为的一些背景。)

现在我的问题。 。我们要改变 utilities.lib ,使它成为一个DLL。它变得非常大和复杂,我们希望以DLL形式而不是 .lib 形式分发它。问题是,对于这个应用程序,我们希望保留每个应用程序DLL在实用程序库中拥有自己的全局变量副本的当前行为。 你会怎么做?实际上,我们不需要这个全局变量,只有一些;



我们的想法:


  1. 我们关心的库中没有很多全局变量,与一个访问器,做一些时髦的伎俩试图找出哪个DLL正在调用它。大概我们可以走上调用栈,并为每个函数掏出 HMODULE ,直到找到一个 utilities.dll

  2. 我们可以在调用<$ c $中的任何函数之前,强制调用者设置一个特定的全局变量(也许是线程本地的) c> utilities.dll 。

  3. 我们可以找到一些加载 utilities.dll 的方法。在运行时多次。也许我们需要在构建时创建多个重命名的副本,这样每个应用程序DLL都可以拥有它自己的实用程序DLL的副本。这消除了首先使用DLL的一些优点,但是存在不需要这种静态库样式行为并且仍然可以从 utilities.lib 成为 utilities.dll


解决方案

你可能最好只是让utilities.dll导出附加函数来分配和释放一个包含变量的结构,然后让每个其他worker DLL在需要时在运行时调用这些函数,例如DllEntryPoint()的DLL_ATTACH_PROCESS和DLL_DETACH_PROCESS阶段。这样,每个DLL都有自己的变量的本地副本,并且可以将结构传回给utilities.dll函数作为一个附加参数。



在每个工作程序DLL内部直接声明各个变量,然后在需要时将它们作为输入/输出参数传递给utilities.dll。



无论如何,没有实用程序。 dll尝试自己找出上下文信息。它不会很好地工作。


We have an application written in C/C++ which is broken into a single EXE and multiple DLLs. Each of these DLLs makes use of the same static library (utilities.lib).

Any global variable in the utility static library will actually have multiple instances at runtime within the application. There will be one copy of the global variable per module (ie DLL or EXE) that utilities.lib has been linked into.

(This is all known and good, but it's worth going over some background on how static libraries behave in the context of DLLs.)

Now my question.. We want to change utilities.lib so that it becomes a DLL. It is becoming very large and complex, and we wish to distribute it in DLL form instead of .lib form. The problem is that for this one application we wish to preserve the current behaviour that each application DLL has it's own copy of the global variables within the utilities library. How would you go about doing this? Actually we don't need this for all the global variables, only some; but it wouldn't matter if we got it for all.


Our thoughts:

  1. There aren't many global variables within the library that we care about, we could wrap each of them with an accessor that does some funky trick of trying to figure out which DLL is calling it. Presumably we can walk up the call stack and fish out the HMODULE for each function until we find one that isn't utilities.dll. Then we could return a different version depending on the calling DLL.
  2. We could mandate that callers set a particular global variable (maybe also thread local) prior to calling any function in utilities.dll. The utilities DLL could then use this global variable value to determine the calling context.
  3. We could find some way of loading utilities.dll multiple times at runtime. Perhaps we'd need to make multiple renamed copies at build time, so that each application DLL can have it's own copy of the utilities DLL. This negates some of the advantages of using a DLL in the first place, but there are other applications for which this "static library" style behaviour isn't needed and which would still benefit from utilities.lib becoming utilities.dll.

解决方案

You are probably best off simply having utilities.dll export additional functions to allocate and deallocate a structure that contains the variables, and then have each of your other worker DLLs call those functions at runtime when needed, such as in the DLL_ATTACH_PROCESS and DLL_DETACH_PROCESS stages of DllEntryPoint(). That way, each DLL gets its own local copy of the variables, and can pass the structure back to utilities.dll functions as an additional parameter.

The alternative is to simply declare the individual variables locally inside each worker DLL directly, and then pass them into utilities.dll as input/output parameters when needed.

Either way, do not have utilities.dll try to figure out context information on its own. It won't work very well.

这篇关于如何模拟“应用程序中的全局变量的多个实例”静态库的行为但是使用DLL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆