title: How to convert char* to LPCWSTR?
body: <p>I know this has already been discussed in several questions on SO, but none of those solutions have worked for me.</p>
  
  <p>I start with a <code>char*</code> because this is for a DLL that will be called from VBA, and <code>char*</code> is necessary for VBA to pass a string to the DLL.</p>
  
  <p>I need to return a <code>LPCWSTR</code> 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:</p>
  
  <pre><code>#if !defined(UNICODE) &amp;&amp; !defined(NOUNICODE)
  #error UNICODE is not defined. UNICODE must be defined for correct API arguments.
  #endif
  </code></pre>
  
  <p>I tried this:</p>
  
  <pre><code>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]));
      }
  </code></pre>
  
  <p>which gave these errors:</p>
  
  <pre><code>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'
  </code></pre>
  
  <p>I tried this (same function header), loosely adapted from <a href="https://stackoverflow.com/a/6858692/619177">this post</a>:</p>
  
  <pre><code>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(&amp;retVal,buf,20, &amp;cs, length + 1, NULL);
  return buf;
  </code></pre>
  
  <p>This compiled ok, but when I passed it an example string of "xyz.xlsx", <code>mbsrtowcs_s()</code> set <code>buf</code> to an empty string: <code>L""</code></p>
  
  <p>So, how do I make this conversion?</p>