简体   繁体   English

C ++如何将wchar_t *转换为TCHAR [](不是TCHAR *)

[英]C++ How can I convert wchar_t* to TCHAR [] (not TCHAR*)

I wants to add a small additional options to a big unit, so I do not want to process a large amount of code. 我想在一个大单元中添加一些小的附加选项,所以我不想处理大量代码。

TCHAR szTempFileName[MAX_PATH];
TCHAR lpTempPathBuffer[MAX_PATH];
int uRetVal = 0;
GetTempPath(MAX_PATH,          // length of the buffer
    lpTempPathBuffer); // buffer for path 
GetTempFileName(lpTempPathBuffer, // directory for tmp files
    TEXT("QRCode_"),     // temp file name prefix 
    0,                // create unique name 
    szTempFileName);  // buffer for name 

I want to change szTempFileName to optional wstring/std::string/wchar_t* parametr . 我想将szTempFileName更改为可选的wstring / std :: string / wchar_t * parametr。

Solution: 解:

  1. change TCHAR to wchar_t 将TCHAR更改为wchar_t

  2. wcscpy(wchar_t[], wchat_t*); wcscpy(wchar_t [],wchat_t *);

Typically, there's no reason to use TCHAR s at all. 通常,根本没有理由使用TCHAR If you're working with wchar_t anyway, simply call the Unicode variants of the Winapi functions directly by adding a W suffix: 如果仍然使用wchar_t ,只需添加一个W后缀即可直接调用Winapi函数的Unicode变体

// Use wchar_t instead of TCHAR.
wchar_t szTempFileName[MAX_PATH];
wchar_t lpTempPathBuffer[MAX_PATH];
int uRetVal = 0;
// Call GetTempPathW directly.
GetTempPathW(MAX_PATH, lpTempPathBuffer);
// Use L"..." instead of TEXT("...").
GetTempFileNameW(lpTempPathBuffer, L"QRCode_", 0, szTempFileName);

Stick with what you have and convert to the appropriate string type at the end. 坚持使用现有内容,最后将其转换为适当的字符串类型。 Getting windows to play nicely and correctly with the C++ string types will probably turn your hair grey. 使Windows在C ++字符串类型中正确正确地播放可能会使您的头发变灰。

Use the following typedef to create your string type that you can use throughout your code instead of std::string or std::wstring: 使用以下typedef创建可在整个代码中使用的字符串类型,而不是std :: string或std :: wstring:

typedef std::basic_string<TCHAR> tstring;

Construct your string from the buffer directly: 直接从缓冲区构造字符串:

tstring RealFileName(szTempFileName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 连接一个wchar_t和TCHAR - Concat a wchar_t and TCHAR 使用TCHAR的错误,无法转换为wchar_t - Errors using TCHAR,cannot convert to wchar_t 无法从&#39;const wchar_t *&#39;转换为&#39;_TCHAR *&#39; - Cannot convert from 'const wchar_t *' to '_TCHAR *' 错误C2664:&#39;布尔Strless :: operator()(const TCHAR *&,const TCHAR *&)const&#39;:无法将参数1从&#39;wchar_t * const&#39;转换为&#39;const TCHAR *&&#39; - error C2664: 'bool Strless::operator ()(const TCHAR *&,const TCHAR *&) const' : cannot convert parameter 1 from 'wchar_t *const ' to 'const TCHAR *&' 如何在 C++ 中将 CString 转换为 WCHAR* 并将 std::string 转换为 WCHAR*,而不是 wchar_t* - How can I convert CString to WCHAR* and std::string to WCHAR* in C++, not wchar_t* 无法从“ TCHAR [260]”转换为“ std :: basic_string” <wchar_t,std::char_traits<wchar_t> ,性病::分配器 <wchar_t> &gt; - Cannot convert from 'TCHAR [260]' to 'std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>> 如何将TCHAR转换为双视觉C ++ - How to convert TCHAR to double visual C++ 启动一个新的Windows应用程序:我应该使用_TCHAR或wchar_t作为文本吗? - Starting a new Windows app: Should I use _TCHAR or wchar_t for text? C ++将字符串转换为TCHAR * - C++ Convert string to TCHAR* 如何在C ++中打印tchar - how to print tchar in c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM