C++ Argument of type "const wchar_t" * incompatible with parameter of type "wchar_t"

Сan't call "GetProcessByExeName"

DWORD GetProcessByExeName(wchar_t* ExeName)
{
PROCESSENTRY32W pe32;
pe32.dwSize = sizeof(PROCESSENTRY32W);

HANDLE hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
if (hProcessSnap == INVALID_HANDLE_VALUE)
{
    MessageBoxW(NULL, L"Error CreateToolhelp32Snapshot", L"error", MB_OK);
    return false;
}

if (Process32FirstW(hProcessSnap, &pe32))
{
    do
    {
        if (_wcsicmp(pe32.szExeFile, ExeName) == 0)
        {
            CloseHandle(hProcessSnap);
            return pe32.th32ProcessID;
        }
    } while (Process32NextW(hProcessSnap, &pe32));
}

   CloseHandle(hProcessSnap);
   return 0;
}

By calling GetProcessByExeName(L"chrome.exe"); writes -> Argument of type "const wchar_t" * is incompatible with parameter of type "wchar_t"

1 answer

  • answered 2020-08-13 00:17 drescherjm

    You are attempting to pass a wide character string literal which is not modifiable and should be a const wchar_t* to a function that is declared to take a wchar_t* which is not a const. Since you don't want to modify the string in your function you should change the signature of your function from

    DWORD GetProcessByExeName(wchar_t* ExeName)
    

    to

    DWORD GetProcessByExeName(const wchar_t* ExeName)
    

    This question should add some information on why string literals must be const: Why are string literals const?