GetModuleHandleEx usage example. WTL internationalisation

本秂侑毒 提交于 2020-01-05 04:29:07

问题


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:


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.



来源:https://stackoverflow.com/questions/18365924/getmodulehandleex-usage-example-wtl-internationalisation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!