title: how to pass LPTSTR to a function and modify it's contents
body: <p>I need a function that is supplied a LPTSTR and an enumerated value, constructs a string based on the value and puts it in the LPTSTR.</p>
  
  <p>I've written the following function which uses an array of names indexed by an enumerated value:</p>
  
  <pre><code>bool GetWinClassName(const int &amp;WinType, LPTSTR *className, const int bufSize)
  {
      bool allOk = true;
      LPTSTR tempName = new TCHAR[bufSize];
      _stprintf_s(tempName, bufSize, TEXT("Win%sClass"), g_WinNames[WinType]);
      std::cout &lt;&lt; (char*)tempName &lt;&lt; std::endl;
      if (FAILED(StringCchCopy(*className, (bufSize+1)*sizeof(TCHAR), tempName)))
      {
          allOk = false;
      }
      delete[] tempName;
      return allOk;
  }
  </code></pre>
  
  <p>(Originally I just had the <code>_stprintf_s</code> line using <code>className</code> instead of <code>tempName</code>, this has been broken up to find where the error lies.)</p>
  
  <p>The above code compiles in VC2010 Express but gives an unhandled exception: "Access violation writing" to (presumably) <code>*className</code> when it tries to execute the <code>StringCchCopy</code> line. </p>
  
  <p>I can get this to work by doing </p>
  
  <pre><code>className = new TCHAR[bufSize];
  </code></pre>
  
  <p>before calling the function (with a matching  <code>delete[]</code> after it) but do I really need to do that each time I want to call the function? </p>
  
  <p>I understand <em>where</em> the problem lies but not <em>why</em> which is hampering my efforts to come up with a workable solution. The problem appears to me to be that I can't put something in the LPTSTR (via <code>_stprintf_s</code> or <code>StringCchCopy</code>) unless I allocate it some memory by using <code>new TCHAR[bufSize]</code>. I've tried assigning it an intial value of exactly the same size but with the same results which is leading me to think that the memory allocation actually has nothing to do with it. Is it then somehow casting my LPTSTR into a TCHAR[]? I don't see how that's possible but at this stage, I'd believe anything. </p>
  
  <p>Can someone please explain what I'm doing wrong? (Or at least where my understanding is wrong.) And a probably related question is why is my <code>std::cout</code> only showing the first character of the string?</p>