English 中文(简体)
C++ 单一吨级
原标题:C++ a singleton class with dll

我创建了一个固定图书馆,分为:

class CLog
{
   private:
   CLog();
   ...
   ...
   public:
   static CLog& GetInstance()                                
   {
           static CLog Instance;
           return Instance;
   }
   void Write(char *cpPr);
};
#define Log CLog::GetInstance()

该图书馆与一个小馆和一个主要方案连接。 拖拉由LodLibrary装载。 在本案中,很明显的是,将Log.Write称作主力和干.,造成两个单独的行车监督记录仪。 任何想法如何围绕这一问题开展工作,仍能提供动态的负荷?

问题回答

问题是,每一个连接静态图书馆的项目,无论是主要方案还是数字图书馆,都将获得固定变量的单独拷贝。 这打破了创建单一州这一典型方法。

围绕这一点,最简单的办法是创建另一个拥有单一州而不是固定图书馆的DL。 由于只有一个链接输出器将包含静态变量,这个问题已经解决。

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.

这里有一个简单的图书馆,支持在动态图书馆和可读图书馆之间分享相同的单一州案例。 (在双赢、中、小aco上测试)

为了获得T类的单一吨位,仅使用单一吨和带;T>()是科索沃。

https://github.com/xhawk18/singleton-cpp

#include "singleton-cpp/singleton.h"

MyObject &obj = singleton<MyObject>();

我使用的方法是从出口企业出口一个称为“Get Carloger”的功能,向单一州提供一名点子。 有条件执行——美国RDL 预处理器界定。 •USRDLL(为DL汇编)设立时,GetInstance(为DL)电话:GemoduleHandle()接手EXE,并装上称为Gelogger的职能。 以你为榜样的守则:

法定校准有Logh:

class Log
{
  private:
  Log();

  public:
  ~Log();

  static Log& 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& str );
};
#define LOG Log::GetInstance()

法定校准有Log.cpp:

#include "Log.h"

void Log::Write(const std::string& str )
{
    std::cout << this << "  " << str << std::endl;
}

Log::Log()
{
}

Log::~Log()
{
    std::cout << "Log destroyed" << std::endl;
}

DLL刚刚在DllMain做了一个记录:

#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;
}

EXE看着这一点:

#include "stdafx.h"
#include "../static/Log.h"
#include <Windows.h>
extern "C"
{
    __declspec( dllexport ) Log* GetLogger()
    {
        return &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;
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签