Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert char* to LPCWSTR?

I know this has already been discussed in several questions on SO, but none of those solutions have worked for me.

I start with a char* because this is for a DLL that will be called from VBA, and char* is necessary for VBA to pass a string to the DLL.

I need to return a LPCWSTR because that's the input parameter for the API function I'm trying to call, and I can't enable casting by switching from Unicode to multi-byte character set in the Properties window, because the API has this code:

#if !defined(UNICODE) && !defined(NOUNICODE)
#error UNICODE is not defined. UNICODE must be defined for correct API arguments.
#endif

I tried this:

LPCWSTR convertCharArrayToLPCWSTR(char* charArray)
    {
        const char* cs=charArray;
        wchar_t filename[4096] = {0};
        MultiByteToWideChar(0, 0, cs[1], strlen(cs[1]), filename, strlen(cs[1]));
    }

which gave these errors:

error C2664: 'strlen' : cannot convert parameter 1 from 'const char' to 'const char *'
error C2664: 'MultiByteToWideChar' : cannot convert parameter 3 from 'const char' to 'LPCCH'

I tried this (same function header), loosely adapted from this post:

size_t retVal;
const char * cs = charArray;    
size_t length=strlen(cs);
wchar_t * buf = new wchar_t[length]();  // value-initialize to 0 (see below)
size_t wn = mbsrtowcs_s(&retVal,buf,20, &cs, length + 1, NULL);
return buf;

This compiled ok, but when I passed it an example string of "xyz.xlsx", mbsrtowcs_s() set buf to an empty string: L""

So, how do I make this conversion?

like image 307
sigil Avatar asked Oct 31 '13 19:10

sigil


2 Answers

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.

like image 159
sigil Avatar answered Oct 04 '22 12:10

sigil


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.

like image 24
cHao Avatar answered Oct 04 '22 10:10

cHao