How to Convert char* to LPWSTR in VC++?

2019-08-27 00:01发布

问题:

How to Convert char* to LPWSTR in VC++ ?

LPNETRESOURCEW nr = NULL;
memset(&nr, 0, sizeof (NETRESOURCE));
nr->lpLocalName = strDriveLetter.GetBuffer(strDriveLetter.GetLength()); // this line giving me error "Cannot Convert char* to LPWSTR"

Any help is appreciated. Thanks.

回答1:

Use MultiByteToWideChar function;

const char* msg = "foobarbaz";
int len = strlen(msg) + 1;
wchar_t *w_msg = new wchar_t[len];
memset(w_msg, 0, len);
MultiByteToWideChar(CP_ACP, NULL, msg, -1, w_msg, len);


回答2:

memset(&nr, 0, sizeof (NETRESOURCE)); here nr is a NULL pointer. This is not correct. You should have nr point to a valid memory first by either using explicit allocation like new or on allocate on stack.