How to convert char* to LPCWSTR?
Following Hans Passant's advice regarding pointers to local variables, I worked out this approach, which seems to work well:
wchar_t *convertCharArrayToLPCWSTR(const char* charArray)
{
wchar_t* wString=new wchar_t[4096];
MultiByteToWideChar(CP_ACP, 0, charArray, -1, wString, 4096);
return wString;
}
I'm aware that the use of new
requires memory management, which I perform in the function that calls this one.
Since cs
is a const char*
, cs[1]
is a const char
. C++ won't convert it to a pointer for you, because in most cases that doesn't make sense.
You could instead say &cs[1]
or cs+1
if the intent is to skip the first char. (That's what you're doing when you pass a pointer to the 1th element; in C++, indexes start at 0.) If the intent is to pass the whole string, then just pass cs
.