0

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!

  • `std::string` deals in regular characters, `const char* procname` specifies regular characters, and win32 functions with the `A` suffix explicitly expect regular characters. The only thing I see in the code snippet that could be wide characters is `pe32.szExeFile`, so you should show us more about `pe32`, how it was defined and initialized. – user4581301 Sep 21 '22 at 23:05
  • Technically you should provide a [mre] (MRE), but usually when people make a MRE, they don't bother asking the question. Most of the time they reduced the noise around the mistake while making the MRE enough that they spotted and fixed the bug themselves. Make the MRE early in the question asking process and you often same a lot of time. As an added bonus, if you make a MRE and still have a problem, it's usually a tough problem worth sharing. – user4581301 Sep 21 '22 at 23:08
  • std::wstring something = L"someprocess" so you can call my_pid() with a const wchar_t* argument. [Backgrounder](https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/) – Hans Passant Sep 21 '22 at 23:08

1 Answers1

0

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770