Tlhelp32 and std::string when using unicode on msvc

Viewed 30

I am trying to write my first project ever in c++ and have come across this error:

std::string something = "someprocess"
my_pid(something.c_str());


DWORD my_pid(const char* procname) {
   if (lstrcmpiA(procname, pe32.szExeFile) == 0)

Error: "WCHAR *" is incompatible with parameter of type "LPCSTR" in c++

This is referenced and answered here:

argument of type "WCHAR *" is incompatible with parameter of type "LPCSTR" in c++

Unfortunately, while I understand the answer in the above question in a abstract way, I cannot figure out how to solve it, leaving me no choice but to hope someone could provide a small code example to help wth my learning.

I have tried doing conversions from multibytetowchar and wchartomutibyte (referenced here:)

How do I use MultiByteToWideChar?

This has left me with the error:

cannot convert argument 1 from 'const _Elem *' to 'const int'
    with
    [
        _Elem=char
    ]
 main.cpp(81): note: There is no context in which this conversion is possible

I am a complete beginner with C++ and after reading through other posts, I am still lost so please no high level answers. I am not proficient enought to understand them (yet!).

Thanks!

1 Answers

I cannot figure out how to solve it

You can either:

  • use PROCESSENTRY32A with Process32FirstA()/Process32NextA(), so that you can continue using std::string/const char* as-is.

  • use std::wstring/const wchar_t* instead.

Related