Click here to Skip to main content
15,880,796 members
Articles / Desktop Programming / MFC
Article

DllInstanceSwitcher class for switching to MFC Extension DLL resources

Rate me:
Please Sign up or sign in to vote.
4.59/5 (11 votes)
18 Jul 2002 108.1K   37   17
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


Written By
Web Developer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalhelp Pin
guyuewuhua24-Sep-09 19:43
guyuewuhua24-Sep-09 19:43 
GeneralMy vote of 1 Pin
KarstenK23-Nov-08 20:54
mveKarstenK23-Nov-08 20:54 
Questionshort example code? Pin
Anton Heidelwalder9-Nov-04 3:13
Anton Heidelwalder9-Nov-04 3:13 
AnswerRe: short example code? Pin
Member 62008829-Aug-05 9:03
Member 62008829-Aug-05 9:03 
GeneralHi, Problems with MFC in a DLL Pin
4-Dec-02 0:23
suss4-Dec-02 0:23 
Generalthanks a lot Pin
Anonymous8-Oct-02 3:27
Anonymous8-Oct-02 3:27 
it's working.;POMG | :OMG: Roll eyes | :rolleyes: Blush | :O Cry | :(( Rose | [Rose]
GeneralCommand ID's Pin
Peredelsky Alexey14-Aug-02 2:33
Peredelsky Alexey14-Aug-02 2:33 
GeneralExtension DLL resource management Pin
Gary R. Wheeler20-Jul-02 7:45
Gary R. Wheeler20-Jul-02 7:45 
GeneralRe: Extension DLL resource management Pin
Kuelshammer21-Jul-02 0:52
Kuelshammer21-Jul-02 0:52 
GeneralRe: Extension DLL resource management Pin
Peredelsky Alexey21-Jul-02 20:26
Peredelsky Alexey21-Jul-02 20:26 
GeneralExample code needed Pin
Kuelshammer20-Jul-02 7:16
Kuelshammer20-Jul-02 7:16 
GeneralRe: Example code needed Pin
Peredelsky Alexey21-Jul-02 20:33
Peredelsky Alexey21-Jul-02 20:33 
GeneralThat's useful! Pin
Philip Patrick19-Jul-02 20:06
professionalPhilip Patrick19-Jul-02 20:06 
GeneralI think it will not work in Win 98 & Me Pin
muayyad19-Jul-02 3:21
muayyad19-Jul-02 3:21 
GeneralRe: I think it will not work in Win 98 & Me Pin
Craig Henderson19-Jul-02 4:01
Craig Henderson19-Jul-02 4:01 
GeneralRe: I think it will not work in Win 98 & Me Pin
Peredelsky Alexey19-Jul-02 4:25
Peredelsky Alexey19-Jul-02 4:25 
GeneralNice solution Pin
Roger Allen19-Jul-02 2:58
Roger Allen19-Jul-02 2:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.