title: Mixing c++ standard strings and windows API
body: <p>Many windows APIs take a pointer to a buffer and a size element but the result needs to go into a c++ string. (I'm using windows unicode here so they are wstrings)</p>
  
  <p>Here is an example :-</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;windows.h&gt;
  
  using namespace std;
  
  // This is the method I'm interested in improving ...
  wstring getComputerName()
  {
      vector&lt;wchar_t&gt; buffer;
      buffer.resize(MAX_COMPUTERNAME_LENGTH+1);
      DWORD size = MAX_COMPUTERNAME_LENGTH;
  
      GetComputerNameW(&amp;buffer[0], &amp;size);
  
      return wstring(&amp;buffer[0], size);
  }
  
  int main()
  {
      wcout &lt;&lt; getComputerName() &lt;&lt; "\n";
  }
  </code></pre>
  
  <p>My question really is, is this the best way to write the getComputerName function so that it fits into C++ better, or is there a better way? I don't see any way to use a string directly without going via a vector unless I missed something? It works fine, but somehow seems a little ugly. The question isn't about that particular API, it's just a convenient example.</p>