Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

DllInstanceSwitcher class for switching to MFC Extension DLL resources

0.00/5 (No votes)
18 Jul 2002 1  
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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here