Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert WCHAR[260] to std::string

I have gotten a WCHAR[MAX_PATH] from (PROCESSENTRY32) pe32.szExeFile on Windows. The following do not work:

std::string s;
s = pe32.szExeFile; // compile error. cast (const char*) doesnt work either

and

std::string s;
char DefChar = ' ';
WideCharToMultiByte(CP_ACP,0,pe32.szExeFile,-1, ch,260,&DefChar, NULL);
s = pe32.szExeFile;
like image 721
user1334943 Avatar asked Feb 20 '23 18:02

user1334943


2 Answers

For your first example you can just do:

std::wstring s(pe32.szExeFile);

and for second:

char DefChar = ' ';
WideCharToMultiByte(CP_ACP,0,pe32.szExeFile,-1, ch,260,&DefChar, NULL);
std::wstring s(pe32.szExeFile);

as std::wstring has a char* ctor

like image 73
EdChum Avatar answered Mar 03 '23 17:03

EdChum


Your call to WideCharToMultiByte looks correct, provided ch is a sufficiently large buffer. After than, however, you want to assign the buffer (ch) to the string (or use it to construct a string), not pe32.szExeFile.

like image 29
James Kanze Avatar answered Mar 03 '23 17:03

James Kanze