简体   繁体   中英

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

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. In your case, it looks like it is not defined, so you are trying to construct a std::wstring from a char[] array.

I would advise you to always use wide APIs on Windows, because that is the only way to get proper Unicode support on there:

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.

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. Nowadays, there is no reason to use the ANSI variants at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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