2

我必须说我是 win32 c++ 编程的新手,所以我面临一个问题,即
某些代码在多字节字符集中而不是在 Unicode 字符集中编译。
我的代码如何支持两者?
例如,这 NOT 仅在 Unicode 中编译为多字节,而注释向量仅在多字节中编译:

 //vector<char> str2(FullPathToExe.begin(), FullPathToExe.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))
4

4 回答 4

6

用作TCHAR字符类型(例如std::vector<TCHAR>),即:

一个WCHARifUNICODE被定义,一个CHAR else 。

此类型在 WinNT.h 中声明如下:

#ifdef UNICODE
   typedef WCHAR TCHAR;
#else
   typedef char TCHAR;
#endif
于 2011-05-14T04:42:24.187 回答
4

您不必同时支持两者,除非您的应用程序必须支持 Windows Mobile 或桌面版本,如 Windows 95 或更早版本。

如果您为当前的桌面或服务器 Windows 编写,支持“Unicode”就足够了。去吧wchar_t

于 2011-05-14T06:21:09.613 回答
0

您可以使用 microsoft 提供的宏/类型定义并添加您自己的,以支持两者。

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

添加可能有用,因此您可以使用 String 类而不是向量进行文本操作:

#ifdef UNICODE
   typedef std::wstring String;
#else
   typedef std::string String;
#endif
于 2011-05-14T05:29:28.550 回答
0

通过“Win32 C++ 编程新手”,我假设您的意思是您没有使用需要维护的“ANSI”字符串的现有大型程序。如果是这样,那您为什么构建“ANSI”版本?只需使用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))

char如果您需要使用多字节字符串(例如,用于读取文件,或用于使用使用而不是的第三方库),则使用andwchar_t转换它们。WideCharToMultiByteMultiByteToWideChar

于 2011-05-14T14:10:45.183 回答