Click here to Skip to main content
Click here to Skip to main content

How to extract Unicode strings (Japanese) from an MFC edit control

, 20 Oct 2006 CPOL
Rate this:
Please Sign up or sign in to vote.
How to extract any Unicode string from an MFC edit box.

Introduction

This project is a small demo to show how VC++ can be used for localization of existing products.

Windows 2000 is the suggested OS, as it was completely built on Unicode, and though NT was built on Unicode too, using its full capabilities is difficult. Visual Studio and Visual C++ were used to implement this tool.

Japanese language is used as an example to implement the case.

Overview

Before we get our hands dirty, let us understand the distinction between Internationalization and Localization.

Internationalization versus Localization

For the software development process, internationalization means to prepare an application so that the code does not need to be modified each time an additional language is to be displayed. Separate files contain all of the translatable information.

Localization of an application means to change it to be able to handle a particular language and locale. To localize a program that has already been internationalized involves no changes to the source code. Instead, translatable files are sent to the product or a contractor translation agency for modification.

For a system to support multiple languages, we need to:

  • Windows 2000 or XP.
  • Visual Studio and Visual C++.

Setting up Windows 2000 or XP

Step 1: Install the Japanese language pack.

Navigate to Start menu->Control Panel->Regional Options->General. Once in the General tab, check the check box left to the Japanese language from the language settings for the System tab.

Sample screenshot

Step 2: Setting the input locale.

Navigate to Start menu->Control Panel->Regional Options->Input Locale. Under the Installed Input Locales section, click Add. From the Input Locale drop down list, select Japanese, and for the Keyboard Layout/IME drop down box, select Japanese Input System (MS-IME2000).

Sample screenshot

Step 3: Setting the default locale.

In the Control Panel - Regional Options, under the General tab, scroll down the list for Language Settings for the System. Highlight Japanese, and click Set Default. Once done, verify it did not change your Input Locale default input language. Click OK.

Sample screenshot

Setting up Visual C++ for Unicode compilation

Step 1: To Unicode enable a project in VC++: in the VC++ IDE, navigate to Project->Settings->C++, in the Preprocessor definitions section, insert WIN32,_DEBUG,_WINDOWS,_AFXDLL,_UNICODE, as normal MFC uses WIN32,_DEBUG,_WINDOWS,_AFXDLL,_MBCS.

Sample screenshot

Once _UNICODE has been defined for your project, a few steps need to be taken to ensure that string handling is done properly.

The first thing you need to understand is that the use of a char or char* for strings is no longer valid. TCHAR should be used in place of this. However, for binary data processing, use of char, char*, and BYTE (typedefed as an unsigned char) are still OK.

From MSDN:
  • Except for database class member functions, all MFC functions are Unicode-enabled, including CString. CString also provides Unicode/ANSI conversion functions.
  • The run-time library supplies Unicode versions of all string-handling functions. (The run-time library also supplies “portable” versions suitable for Unicode or for MBCS. These are the _tcs macros.)
  • TCHAR.H supplies portable data types and the _T macro for translating literal strings and characters. See Generic-Text Mappings in TCHAR.H.
  • The run-time library provides a wide-character version of main. Use wmain to make your application “Unicode-aware.”

Step 2: Navigate to the Link tab and select Output in the drop down list in the Category section, and specify wWinMainCRTStartup as the entry point for the program, in the Entry-point symbol edit box.

Sample screenshot

Hurry !!! Now, we are all set to build a small application with a dialog box which accepts Japanese language codes and outputs the same text in the output window.

Implementation

// I am trying to collect stones from the ocean of knowledge...
// Today I learnt it how to extract unicode information from
// edit box which is not supported by MFC but taken help from
// win32 SDK function, named ::GetWindowTextW() 
void CUnicode_DialogDlg::OnOK()
{
  // TODO: Add extra validation here
  USES_CONVERSION;
  CWnd *pWnd="GetDlgItem(IDC_EDIT_UNICODE);
  LPWSTR lpString = new WCHAR[MAX_PATH];
  ::GetWindowTextW(pWnd->m_hWnd, lpString, MAX_PATH);
  LPWSTR caption = A2W("Caption");
  HWND hWnd = this->m_hWnd;
  MessageBoxW(lpString, caption, MB_OK );
  // free the string
  delete[] lpString;
  CDialog::OnOK();
}

// ******************************* //

Further reading

Locale

Locale programming

Pros

The Operating System is responsible for loading the language specific resources.

Cons

This project does not demonstrate the conversion of the file dialog and the titles in the application as this is all part of the Windows Common Dialog library.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Chaitanya Seshadri
Web Developer
India India
Completed B.E in Electrical and Electronics
Senior Software Engineer in Thomson Corporation

Comments and Discussions

 
QuestionJapanese characters displayed as ??? Pinmemberlimaye_ketaki1-Mar-11 21:00 
GeneralThanks ! Pinmember2pers14-Sep-09 13:29 
GeneralMy vote of 1 Pinmemberetking007200410-May-09 23:54 
Generalis this code trully working... Pinmemberaravind.sn4-May-09 2:23 
GeneralRe: is this code trully working... Pinmember2pers14-Sep-09 13:34 
GeneralVery good, 5 star ! Pinmemberincludeh1017-Oct-08 6:08 
QuestionHow to Show Japanese String in CWnd? PinmemberJothiMurugeswaran23-Sep-08 22:05 
GeneralGood! Pinmemberyearn_remy23-Oct-06 1:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 20 Oct 2006
Article Copyright 2006 by Chaitanya Seshadri
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid