简体   繁体   中英

no suitable constructor exists to convert from “const char [8]” to “std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>>”

First. I want to say, that I am not C++ programmer. The outlook string generates an error in visual studio 2015 with the message that is in the title.

HWND windowHandle = (HWND)FindProcessId("outlook");

The function definition:

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

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

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

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

    return 0;
}

I get an error on the outlook string,

HWND windowHandle = (HWND)FindProcessId(**"outlook"**);

I also put a cast to HWND, but I am not sure if I get any run-time compilation errors.

You got error beacause you are passing ASCII string whereas signature expects wide string.

Try

HWND windowHandle = (HWND)FindProcessId(L"outlook");

Your signature

DWORD FindProcessId(const std::wstring& processName)

mentions that it takes wstring which is wide string. And you are passing "outlook" which is compatible with const string& . Hence, you got compile error.

You are taking a string literal to function as std::wstring & which isn't allowed. Probably, you can fix by taking string as std::wstring const & ,

DWORD FindProcessId(std::wstring const & processName)

Since "outlook" literal is an rvalue, it can't binded to a non-const reference but it is legal to bind it to a const reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM