Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Copy to clipboard problem.

Copy to clipboard problem.

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rrrado
    wrote on last edited by
    #1

    I need to copy some string to the clipboard. This is the code I use:

    handle = GlobalAlloc(GMEM\_MOVEABLE,text.GetLength()+1);
    if (handle)
    {
    	if (OpenClipboard(hWnd) && EmptyClipboard() )
    	{
    		out = (char\*)GlobalLock(handle);
                        // this fails sometimes (out=NULL) 
    

    My code sometimes fail at out = (char*)GlobalLock(handle); GlobalLock returns NULL and GetLastError() = 6 (invalid handle). Any ideas why ? Is there more reliable code for copying to clipboard ? Thank you!


    rrrado

    C 1 Reply Last reply
    0
    • R rrrado

      I need to copy some string to the clipboard. This is the code I use:

      handle = GlobalAlloc(GMEM\_MOVEABLE,text.GetLength()+1);
      if (handle)
      {
      	if (OpenClipboard(hWnd) && EmptyClipboard() )
      	{
      		out = (char\*)GlobalLock(handle);
                          // this fails sometimes (out=NULL) 
      

      My code sometimes fail at out = (char*)GlobalLock(handle); GlobalLock returns NULL and GetLastError() = 6 (invalid handle). Any ideas why ? Is there more reliable code for copying to clipboard ? Thank you!


      rrrado

      C Offline
      C Offline
      cje
      wrote on last edited by
      #2

      I ran across the following code in one of our projects - not sure if it will help you or not HGLOBAL hData = 0; // open memory file CSharedFile clipb (GMEM_MOVEABLE|GMEM_DDESHARE|GMEM_ZEROINIT); clipb.Write(cstrString, cstrString.GetLength()*sizeof(TCHAR)); clipb.Write(_T("\0"), sizeof(TCHAR)); hData = clipb.Detach(); // Now, hand over memory block to clipboard if (hData) { VERIFY(OpenClipboard()); SetClipboardData(CF_TEXT, hData); VERIFY(CloseClipboard()); }

      cje

      R 1 Reply Last reply
      0
      • C cje

        I ran across the following code in one of our projects - not sure if it will help you or not HGLOBAL hData = 0; // open memory file CSharedFile clipb (GMEM_MOVEABLE|GMEM_DDESHARE|GMEM_ZEROINIT); clipb.Write(cstrString, cstrString.GetLength()*sizeof(TCHAR)); clipb.Write(_T("\0"), sizeof(TCHAR)); hData = clipb.Detach(); // Now, hand over memory block to clipboard if (hData) { VERIFY(OpenClipboard()); SetClipboardData(CF_TEXT, hData); VERIFY(CloseClipboard()); }

        cje

        R Offline
        R Offline
        rrrado
        wrote on last edited by
        #3

        Thank you. I think I've fouind the problem - I was releasing the memory after SetClipboardData which caused the problems.


        rrrado

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups