开发者

How can i suppport both Unicode and Multi-Byte Character Set in my code?

i must say im new to win32 c++ programming so i face a problem that

some code compile in Multi-Byte Character Set and not in Unicode Character Set.

how can my code support both ?

for example this NOT compiles in Multi-byte only in Unicode and the commented vector only in MultiByte:

 //vector<char> str2(FullPathToExe.begin(), FullPathToEx开发者_开发百科e.end());
 vector<wchar_t> str2(FullPathToExe.begin(), FullPathToExe.end());

    str2.push_back('\0');
    if (!CreateProcess(NULL,
                     &str2[0],
                    NULL,
                    NULL,
                    TRUE,
                    0,
                    NULL,
                    NULL,
                    &si,
                    &pi))


Use TCHAR as the character type (e.g. std::vector<TCHAR>), which is:

A WCHAR if UNICODE is defined, a CHAR otherwise.

This type is declared in WinNT.h as follows:

#ifdef UNICODE
   typedef WCHAR TCHAR;
#else
   typedef char TCHAR;
#endif


You don't have to support both, unless your application must support Windows Mobile or desktop version like Windows 95 or older.

If you write for current desktop or server Windows, supporting "Unicode" is enough. Just go for wchar_t!


You could use the macros/typedefs provided by microsoft and add your own, to support both.

TCHAR -> typedef to char/wchar_t
_TEXT() -> creates a text constant either wide or multibyte _TEXT("hallo")

Probably usefull to add so you could use a String class instead of a vector for text manipulations:

#ifdef UNICODE
   typedef std::wstring String;
#else
   typedef std::string String;
#endif


By "new to Win32 C++ programming", I'm assume you mean you don't have an existing large program using "ANSI" strings that you need to maintain. If so, then why do you want to build an "ANSI" version? Just do everything with wchar_t.

vector<wchar_t> str2(FullPathToExe.begin(), FullPathToExe.end());

str2.push_back(L'\0');      // Note the prefix.
if (!CreateProcessW(NULL,   // Note the W; explicit is better than implicit.
                    &str2[0],
                    NULL,
                    NULL,
                    TRUE,
                    0,
                    NULL,
                    NULL,
                    &si,
                    &pi))

If you need to work with multi-byte strings (e.g., for reading files, or for working with third-party libraries that use char rather than wchar_t), then convert them using WideCharToMultiByte and MultiByteToWideChar.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜