GetModuleHandleEx usage example. WTL internationalisation

3.3k views Asked by At

I am trying to do internationalization in a WTL GUI application .. in my drop down selection change handler (which is used for language selection I do something like this):

int selected = (int)::SendMessage(m_cbLang, CB_GETCURSEL,0,0);
HMODULE hmod;
int retCode = 0;
switch(selected)
{
case 0:
    retCode =::GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN, NULL, &hmod);
    ATL::_AtlBaseModule.SetResourceInstance(hmod);
    break;
case 1:

    retCode =::GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_PIN, L"GuiLibOther.dll", &hmod);
    ATL::_AtlBaseModule.SetResourceInstance(hmod);
    break;
}
return S_OK;

Now, I really don't know how to use this function, although it is here , I don t know what the lpModuleName represents. The "GuiLibOther.dll" is a dll which contains the entire interface in another language.. all resources translated to another language.. I want the interface to change the language imediatelly after another language is selected. is this the right way? Case 0 return hmod = NULL

1

There are 1 answers

6
Mark Ransom On BEST ANSWER

First of all you don't want to use the GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS flag unless you're passing the address of some item in the DLL, which in this case you're not.

Second the documentation implies that the DLL must already be loaded before you call GetModuleHandleEx. If you haven't linked it in to your .exe so that it's automatically loaded, you must use LoadLibrary.

The need to use LoadLibrary suggests a simplification:

static HMODULE hmodExe = INVALID_HANDLE;
static HMODULE hmodDLL1 = INVALID_HANDLE;
switch(selected)
{
case 0:
    if (hmodExe == INVALID_HANDLE)
        retCode =::GetModuleHandleEx(0, NULL, &hmodExe);
    ATL::_AtlBaseModule.SetResourceInstance(hmodExe);
    break;
case 1:
    if (hmodDLL1 == INVALID_HANDLE)
        hmodDLL1 = LoadLibrary(L"GuiLibOther.dll");
    ATL::_AtlBaseModule.SetResourceInstance(hmodDLL1);
    break;

This should let you switch resource libraries dynamically without extra overhead.