3

We have moved all our strings to resources (and satellite DLLs) for an MFC application. Right now the primary language is incorporated into the EXE itself so when I call LoadString() I can just pass in the module handle of the exe.

However, I need to make this generic - how do I get the module handle in a generic way and make sure I load strings form a satellite DLL if appropriate? We need to get the appropriate module for the currently loaded resource DLL. (or the exe if English)

The ::LoadString() method takes a handle as its first argument - and we're just using the current exe's handle.

Do I have to determine if I need to load the DLL, or does Windows automatically do that for me. It is not clear from the docs I have read.

This indicates that MFC does it automatically. SO how do I get that hmodule?

Tim
  • 20,184
  • 24
  • 117
  • 214

3 Answers3

5

After you've loaded the resources dll with LoadLibrary, you store its HMODULE (returned by LoadLibrary) and pass it to the LoadString function (as well as to the other resource functions).

By the way, if you use your resources DLLs exclusively to store resources (i.e. no code is included in them) you can load them with LoadLibraryEx with the LOAD_LIBRARY_AS_DATAFILE option, making the loading a bit faster and avoiding possible exploits due to malicious code embedded in resources dlls (but in this case be careful with dialogs).

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • I thought that the exe automatically loaded the satellite DLLs based on the locale? – Tim Mar 17 '11 at 14:48
  • AFAIK the automatic language choice is when you have the same resource for multiple languages in the same module. – Matteo Italia Mar 17 '11 at 15:05
  • THis is what is causing my confusion: http://msdn.microsoft.com/en-us/library/8fkteez0%28vs.71%29.aspx – Tim Mar 17 '11 at 15:08
  • @Tim: that's MFC-specific stuff, Win32 does not provide automatically anything like that. – Matteo Italia Mar 17 '11 at 15:09
  • 1
    @Tim: then you should have added the MFC tag, and I wouldn't have replied since I know nothing about MFC. `:)` – Matteo Italia Mar 17 '11 at 15:10
  • I was wrong anyway - we use MFC classes, but it is not an MFC app. I was confused because I was reading what I thought were conflicting ideas - manual loading or automagic loading. Thanks for the help – Tim Mar 17 '11 at 15:15
1

Are you loading the libray with LoadLibrary(Ex)? Remember the handle it returns.

Otherwise use GetModuleHandle("Name of resource module").

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • No - I am not loading the dll myself - I thought I read that the "correct" satellite dll gets loaded automatically by the OS. – Tim Mar 17 '11 at 14:48
1

Use AfxGetResourceHandle().

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536