2011-05-14 59 views
2

我必须说im新的win32 c + +编程,所以我面临一个问题,
一些代码编译多字节字符集,而不是Unicode字符集。
我的代码如何同时支持?
例如此NOT编译在多字节只有在Unicode和注释向量仅在多字节:我如何在我的代码中支持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)) 

回答

6

使用TCHAR作为字符类型(例如std::vector<TCHAR>),它是:

甲如果UNICODE被定义,则为WCHAR,否则为CHAR

此类型在WINNT.H声明 如下:

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

在向量中使用它的地方? – user63898 2011-05-14 05:15:30

+0

@user:'std :: vector '。 – 2011-05-14 05:16:34

0

你可以使用微软提供的宏/ typedef和添加自己的,同时支持。

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 
+0

为什么不只是'typedef std :: basic_string String;'? – 2011-05-14 05:56:40

+0

@Georg这个更短。太好了! – 2011-05-14 06:36:53

4

你不必支持,除非你的应用程序必须支持Windows Mobile或像Windows 95或更旧的桌面版本。

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

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而不是wchar_t第三方库的工作),然后使用WideCharToMultiByteMultiByteToWideChar它们转换。

相关问题