简体   繁体   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>>

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定义为charwchar_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宏扩展为GetModuleFileNameExAGetModuleFileNameExW的宏。

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.

相关问题 没有合适的构造函数来将“ const char [8]”转换为“ std :: basic_string” <wchar_t, std::char_traits<wchar_t> ,std :: allocator <wchar_t> &gt;” - no suitable constructor exists to convert from “const char [8]” to “std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>>” 转换std :: basic_string <wchar_t> 和std :: basic_string <uint16_t> - Convert std::basic_string<wchar_t> and std::basic_string<uint16_t> 将const wchar_t *转换为const std :: basic_string的更好方法<char_t> - Better way to convert const wchar_t * to const std::basic_string<char_t> 如何将 std::string 转换为 wchar_t* - How to convert std::string to wchar_t* C ++错误(将wstring转换为字符串):无法将参数1从std :: wchar_t转换为std :: char - C++ error(converting wstring to string): cannot convert argument 1 from std::wchar_t to std::char boost :: filesystem :: path :: native()返回std :: basic_string <wchar_t> 而不是std :: basic_string <char> - boost::filesystem::path::native() returns std::basic_string<wchar_t> instead of std::basic_string<char> 将std :: string转换为wchar_t *的typedef - Converting std::string to a typedef of wchar_t* wchar_t 到 std::string 以十六进制格式 - wchar_t to std::string in hex format 从&#39;std :: wstring {aka std :: basic_string <wchar_t> }&#39;转换为非标量类型&#39;UString {aka std :: basic_string <char> }&#39; - conversion from ‘std::wstring {aka std::basic_string<wchar_t>}’ to non-scalar type ‘UString {aka std::basic_string<char>}’ 从std :: vector转换 <char> 到wchar_t * - conversion from std::vector<char> to wchar_t*
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM