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?

5
  • 2
    What is the actual error text? Commented Mar 7, 2017 at 17:39
  • error C2665: std::basic_string<_Elem,_Traits,_Alloc>::basic_string: none of the 17 overloads can convert parameters Commented Mar 7, 2017 at 17:41
  • try wstring(process.szExeFile) Commented Mar 7, 2017 at 17:41
  • says it cant do the operation std::wstring == PCSTR Commented Mar 7, 2017 at 17:44
  • @BerNardEr: That's because PCSTR is unrelated to WCHAR[]. You need to understand the difference between 8 and 16 bit characters in Windows. Commented Mar 8, 2017 at 10:46

1 Answer 1

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?).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, i really haven't thought about it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.