title: Copying string to clipboard, only one character written when pasted
body: <p>I was basic some code off this <a href="http://www.frostcode.info/common-articles/write-to-windows-clipboard.html" rel="nofollow noreferrer">code</a>, also mentioned in my other <a href="https://stackoverflow.com/questions/2253476/does-mfc-provide-a-quick-way-to-throw-text-on-the-clipboard">question</a>. That version forces the character type to <code>char*</code>, which breaks compilation on my unicode project. So I made the following tweaks:</p>
  <pre><code>void SetClipboardText(CString &amp; szData)
  {
      HGLOBAL h;
      LPTSTR arr;
  
      size_t bytes = (szData.GetLength()+1)*sizeof(TCHAR);
      h=GlobalAlloc(GMEM_MOVEABLE, bytes);
      arr=(LPTSTR)GlobalLock(h);
      ZeroMemory(arr,bytes);
      _tcscpy_s(arr, szData.GetLength()+1, szData);
      szData.ReleaseBuffer();
      GlobalUnlock(h);
  
      ::OpenClipboard (NULL);
      EmptyClipboard();
      SetClipboardData(CF_TEXT, h);
      CloseClipboard();
  }
  </code></pre>
  <p>The copying looks fine - running in a debugger Visual Studio tells me <code>arr</code> contains the copied string as expected.
  But When I then paste into any application, only the first character is pasted.</p>
  <p>What's going wrong?</p>