没有重载函数“”的实例匹配参数列表错误

时间:2016-12-06 00:04:53

标签: c++ winapi

我在我的cpp中遇到以下错误,我不太确定如何修复它们(我是C ++的新手)

错误:

Severity    Code    Description Project File    Line    Suppression State
Error (active)      no instance of overloaded function "std::basic_string<_Elem, _Traits, _Alloc>::compare [with _Elem=char, _Traits=std::char_traits<char>, _Alloc=std::allocator<char>]" matches the argument list    KernelHop   c:\Users\Root\Downloads\KernelHop-master (1)\KernelHop-master\KernelHop\KernelHop.cpp   102 


Severity    Code    Description Project File    Line    Suppression State
Error (active)      no instance of overloaded function "std::basic_string<_Elem, _Traits, _Alloc>::compare [with _Elem=char, _Traits=std::char_traits<char>, _Alloc=std::allocator<char>]" matches the argument list    KernelHop   c:\Users\Root\Downloads\KernelHop-master (1)\KernelHop-master\KernelHop\KernelHop.cpp   110 

我的代码:

DWORD FindProcessId(const std::string processName)
{
    PROCESSENTRY32 processInfo;
    processInfo.dwSize = sizeof(processInfo);

    HANDLE processSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
    if (processSnapshot == INVALID_HANDLE_VALUE)
        return 0;

    Process32First(processSnapshot, &processInfo);
    if (!processName.compare(processInfo.szExeFile))
    {
        CloseHandle(processSnapshot);
        return processInfo.th32ProcessID;
    }

    while (Process32Next(processSnapshot, &processInfo))
    {
        if (!processName.compare(processInfo.szExeFile))
        {
            CloseHandle(processSnapshot);
            return processInfo.th32ProcessID;
        }
    }

    CloseHandle(processSnapshot);
    return 0;
}

任何人都知道我做错了什么?

1 个答案:

答案 0 :(得分:1)

您可能正在编译为Unicode,在这种情况下PROCESSENTRY32::szExeFile将是一个宽字符串(wchar_t[]),但std::string::compare()需要一个字节字符串(char[])。因此,请尝试使用std::wstring