Click here to Skip to main content
Licence 
First Posted 18 Jul 2002
Views 82,894
Bookmarked 33 times

DllInstanceSwitcher class for switching to MFC Extension DLL resources

By | 18 Jul 2002 | Article
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

About the Author

Peredelsky Alexey

Web Developer

Russian Federation Russian Federation

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalhelp Pinmemberguyuewuhua19:43 24 Sep '09  
GeneralMy vote of 1 PinmemberKarstenK20:54 23 Nov '08  
Questionshort example code? PinmemberAnton Heidelwalder3:13 9 Nov '04  
AnswerRe: short example code? Pinmemberdickey wong9:03 29 Aug '05  
GeneralHi, Problems with MFC in a DLL PinmemberBeginnerOnline0:23 4 Dec '02  
Generalthanks a lot PinsussAnonymous3:27 8 Oct '02  
it's working.;POMG | :OMG: Roll eyes | :rolleyes: Blush | :O Cry | :(( Rose | [Rose]
GeneralCommand ID's PinmemberPeredelsky Alexey2:33 14 Aug '02  
GeneralExtension DLL resource management PinmemberGary R. Wheeler7:45 20 Jul '02  
GeneralRe: Extension DLL resource management PinsussMichael Kuelshammer0:52 21 Jul '02  
GeneralRe: Extension DLL resource management PinmemberPeredelsky Alexey20:26 21 Jul '02  
GeneralExample code needed PinsussMichael Kuelshammer7:16 20 Jul '02  
GeneralRe: Example code needed PinmemberPeredelsky Alexey20:33 21 Jul '02  
GeneralThat's useful! PinmemberPhilip Patrick20:06 19 Jul '02  
GeneralI think it will not work in Win 98 & Me Pinmembermuayyad3:21 19 Jul '02  
GeneralRe: I think it will not work in Win 98 & Me PinmemberCraig Henderson4:01 19 Jul '02  
GeneralRe: I think it will not work in Win 98 & Me PinmemberPeredelsky Alexey4:25 19 Jul '02  
GeneralNice solution PinmemberRoger Allen2:58 19 Jul '02  

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

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

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