0

So, I'm using Visual Studio 2012 with project settings set to "use unicode".

I have this included to my file:

#include <string>
using namespace std;

And when i try to do this

 //process.szExeFile - WCHAR[260]
 //name - PCSTR
if (string(process.szExeFile) == string(name))

Visual studio throws an error C2665.

What am I doing wrong?

1 Answers1

3

What am I doing wrong?

When the project is set to "use unicode", the process.szExeFile field is of type WCHAR[]. The std::string class does not provide a constructor which accepts WCHAR[] (or wchar_t*) as input.

You are comparing a name variable as a non-Unicode string, so I assume you don't care about non-ASCII characters. If that is true, you may do this:

std::wstring exeStr(process.szExeFile);
std::string exeStrA(exeStr.begin(), exeStr.end());
if (exeStrA == string(name))

If you care about non-ASCII characters, you should do it the other way around, converting your name string to Unicode, for example using wsctombs() (you can find an example here: How do I convert a string to a wstring using the value of the string?).

Community
  • 1
  • 1
marcinj
  • 48,511
  • 9
  • 79
  • 100