65.9K
CodeProject is changing. Read more.
Home

DllInstanceSwitcher class for switching to MFC Extension DLL resources

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.59/5 (9 votes)

Jul 19, 2002

viewsIcon

109884

AFX_MANAGE_STATE(AfxGetStaticModuleState()) results in an error. This is the solution to the problem.

Introduction

Some time ago I wrote an MFC Extension DLL. This DLL contained CMDIFrameWnd derived class with CToolbar placed on it. After dynamically loading this DLL into main application I saw that toolbar tool-tip strings loaded from the main application resources, because they have string IDs equal to DLL strings IDs.

Macro AFX_MANAGE_STATE(AfxGetStaticModuleState()) doesn't work because Microsoft don't want us to use it.

The solution is to write own resource switcher class and create it everywhere we need to use Extension DLL resources. Here is the code of this class:

extern "C" AFX_EXTENSION_MODULE ExtensionDLL;
class DllInstanceSwitcher
{
public:
    DllInstanceSwitcher()
    {
        m_hInst = AfxGetResourceHandle();
        AfxSetResourceHandle(ExtensionDLL.hResource);
    }

    ~DllInstanceSwitcher()
    {
        AfxSetResourceHandle(m_hInst);
    }

private:
    HINSTANCE m_hInst;
};

#define SWITCH_RESOURCE  DllInstanceSwitcher __SwitchInstance;

Also we need to replace declaration of:

static AFX_EXTENSION_MODULE someDLL = { NULL, NULL }

with

extern "C" AFX_EXTENSION_MODULE ExtensionDLL = { NULL, NULL }

And all we need is to insert SWITCH_RESOURCE; everywhere we want to use DLL resources.