Localizing Application Resources with Foundation Classes
  
PSS ID Number: Q100390
Article last modified on 10-12-1995
 
1.00 1.50 1.51 1.52 | 1.00
 
WINDOWS             | WINDOWS NT
 

---------------------------------------------------------------------
The information in this article applies to:
 
 - The Microsoft Foundation Classes (MFC), included with:
 
    - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, and 1.52
    - Microsoft Visual C++, 32-bit Edition, version 1.0
      on the following platform: x86
---------------------------------------------------------------------
 
SUMMARY
=======
 
Microsoft Foundation Class Library Technical Note 23 explains how to
customize standard framework resources, such as the buttons on a print
preview dialog bar. However, this technical note does not provide a
good solution for a developers producing an application localized into
more than one language, and there is a problem with the method
described.
 
When localizing an application for several languages, it is most
convenient to place all of the application's resources into a
dynamic-link library (DLL). To localize the application into a new
language, clone the DLL translate the resources into the appropriate
language. Then, after appropriate testing, you can ship the
application code with the appropriate language-specific DLL to address
each market.
 
The remainder of this article outlines the general steps required to
localize an application by moving the resources into a DLL.
 
MORE INFORMATION
================
 
To place all application resources into a DLL project, open the .RC
file in App Studio. Choose Save As from the File menu to store the
resources into another directory (created for this purpose). Then,
open the original .RC file 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-Time Directives list and choose OK.
 
Use the Visual Workbench to create a new Windows DLL project for the
resources. Set the project type to Windows and remove the selection
from the "Use Microsoft Foundation Classes" checkbox. Add the .RC file
to the project. Then, create the following files in your DLL directory
and add them to the project.
 
MYLOCAL.CPP
-----------
 
   #include <windows.h>
 
   int CALLBACK LibMain(HINSTANCE hinst, WORD wDataSeg, WORD cbHeap,
         LPSTR lpszCmdLine )
   {
      return 1;
   }
 
MYLOCAL.DEF
-----------
 
   LIBRARY      MYLOCAL
   DESCRIPTION  'Localized resources for MFC application'
   EXETYPE      WINDOWS
 
   CODE         PRELOAD MOVEABLE DISCARDABLE
   DATA         PRELOAD MOVEABLE SINGLE
 
   SEGMENTS
      WEP_TEXT FIXED PRELOAD
 
   EXPORTS
      WEP @1 RESIDENTNAME ; required WEP entry point
                          ; (uses library WEP routine)
 
Copy the standard framework resources into the .RC file for your DLL
so you can modify them. Technical Note 23 outlines the procedure to do
this. Copy all resources from AFXPRINT.RC and AFXRES.RC into the .RC
file for your DLL by selecting the resources in each source file. Then
press and hold the CTRL 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
steps to work around these problems before compiling your DLL:
 
NOTE: In version 2.5, 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
 
   Unfortunately, however, 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 (you may need to
   change an option in the Editor Options dialog box in Visual
   Workbench) and add the vertical bar 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
version information for the application. Edit this information as
appropriate for your 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
the following 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();
      }
 
Once you establish that the application works as expected, you can use
App Studio translate the resources in the DLL project and make
additional copies of the project to localize for other languages.
 
For more information on this procedure, see the Microsoft Foundation
Class Library Technical Notes. The Visual C++ 1.0 for Windows setup
program creates an MFC Tech Notes icon in the Visual C++ 1.0 group.
 
Additional reference words: kbinf 1.00 1.50 2.00 2.50 2.51 2.52 2.10
KBCategory: kbprg
KBSubcategory: MfcDLL
=============================================================================
Copyright Microsoft Corporation 1995.
