title: C++: convert TCHAR array to std::string
body: <p>I want to convert a <code>TCHAR</code> array to <code>std::string</code>. After searching the internet, I did it by this way: </p>
  
  <pre><code>TCHAR fileName[260];
  GetProcessName(
      reinterpret_cast&lt;DWORD&gt;(processId), 
      fileName, 
      MAX_PATH
      );
  std::wstring tmpWString( fileName );
  std::string result( tmpWString.begin(), tmpWString.end() );
  
  std::cout &lt;&lt; "the length of the tmpWString is: " &lt;&lt; tmpWString.length() &lt;&lt; std::endl;
  </code></pre>
  
  <p>This gives me a <code>tmpWString</code> of length <code>0</code>. However, by a pure coincidence, I added a sleep function before the conversion, then everything goes smoothly, and I can get the conversion result properly:</p>
  
  <pre><code>TCHAR fileName[260];
  ::Sleep(500);
  GetProcessName(
      reinterpret_cast&lt;DWORD&gt;(processId), 
      fileName, 
      MAX_PATH
      );
  std::wstring tmpWString( fileName );
  std::string result( tmpWString.begin(), tmpWString.end() );
  
  std::cout &lt;&lt; "the length of the tmpWString is: " &lt;&lt; tmpWString.length() &lt;&lt; std::endl;
  std::cout &lt;&lt; "this is the resulted string: " &lt;&lt; result &lt;&lt; std::endl;
  </code></pre>
  
  <p>I didn't get it. Why would adding a sleep makes it work while omitting it causes a zero-length <code>wstring</code>? I didn't want to include any sleep in my code after all. Could someone explain this? My working environment is Visual Studio 2010, on a virtual machine with Windows XP. For the <code>Character Set</code> of <code>Configuration Properties</code> of VS2010, I didn't specify any set. Thanks in advance. </p>