我在我的一个DLL中创建了一个简单的RAII类(我们将其称为exporting
DLL),该类监视我的应用程序中的配置还原:
头文件
class __declspec(dllexport) CEmuConfigurationRestoreMonitor
{
public:
CEmuConfigurationRestoreMonitor()
{
m_restoreInProgress = true;
}
~CEmuConfigurationRestoreMonitor()
{
m_restoreInProgress = false;
}
static bool IsRestoreInProgress()
{
return m_restoreInProgress;
}
private:
static bool m_restoreInProgress;
};
源文件
bool CEmuConfigurationRestoreMonitor::m_restoreInProgress = false;
这个想法是,
exporting
DLL中的还原代码将实例化堆栈上的CEmuConfigurationRestoreMonitor
,并且当该方法的结尾超出范围时,该标志将被关闭。问题是我想使用
IsRestoreInProgress()
从另一个DLL(假设importing
DLL)查询标志。这就是为什么我将__declspec(dllexport)
放在exporting
DLL的类声明中的原因。当我链接
importing
DLL时,出现了一个未解析的m_restoreInProgress
符号。因此,我将以下行添加到了importing
DLL中的.cpp文件中,它解决了该问题:bool CEmuConfigurationRestoreMonitor::m_restoreInProgress = false;
我现在发现的是,即使
m_restoreInProgress
设置为true
,当我从importing
DLL查询它时,它总是返回false
。importing
DLL中的静态初始化是否以某种方式覆盖了exporting
DLL中的实际(当前)值? 最佳答案
您已经为每个DLL提供了自己的m_restoreInProgress副本。
您可以通过以下方法解决此问题:
不使用内联函数。
在仅包含在导出DLL中的源文件中,对m_resotreInProgress使用文件范围的变量。