AfxRegisterWndClass() within dll
I tried to implement a message-only CWnd class that can be instantiated
ANYWHERE. To use, I put this class's .h and .cpp file into a dll, and the I
use AfxLoadLibrary() and AfxFreeLibrary() to load and unload that dll, and
such dll will be unloaded and then reloaded (possibly repeatedly). (NOTE:
please don't question why I have to keep unloading and then reloading the
same dll; the app is for my client so I cannot tell any more about that).
For the first time the dll is loaded and my CMsgOnlyWnd is created, all is
fine. The problem arises when the dll is unloaded and then reloaded, thereby
re-executing AfxRegisterWndClass(0), it generates a rubbish class string, and
of course fail the window creation.
How to solve this problem, so that it can truly be instantiated anywhere?
class CMsgOnlyWnd : public CWnd
{
// ......
};
BOOL CMsgOnlyWnd::Create()
{
static LPCTSTR sg_lpszWndClass = ::AfxRegisterWndClass(0);
BOOL bCreated = CWnd::CreateEx(0, sg_lpszWndClass, _T(""), 0, 0, 0, 0, 0,
HWND_MESSAGE, NULL);
ASSERT(bCreated);
return bCreated;
}