Creating a Win32 Application on ANSI and UNICODE

I want the Win32 C ++ application to be playable in any encoding version (UNICODE and ANSI). Now I'm a little confused about what exactly is the difference between two (or more?) Encodings?

To make a Win32 application compatible with cross-coding, this means that I have to go through my code and replace each std :: string with std :: wstring, then replace each char with wchar_t * and then replace each literal string (" ) with L ""?

What happens if my application runs on a UNICODE machine and my application contains std :: string?

Do you have any recommendations regarding the steps that I need to take to ensure compatibility of my cross-coding applications? For example: - Change all c_strings and strings to their UNICODE equivalent - Change any Win32 functions to uncide version (for example, change from getenv () to _wgetenv ())

+5
source share
4 answers

What happens if my application runs on a UNICODE machine and my application contains std :: string?

ANSI Unicode, , . Windows, Unicode, Windows 3.11 . ASCII UniCode.

( ?) ?

ASCII?
ASCII , 128 , . . ASCII , .

Unicode?
ASCII - , 256 . , , . , ASCII . Unicode, 65 536 .

Unicode - ISO Unicode , . Unicode 8-, 16- 32- , Unicode , ASCII Latin-1. 256 Unicode -1.

Win32 UNICODE # Unicode _UNICODE. , , Unicode Win32.

- , , - ?

Win32 ( ) : ASCII Unicode. , , UNICODE. Unicode- . :

std::string std::wstring,
char wchar_t*
string("") L""
TCHAR Windows ..

, , , , .

, Unicode .

+5

Windows, Unicode, Windows ME. - Unicode . , , .

. Microsoft CString, CStringA CStringW - , CStringW , CString, . std:: wstring std::string. L"" Microsoft _T(""), .

+3

ANSI Unicode, .

  • API- . , CreateFile(). API CreateFileA(), CreateFileW() (ANSI Wide (.. Unicode)) . ​​NT Unicde API. ANSI API ANSI Unicode API. API- Unicode.
  • T*. TCHAR char ANSI, wchar_t Unicode.

, std::string std::wstring, , API . string vs. wstring , ANSI Unicode.

You can use ATL to easily convert strings as needed.

// assume compiled for Unicode
#include <atlbase.h>

void myfunc() {
   USES_CONVERSION;

   std::string filename = "...";
   HANDLE hFile = CreateFile(A2W(filename.c_str()), ...

or if you want, you can use A2T(), and your code will work if it is compiled for ANSI or Unicode.

+3
source

You can use TCHAR in your case.

In UNICODE, TCHAR is a WCHAR. IN NOT UNICODE, TCHAR CHAR.

If you want to use std :: string, I recommend you the following use.

 #ifdef UNICODE
 #define std::tstring str::wstring
 #else
 #define std::tstring str::string
 #endif

and

Use std :: tstring in your program.

-1
source

All Articles