[英]Cannot convert from 'TCHAR [260]' to 'std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>
error C2440: 'initializing': 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 >>
I get this error, been stuck on this for a few hours.. 我收到此错误,在此问题上停留了几个小时。
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
TCHAR
is defined as either char
or wchar_t
depending on whether you have the UNICODE
macro defined. 根据是否已定义
UNICODE
宏,将TCHAR
定义为char
或wchar_t
。 In your case, it looks like it is not defined, so you are trying to construct a std::wstring
from a char[]
array. 在您的情况下,它似乎未定义,因此您尝试从
char[]
数组构造std::wstring
。
I would advise you to always use wide APIs on Windows, because that is the only way to get proper Unicode support on there: 我建议您在Windows上始终使用广泛的API,因为那是在此处获得适当Unicode支持的唯一方法:
wchar_t szModName[MAX_PATH];
if (!GetModuleFileNameExW(hProcess, hMods[i], szModName, MAX_PATH))
continue;
wstring modName = szModName;
GetModuleFileNameEx
is not a function, but rather is a macro that expands to either GetModuleFileNameExA
or GetModuleFileNameExW
depending on the UNICODE
macro. GetModuleFileNameEx
不是函数,而是一个根据UNICODE
宏扩展为GetModuleFileNameExA
或GetModuleFileNameExW
的宏。
Originally, the TCHAR
variants were provided to ease transition from old Windows versions without Unicode support to newer NT-based Windows versions with Unicode support. 最初,提供了
TCHAR
变体,以简化从不支持Unicode的旧Windows版本到具有Unicode支持的基于NT的新Windows版本的过渡。 Nowadays, there is no reason to use the ANSI variants at all. 如今,根本没有理由使用ANSI变体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.