繁体   English   中英

无法从“ 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>>

错误C2440:“正在初始化”:无法从“ TCHAR [260]”转换为“ std :: basic_string <wchar_t,std :: char_traits <wchar_t>,std :: allocator <wchar_t >>

我收到此错误,在此问题上停留了几个小时。

TCHAR szModName[MAX_PATH];
if (!GetModuleFileNameEx(hProcess, hMods[i], szModName, sizeof(szModName) / sizeof(TCHAR))) // Get the full path to the module's file
    continue;

wstring modName = szModName; <-------- this is what im having problem with

根据是否已定义UNICODE宏,将TCHAR定义为charwchar_t 在您的情况下,它似乎未定义,因此您尝试从char[]数组构造std::wstring

我建议您在Windows上始终使用广泛的API,因为那是在此处获得适当Unicode支持的唯一方法:

wchar_t szModName[MAX_PATH];
if (!GetModuleFileNameExW(hProcess, hMods[i], szModName, MAX_PATH))
    continue;

wstring modName = szModName;

GetModuleFileNameEx不是函数,而是一个根据UNICODE宏扩展为GetModuleFileNameExAGetModuleFileNameExW的宏。

最初,提供了TCHAR变体,以简化从不支持Unicode的旧Windows版本到具有Unicode支持的基于NT的新Windows版本的过渡。 如今,根本没有理由使用ANSI变体。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM