I created a static library with class:
class CLog
{
private:
CLog();
...
...
public:
static CLog& GetInstance()
{
static CLog Instance;
return Instance;
}
void Write(char *cpPr);
};
#define Log CLog::GetInstance()
This library is linked to a dll and a main program. The dll is loaded by LoadLibrary. In this case is obvious that calling Log.Write in a main exe and in dll, creates two separate instances of CLog. Any ideas how to work around this issue and still provide dynamic loading a dll?
If you instead use a static class member (as opposed to using a statically allocated local variable), I believe this will only create a single instance (I cannot verify this at the moment). Something like:
Edit
Okay, so Mark pointed out that this doesn't work either. The DLL and the EXE still get different CLog instances. Something else must be thought of.
The method I used was to export a function called GetLogger from the EXE which provides a pointer to the singleton. GetInstance() implementation is conditional on the _USRDLL preprocessor define. When _USRDLL is set (for the DLL compilation) GetInstance() calls GetModuleHandle() to get a handle to the EXE and loads the function called GetLogger. Here's the code based on your example:
Static lib has Log.h:
Static lib has Log.cpp:
DLL just has a log statement in DllMain:
And EXE looks like this:
The problem is that every project that links the static library, be it main program or DLL, will get a separate copy of the static variable. This breaks the typical method of creating a singleton.
The simplest way around this is to create another DLL which holds the singleton, rather than a static library. Since only one linker output will contain the static variable, the problem is solved.
In my own case I created a singleton manager that identified each singleton by a unique GUID and ensured that only one copy existed application wide. The singleton manager existed as its own DLL.