You are currently offline, waiting for your internet to reconnect

How To Localize Application Resources with Foundation Classes

This article was previously published under Q147149
This article has been archived. It is offered "as is" and will no longer be updated.
Microsoft Foundation Class Library Technical Note 23 explains how tocustomize standard framework resources, such as the buttons on a printpreview dialog bar. However, this technical note does not provide a goodsolution for developers who are producing an application localized intomore than one language, and there is a problem with the method described.

When localizing an application for several languages, it is most convenientto place all of the application's resources into a dynamic link library(DLL). To localize the application into a new language, clone the DLL andtranslate the resources into the appropriate language. After testing, youcan ship the application code with the appropriate language-specific DLL toaddress each market.

The remainder of this article outlines the general steps required tolocalize an application by moving the resources into a DLL.
More information
To place all application resources into a DLL project, open the .rc file inApp Studio. Choose Save As from the File menu to store the resources intoanother directory (created for this purpose). Then open the original .rcfile, and remove all resources visible in the App Studio Resource Browser.Finally, choose Set Includes from the App Studio File menu. Delete the#include statements for Afxprint.rc and for Afxres.rc from the Compile-TimeDirectives list and choose OK.

Use the Visual Workbench to create a new Windows DLL project for theresources. Set the project type to Windows dynamic-link library, and clearthe "Use Microsoft Foundation Classes" check box. Add the .rc file to theproject. Then create the following files in your DLL directory, and addthem to the project.


#include <windows.h>int CALLBACK LibMain(HINSTANCE hinst, WORD wDataSeg, WORD cbHeap,      LPSTR lpszCmdLine ){   return 1;}				


LIBRARY      MYLOCALDESCRIPTION  'Localized resources for MFC application'EXETYPE      WINDOWSCODE         PRELOAD MOVEABLE DISCARDABLEDATA         PRELOAD MOVEABLE SINGLESEGMENTS   WEP_TEXT FIXED PRELOADEXPORTS   WEP @1 RESIDENTNAME ; required WEP entry point                       ; (uses library WEP routine)				
Copy the standard framework resources into the .rc file for your DLL so youcan modify them. Technical Note 23 outlines a procedure to do this. Tocopy all resources from Afxprint.rc and Afxres.rc into the .rc file foryour DLL, select the resources in each source file. Then press and hold theCTRL key and drag the resources into the new .rc file. Edit the Compile-Time Directives list box in App Studio to remove the #include files.

There are two problems with this procedure. Perform the following two stepsto work around these problems before compiling your DLL:

NOTE: In MFC version 2.5 and later, step 1 is no longer necessary.

  1. The AFX_IDC_MAGNIFY cursor, used by print preview, is loaded using the AfxGetInstanceHandle() function rather than the AfxGetResourceHandle() function. This error occurs on line 904 of the Viewprev.cpp file in the Microsoft Foundation Class Library source directory (by default, C:\Msvc\Mfc\Src). If you wish, you can modify the source code and recompile the libraries. However, a reasonable method to work around this problem involves copying the AFX_IDC_MAGNIFY resource into your application's .rc file. Only this resource would be common to all languages.
  2. The Afxprint.rc file (located by default in the C:\Msvc\Mfc\Include directory) declares AFX_IDD_PREVIEW_TOOLBAR, the standard framework resource for the print preview CDialogBar, with the following resource style:
          STYLE WS_CHILD | CBRS_TOP						
    The problem is that the CBRS_TOP style is specific to the Microsoft Foundation Class Library; App Studio does not copy this style when you follow the procedure in Technical Note 23. Use a text editor to open the .rc file as a text file. (Note that you may need to change an option in the Editor Options dialog box in Visual Workbench.) Add the vertical bar (the OR symbol) and CBRS_TOP to the STYLE statement. Otherwise, the print preview dialog bar does not appear.
AppWizard creates the .rc2 file in the DLL Res directory to contain versioninformation for the application. Edit this information as appropriate foryour DLL.

Build the DLL project and copy it into your application's directory.

To use the localized resources in the DLL from your application, add thefollowing code to your application and rebuild it:

  1. Modify the main application class (for example, CMyApp) to add an HINSTANCE member variable to contain the DLL instance handle, as follows:
       HINSTANCE m_hInstDLL;						
  2. In CMyApp::InitInstance(), insert the following code before the first line of code generated by ClassWizard:
       if ((m_hInstDLL = ::LoadLibrary("mylocal.dll")) < HINSTANCE_ERROR)   {      return FALSE; // failed to load the localized resources   }   else   {      AfxSetResourceHandle(m_hInstDLL); // get resources from the DLL   }						
  3. Modify the CMyApp::ExitInstance() function to free the library before the application exits. Modify the function as follows:
       int CMyApp::ExitInstance()   {      FreeLibrary(m_hInstDLL);      return CWinApp::ExitInstance();   }						
WARNING: You might encounter a problem if you follow the advice in step 3.For additional information, please see the following article in theMicrosoft Knowledge Base:
187684BUG: Assert When Calling AfxFreeLibrary from ExitInstance
Once you establish that the application works as expected, you can use AppStudio to translate the resources in the DLL project and make additionalcopies of the project to localize for other languages.

For more information on this procedure, see the Microsoft Foundation ClassLibrary Technical Notes. The Visual C++ 1.0 for Windows setup programcreates an MFC Tech Notes icon in the Visual C++ 1.0 group.

Article ID: 147149 - Last Review: 10/26/2013 03:23:00 - Revision: 4.0

Microsoft Foundation Class Library 4.2

  • kbnosurvey kbarchive kbcode kbhowto kblocalization KB147149