Looks like I am getting the same 2 errors on one line of code. Can you help me? What am I doing wrong?
auto get_proc_base = [&](std::wstring moduleName) {
MODULEENTRY32 entry = { };
entry.dwSize = sizeof(MODULEENTRY32);
std::uintptr_t result = 0;
const auto snapShot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, r6_pid);
while (Module32Next(snapShot, &entry)) {
if (std::wstring(entry.szModule) == moduleName) {
result = reinterpret_cast<std::uintptr_t>(entry.modBaseAddr);
break;
}
}
if (snapShot)
CloseHandle(snapShot);
return result;
};
No instance of constructor "std::basic_string<_Elem, _Traits, _Alloc>::basic_string [with _Elem=wchar_t, _Traits=std::char_traits<wchar_t>, _Alloc=std::allocator<wchar_t>]" matches the argument list
'<function-style-cast>': cannot convert from 'char [256]' to 'std::wstring'
CodePudding user response:
You can't construct a std::wstring
from a char[]
array, as you are trying to do. std::wstring
does not have a constructor for that purpose. You would have to convert the char
data to a wchar_t[]
array using MultiByteToWideChar()
or equivalent, or to a std::wstring
using std::wstring_convert::from_bytes()
, or any other Unicode API/library conversion of your choosing (iconv, ICU, etc).
However, you don't actually need to do that in this case. Since your lambda is taking a std::wstring
as input, just use the Unicode-specific version of the Module32 API functions instead, ie using the MODULEENTRY32W
struct with Module32FirstW()
/Module32NextW()
.
Also, you do need to call Module32First/W()
before you can then call Module32Next/W()
.
Try this instead:
auto get_proc_base = [&](const std::wstring &moduleName) {
MODULEENTRY32W entry = { };
entry.dwSize = sizeof(entry);
std::uintptr_t result = 0;
const auto snapShot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, r6_pid);
if (snapShot)
if (Module32FirstW(snapShot, &entry)) {
do {
if (moduleName == entry.szModule) { // alternatively: if (moduleName.compare(entry.szModule) == 0) {
result = reinterpret_cast<std::uintptr_t>(entry.modBaseAddr);
break;
}
}
while (Module32NextW(snapShot, &entry));
}
CloseHandle(snapShot);
}
return result;
};