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

2019-08-26 23:25发布

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.

2条回答
疯言疯语
2楼-- · 2019-08-26 23:56

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);
查看更多
小情绪 Triste *
3楼-- · 2019-08-27 00:00

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.

查看更多
登录 后发表回答