Click here to Skip to main content
Email Password   helpLost your password?

Introduction

I have created this class to help me load a different resource DLL if it is present during runtime. An example would be a multi-language application. If another resource DLL (with a name specified at compile time) is present, it can load and use it as the resource for the application.

Let's look at the class header. It is quite simple and I think the comments describe well about the individual methods.

class CResourceManager  
{
// Operations

public:

    // Constructor

    CResourceManager();

    // Destructor

    virtual ~CResourceManager();

    // Load external resource dll. If file not present or unable to load,

    // it returns FALSE. If it is present, it is loaded but does not affect

    // the default resource of the application. You should call this if you

    // want to manage the current application resource and an external one.

    BOOL Setup(CString);

    // Load external resource dll and set it as default for application.

    // Before setting as default, the current resource handle is stored

    // in case when you need to access the resource there.

    // If file not present or unable to load, the current application

    // resource is being used. 

    BOOL SetupEx(CString);

    // Get string from string table of external resource if any

    // If there is an external resource loaded, GetString will retrieve from

    // there. If not, it will retrieve from the application default resource.

    CString GetString(UINT);

// Attributes

private:

    HINSTANCE m_hExternalResource; // Handle to external resource loaded

    HINSTANCE m_hApplicationResource; // Handle to current application resource

};

Using the code

Below is an example of how you may use the methods of the class. First you need to create an object of CResourceManager. You can put it as global or as a member of your application class. Then, call the Setup(...) method in InitInstance(...) as shown below.

BOOL CtestApp::InitInstance()
{
    // InitCommonControls() is required on Windows XP if an application

    // manifest specifies use of ComCtl32.dll version 6 or later to enable

    // visual styles.  Otherwise, any window creation will fail.

    InitCommonControls();

    CWinApp::InitInstance();

    // Initialize OLE libraries

    if (!AfxOleInit())
    {
        AfxMessageBox(IDP_OLE_INIT_FAILED);
        return FALSE;
    }
    AfxEnableControlContainer();



    // Load and set the specified resource dll as default if it is present.

    // Normally the location of the dll would be the same as the executable

    // and you would need to determine it with GetModuleFileName(...), but

    // in this case, i assume i know where the dll is located.

    m_oResourceManager.SetupEx(_T("C:\\MyProjects\\anotherres.dll"));

    .
    .
    .
    
    // The one and only window has been initialized, so show and update it

    m_pMainWnd->ShowWindow(SW_SHOW);
    m_pMainWnd->UpdateWindow();
    // call DragAcceptFiles only if there's a suffix

    //  In an SDI app, this should occur after ProcessShellCommand

    return TRUE;
}

As for cleaning up the loaded resource DLL, the destructor will handle it as shown below.

CResourceManager::~CResourceManager()
{
    // Check if handle is valid, free library

    if (m_hExternalResource != NULL)
        FreeLibrary(m_hExternalResource);

    // Init handles

    m_hExternalResource = NULL;
    m_hApplicationResource = NULL;
}

Points of interest

I admit that this class do fall short in terms of the other resource problems that could be addressed, but feel free to extend it if you want. :)

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralAre you the one!!!!!111
saajan
3:30 18 May '04  
Are you the one who studied in JNV Idukki,
Any way the resource manger seemed not to work in different languages .Do you have any idea of language translation in Czech fully? thats some chars are not shown by the editor and the dialog?
GeneralRe: Are you the one!!!!!111
Weiye Chen
6:05 20 May '04  
saajan wrote: Are you the one who studied in JNV Idukki,
Nope, i don't think so.

saajan wrote: Any way the resource manger seemed not to work in different languages .Do you have any idea of language translation in Czech fully? thats some chars are not shown by the editor and the dialog?
I am afraid i only tested this in English.Frown I am not too sure about any Czech translation software. Perhaps u can try searching the internet.

Weiye Chen When pursuing your dreams, don't forget to enjoy your life...
GeneralPlease provide a demo application.
John M. Drescher
5:31 7 Jun '03  
It would be more helpful if you provided a small demo application to show how your class works. It is not completly clear from the current docs.

John
GeneralBroken link.
WREY
3:21 7 Jun '03  
Cannot download file.

William

Fortes in fide et opere!
GeneralRe: Broken link.
Weiye Chen
4:15 7 Jun '03  
Sorry, it is fixed.

Weiye, Chen
When pursuing your dreams, don't forget to enjoy your life...
GeneralSomething handy to have around.
WREY
5:00 7 Jun '03  
This sample may look trivial, but with some creativity, the extensibility of it could prove enormously handy to have around.

Immediately what comes to mind, is customizing it with an array of pointers to strings (representing libraries) that can be dynamically activated in runtime mode.

Thanks for sharing it with the community. Good job!

Cool

William

Fortes in fide et opere!
GeneralRe: Something handy to have around.
Weiye Chen
5:25 7 Jun '03  
WREY wrote: Thanks for sharing it with the community
No problem Wink , and thank you for your suggestion.

Weiye, Chen
When pursuing your dreams, don't forget to enjoy your life...
GeneralRe: Something handy to have around.
Anton Heidelwalder
5:54 28 Oct '04  
This was exact what i was looking for the whole day. It is great. Thanks for sharing it to us!

William: Do You suggest to extend this example to an application that can dynamically switch languages at every time during runtime? Is it possible to exchange the resources during runtime, or can the resources only be loaded before the first call of ShowWindow()? In that point I am like a newbie...:
thanks!
GeneralRe: Something handy to have around.
WREY
8:02 28 Oct '04  
I don't know what you mean by "languages", whether you're referring to C++, VB, or Fortran, (etc.), or whether you're talking about English, French, or Russian, (etc.). In any case, I haven't tried it with either.

As far as switching resources (meaning, resource libraries) during runtime, the answer is "yes", that's very doable. Just make sure the library is already loaded (and that's it's the correct one you want) before you start using it. That might sound trivial, but I've seen so many people (including myself) get tripped-up because of failure to load the correct DLL library (even though sometimes it's not your fault).

A word of caution!!! Do NOT start getting happy-crazy by switching DLL's all over the place. It can become very expensive doing so, not to mention opening up yourself for bad things to happen.

The author did a pretty good job at explaining what he set out to accomplish, and some instructions on how to use his class. It is not difficult, and once you get it to work the first time, you'll see how easy and handy it is to have around.

I wish you much success!!

Smile

William

Fortes in fide et opere!


Last Updated 6 Jun 2003 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010