title: converting LPWSTR to char*/string
body: <p>My function GetErrorString() is passed an error code which is either the result of WSAGetLastError(), or one of the error codes defined in my DLL, which are returned when calls to my DLL functions are unable to complete. </p>
  
  <p>I have an array of std::pairs which store my error codes along with their const char* error strings</p>
  
  <pre><code>std::pair&lt;int, const char*&gt; errorCodeArray[12] =
  { 
      std::pair&lt;int,char*&gt;(0,"Success"),
      std::pair&lt;int,char*&gt;(1,"Connection Error"),
      std::pair&lt;int,char*&gt;(2,"Request Timed Out"),
      // ..etc
  };
  </code></pre>
  
  <p>If the errorcode is from WSAGetLastError() then I must use FormatMessage to get the error string as a LPWSTR, then convert it to a char*, I found this page:</p>
  
  <p><a href="https://stackoverflow.com/questions/5513718/how-do-i-convert-from-lpctstr-to-stdstring">How do I convert from LPCTSTR to std::string?</a></p>
  
  <p>and tried this soultion which apparently works with LPCTSTR</p>
  
  <pre><code>int size = WideCharToMultiByte(CP_ACP, 0, errCode, -1, 0, 0, NULL, NULL);
  char* buf = new char[size];
  WideCharToMultiByte(CP_ACP, 0, errCode, -1, buf, size, NULL, NULL);
  std::string str(buf);
  delete[] buf;
  return str.c_str();
  </code></pre>
  
  <p>but it doesn't seem to work with LPWSTR, the result is always "???????????" and I don't really understand character encoding enough to figure out a solution.</p>
  
  <p>Can anyone shed some light on this? Thanks.</p>