Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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:</p> <p>Static lib has Log.h:</p> <pre><code>class Log { private: Log(); public: ~Log(); static Log&amp; GetInstance() { #ifdef _USRDLL typedef Log* (*GetLoggerFn)(); HMODULE mod = GetModuleHandle( NULL ); GetLoggerFn getLogger = (GetLoggerFn)::GetProcAddress( mod, "GetLogger" ); Log* Instance = getLogger(); return *Instance; #else static Log Instance; return Instance; #endif } void Write(const std::string&amp; str ); }; #define LOG Log::GetInstance() </code></pre> <p>Static lib has Log.cpp:</p> <pre><code>#include "Log.h" void Log::Write(const std::string&amp; str ) { std::cout &lt;&lt; this &lt;&lt; " " &lt;&lt; str &lt;&lt; std::endl; } Log::Log() { } Log::~Log() { std::cout &lt;&lt; "Log destroyed" &lt;&lt; std::endl; } </code></pre> <p>DLL just has a log statement in DllMain:</p> <pre><code>#include "../static/Log.h" BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } LOG.Write("Hello from dll"); return TRUE; } </code></pre> <p>And EXE looks like this:</p> <pre><code>#include "stdafx.h" #include "../static/Log.h" #include &lt;Windows.h&gt; extern "C" { __declspec( dllexport ) Log* GetLogger() { return &amp;LOG; } } int _tmain(int argc, _TCHAR* argv[]) { LOG.Write("Hello from exe"); HMODULE mod = ::LoadLibraryA( "../Debug/tdll.dll"); ::FreeLibrary( mod ); LOG.Write("unloaded library"); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload