Click here to Skip to main content
11,931,104 members (70,074 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked

DllInstanceSwitcher class for switching to MFC Extension DLL resources

, 18 Jul 2002
Rate this:
Please Sign up or sign in to vote.
AFX_MANAGE_STATE(AfxGetStaticModuleState()) results in an error. This is the solution to the problem.


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:

class DllInstanceSwitcher
        m_hInst = AfxGetResourceHandle();


    HINSTANCE m_hInst;

#define SWITCH_RESOURCE  DllInstanceSwitcher __SwitchInstance;

Also we need to replace declaration of:




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


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


About the Author

Peredelsky Alexey
Web Developer
Russian Federation Russian Federation
No Biography provided

You may also be interested in...

Comments and Discussions

Generalhelp Pin
guyuewuhua24-Sep-09 20:43
memberguyuewuhua24-Sep-09 20:43 
GeneralMy vote of 1 Pin
KarstenK23-Nov-08 21:54
memberKarstenK23-Nov-08 21:54 
Questionshort example code? Pin
Anton Heidelwalder9-Nov-04 4:13
memberAnton Heidelwalder9-Nov-04 4:13 
AnswerRe: short example code? Pin
dickey wong29-Aug-05 10:03
memberdickey wong29-Aug-05 10:03 
GeneralHi, Problems with MFC in a DLL Pin
BeginnerOnline4-Dec-02 1:23
memberBeginnerOnline4-Dec-02 1:23 
Generalthanks a lot Pin
Anonymous8-Oct-02 4:27
sussAnonymous8-Oct-02 4:27 
GeneralCommand ID's Pin
Peredelsky Alexey14-Aug-02 3:33
memberPeredelsky Alexey14-Aug-02 3:33 
GeneralExtension DLL resource management Pin
Gary R. Wheeler20-Jul-02 8:45
memberGary R. Wheeler20-Jul-02 8:45 
You have to be careful here. If you use the CDynLinkLibrary() constructor to link your extension DLL into the DLL user's resource chain, then your DLL 'shares' the resource space with the calling application. In this case, the ID's assigned to your resources in the DLL have to be set to not conflict with those in the application, and the approach outlined in this article is not required.

If you don't use the CDynLinkLibrary(), then it is up to you to ensure within the DLL that every reference within the DLL to a resource is accessed from the correct place using AfxSetResourceHandle().

If this sounds like the voice of experience, it is. I just finished fixing a bug in a product that had been released for over a year related to this problem. I renumbered some resources in a DLL, and all of the sudden a dialog box implemented within the DLL stopped working.

I do like the approach in your article, though.

Gary R. Wheeler

GeneralRe: Extension DLL resource management Pin
Michael Kuelshammer21-Jul-02 1:52
sussMichael Kuelshammer21-Jul-02 1:52 
GeneralRe: Extension DLL resource management Pin
Peredelsky Alexey21-Jul-02 21:26
memberPeredelsky Alexey21-Jul-02 21:26 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 19 Jul 2002
Article Copyright 2002 by Peredelsky Alexey
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid