title: How to set HTML Unicode text to clipboard in VC++?
body: <p>I am a newbie to C++. I want to get the content of the clipboard, which might contain Unicode chars, append a div tag with some content formatted in HTML and set that back to clipboard.</p>
  
  <p><em>I have achieved successfully in getting the content and appending it.</em> But could not set it back to the <em>clipboard as an HTML text</em>. I have achieved setting as simple text. Here is my code:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;shlwapi.h&gt;
  #include &lt;iostream&gt;
  #include &lt;conio.h&gt;
  #include &lt;stdio.h&gt;
  
  using namespace std;
  
  wstring getClipboard(){
      if (OpenClipboard(NULL)){
          HANDLE clip = GetClipboardData(CF_UNICODETEXT);
          WCHAR * c;
          c = (WCHAR *)clip;
          CloseClipboard();
          return (WCHAR *)clip;
      }
      return L"";
  }
  
  bool setClipboard(wstring textToclipboard)
  {
      if (OpenClipboard(NULL)){
          EmptyClipboard();
          HGLOBAL hClipboardData;
          size_t size = (textToclipboard.length()+1) * sizeof(WCHAR);
          hClipboardData = GlobalAlloc(NULL, size);
          WCHAR* pchData = (WCHAR*)GlobalLock(hClipboardData);
          memcpy(pchData, textToclipboard.c_str(), size);
          SetClipboardData(CF_UNICODETEXT, hClipboardData);
          GlobalUnlock(hClipboardData);
          CloseClipboard();
          return true;
      }
      return false;
  }
  
  int main (int argc, char * argv[])
  {
     wstring  s =  getClipboard();
     s += std::wstring(L"some extra text &lt;b&gt;hello&lt;/b&gt;");
     setClipboard(s);
     getch();
     return 0;
  }
  </code></pre>
  
  <p>I did try using the code described <a href="http://support.microsoft.com/kb/274308" rel="nofollow">here</a> and read the doc <a href="http://msdn.microsoft.com/en-us/library/aa767917.aspx" rel="nofollow">here</a>. But I couldn't make it work. What I tried could be way off track or completely wrong. </p>
  
  <p><strong>Update:</strong> The code below is what I tried after the modifications suggested by Cody Gray to the original code presented <a href="http://support.microsoft.com/kb/274308" rel="nofollow">here</a>:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>bool CopyHTML2(WCHAR *html ){
  
      wchar_t *buf = new wchar_t [400 + wcslen(html)];
      if(!buf) return false;
  
      static int cfid = 0;
      if(!cfid) cfid = RegisterClipboardFormat("HTML Format");
  
  
          // Create a template string for the HTML header...
      wcscpy(buf,
          L"Version:0.9\r\n"
          L"StartHTML:00000000\r\n"
          L"EndHTML:00000000\r\n"
          L"StartFragment:00000000\r\n"
          L"EndFragment:00000000\r\n"
          L"&lt;html&gt;&lt;body&gt;\r\n"
          L"&lt;!--StartFragment --&gt;\r\n");
  
      // Append the HTML...
      wcscat(buf, html);
      wcscat(buf, L"\r\n");
      // Finish up the HTML format...
      wcscat(buf,
          L"&lt;!--EndFragment--&gt;\r\n"
          L"&lt;/body&gt;\r\n"
          L"&lt;/html&gt;");
  
      wchar_t *ptr = wcsstr(buf, L"StartHTML");
      wsprintfW(ptr+10, L"%08u", wcsstr(buf, L"&lt;html&gt;") - buf);
      *(ptr+10+8) = L'\r';
  
      ptr = wcsstr(buf, L"EndHTML");
      wsprintfW(ptr+8, L"%08u", wcslen(buf));
      *(ptr+8+8) = '\r';
  
      ptr = wcsstr(buf, L"StartFragment");
      wsprintfW(ptr+14, L"%08u", wcsstr(buf, L"&lt;!--StartFrag") - buf);
      *(ptr+14+8) = '\r';
  
      ptr = wcsstr(buf, L"EndFragment");
      wsprintfW(ptr+12, L"%08u", wcsstr(buf, L"&lt;!--EndFrag") - buf);
      *(ptr+12+8) = '\r';
  
      // Open the clipboard...
      if(OpenClipboard(0)) {
          EmptyClipboard();
          HGLOBAL hText = GlobalAlloc(GMEM_MOVEABLE |GMEM_DDESHARE, wcslen(buf)+4);
          wchar_t *ptr = (wchar_t *)GlobalLock(hText);
          wcscpy(ptr, buf);
          GlobalUnlock(hText);
          SetClipboardData(cfid, hText);
          CloseClipboard();
          GlobalFree(hText);
      }
  
      // Clean up...
      delete [] buf;
      return true;
  }
  </code></pre>
  
  <p>This code compiles successfully, But I get the following error at SetClipboardData : <code>HEAP[Project1.exe]: Heap block at 007A8530 modified at 007A860A past requested size of d2
  Project1.exe has triggered a breakpoint.</code></p>
  
  <p>Please guide me on how to proceed. I am using Visual Studio Express 2012 on Windows 8. Thanks.</p>