【问题标题】:C++ Argument of type "const wchar_t" * incompatible with parameter of type "wchar_t"“const wchar_t”类型的 C++ 参数 * 与“wchar_t”类型的参数不兼容
【发布时间】:2020-12-02 18:28:27
【问题描述】:

С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;
}

通过调用GetProcessByExeName(L"chrome.exe"); 写入 -> "const wchar_t" 类型的参数 * 与 "wchar_t" 类型的参数不兼容

【问题讨论】:

  • DWORD GetProcessByExeName(wchar_t* ExeName) 更改为DWORD GetProcessByExeName(const wchar_t* ExeName) 字符串文字是一个常量
  • @drescherjm 是的,一切正常,非常感谢 :)

标签: c++ format wchar-t wchar


【解决方案1】:

您正试图将一个不可修改的宽字符串文字传递给一个声明为采用wchar_t* 而非常量的函数,该函数应为const wchar_t*。由于您不想修改函数中的字符串,因此您应该从

更改函数的签名
DWORD GetProcessByExeName(wchar_t* ExeName)

DWORD GetProcessByExeName(const wchar_t* ExeName)

这个问题应该添加一些关于为什么字符串文字必须是 const 的信息:Why are string literals const?

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 2019-10-31
    • 1970-01-01
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    • 2021-08-06
    • 2020-11-19
    相关资源
    最近更新 更多