Warm tip: This article is reproduced from serverfault.com, please click

'The argument of type WCHAR* is not compatible with "const char*"'

发布于 2015-12-14 21:19:19
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.

Questioner
Leon
Viewed
0
Remy Lebeau 2015-12-15 05:28:52

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().