-1

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.

ismail
  • 46,010
  • 9
  • 86
  • 95
Swapnil Gupta
  • 8,751
  • 15
  • 57
  • 75

2 Answers2

2

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);
ismail
  • 46,010
  • 9
  • 86
  • 95
  • question is there a quideline about where to put the * in c/c++ code? you do something different on the first line and the third line of code. If not follow a guideline is there a reason you do it this way? – BuddyJoe Aug 14 '12 at 13:21
  • Hi @BuddyJoe. Not sure if there is a specific guideline, but I can tell you that if you declare multiple variables on the same line, ala `int* c, p;` You're going to have problems if you expect that both `c` and `p` are pointers. Unfortunately, you'd want to write something like `int *c, *p;`. Having said that, I don't like declaring multiple variables on the same line. – Garviel Aug 17 '17 at 03:25
-1

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Samrat Patil
  • 788
  • 5
  • 23
  • *This is not correct.* **This is correct**. `&nr` is not a `nullptr`. There is another issue: `sizeof(nr) != sizeof(NETRESOURCE)`. – 273K May 04 '23 at 04:12