'The argument of type WCHAR* is not compatible

2020-03-07 06:53发布

问题:

DWORD ProcMem::Module(LPSTR ModuleName){


HANDLE hModule = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);
MODULEENTRY32 mEntry;
mEntry.dwSize = sizeof(mEntry); 

do
    if (!strcmp(mEntry.szModule, ModuleName))
    {
    CloseHandle(hModule);
    return (DWORD)mEntry.modBaseAddr;
    }
while (Module32Next(hModule, &mEntry));

cout << "\nMODULE: Process Platform Invalid\n";
return 0;
 }

the argument of type WCHAR* is not compatible with "const char*"`. while holding my cursor on mEntry.

回答1:

Your project is compiled with Unicode enabled, so CreateToolhelp32Snapshot() maps to CreateToolhelp32SnapshotW(), PROCESSENTRY32 maps to PROCESSENTRY32W, and Process32Next() maps to Process32NextW(). Thus, ProcEntry.szExeFile field is a WCHAR[] array.

You are passing szExeFile to strcmp(), which does not support wchar_t* strings, only char* strings. You need to either:

  1. use WideCharToMultiByte() to convert szExeFile to a char[] array so you can then pass that to strcmp().

  2. change your ProcessName parameter to wchar_t*, or use MultiByteToWideChar() to convert ProcessName to a wchar_t[] array, and pass that to wcscmp() or lstrcmpW() instead of strcmp().

  3. if you want to continue using TCHAR-based APIs, change your ProcessName parameter to LPTSTR and then use _tcscmp() or lstrcmp() instead of strcmp().



标签: c++ memory