SetClipboardData() question...
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: SetClipboardData() question...

  1. #1

    Question SetClipboardData() question...

    I want to set text in a clipboard, so I've searched the msdn and found the next:
    HANDLE SetClipboardData(
    UINT uFormat, // clipboard format
    HANDLE hMem // data handle
    );
    I understand the uFormat, and I know it has to be CF_DSPTEXT, but I don't understand the hMem, what is this "data handle"?



    Thank you.
    Borland C++ 5.5 forever!

    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.TheDP.tk

  2. #2
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899
    Just a pointer to your data ... Nothing else ...

    You can allocate it by GlobalAlloc, new, malloc ... what you love ...

    p.s.: don't forget to call OpenClipboard first
    Last edited by NoHero; April 10th, 2004 at 06:29 AM.
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  3. #3
    That's just it, I've tried that, but the clipboard stays empty:

    Code:
    char *addr_str;
    
    addr_str = (char *) malloc(sizeof(char)*50);
    strcpy(addr_str, "hello");
    
    OpenClipboard(hDlg);
    EmptyClipboard();
    SetClipboardData(CF_DSPTEXT, addr_str);
    CloseClipboard();
    But SetClipboardData() returns the next error code:
    87 The parameter is incorrect. ERROR_INVALID_PARAMETER
    Last edited by Devil Panther; April 10th, 2004 at 08:06 AM.
    Borland C++ 5.5 forever!

    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.TheDP.tk

  4. #4
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899
    *mhm*

    try this ...

    Code:
    char   *addr_str;
    
    addr_str = (char*)GlobalAlloc(GHND, 100); // Or how many bytes you need
    OpenClipboard(hDlg);
    
    // Empty it
    EmptyClipboard();
    // Put the data
    SetClipboardData(CF_TEXT, (void*)addr_str); 
    CloseClipboard();
    works this?!
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  5. #5
    The clipboard still remains empty.

    I tried something similar, with the same result:

    Code:
    char *addr_str;
    
    addr_str = (char *) GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, 100);
    
    strcpy(addr_str, "hello");
    
    OpenClipboard(hDlg);
    EmptyClipboard();
    SetClipboardData(CF_DSPTEXT, addr_str);
    CloseClipboard();
    Borland C++ 5.5 forever!

    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.TheDP.tk

  6. #6
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899
    I'm confused now, cause my sample works fine ... really fine ...
    Strange ... Sry ... wait for the gurus to answer that
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  7. #7
    How did you copy the data into addr_str?
    Borland C++ 5.5 forever!

    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.TheDP.tk

  8. #8
    Well I figured it out, kinda:
    Code:
    HGLOBAL hText;
    char *pText;
    
    hText = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, 100);
    pText = GlobalLock(hText);
    strcpy(pText, "hello");
    GlobalUnlock(hText);
    
    OpenClipboard(hDlg);
    EmptyClipboard();
    SetClipboardData(CF_TEXT, hText);
    CloseClipboard();
    But can someone explain me what's with the GlobalLock(), etc. ?
    Borland C++ 5.5 forever!

    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.TheDP.tk

  9. #9
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899
    GlobalLock locks the buffer and returns a handle to it ...

    I searched MSDN for some answers and I found this code ... I made it after this direction and it worked ...

    Code:
    #define BOX_ELLIPSE  0 
    #define BOX_RECT     1 
     
    #define CCH_MAXLABEL 80 
    #define CX_MARGIN    12 
     
    typedef struct tagLABELBOX {  // box 
        RECT rcText;    // coordinates of rectangle containing text 
        BOOL fSelected; // TRUE if the label is selected 
        BOOL fEdit;     // TRUE if text is selected 
        int nType;      // rectangular or elliptical 
        int ichCaret;   // caret position 
        int ichSel;     // with ichCaret, delimits selection 
        int nXCaret;    // window position corresponding to ichCaret 
        int nXSel;      // window position corresponding to ichSel 
        int cchLabel;   // length of text in atchLabel 
        TCHAR atchLabel[CCH_MAXLABEL]; 
    } LABELBOX, *PLABELBOX; 
    // Following is the EditCopy function. 
    
    BOOL WINAPI EditCopy(VOID) 
    { 
        PLABELBOX pbox; 
        LPTSTR  lptstrCopy; 
        HGLOBAL hglbCopy; 
        int ich1, ich2, cch; 
     
        if (hwndSelected == NULL) 
            return FALSE; 
     
        // Open the clipboard, and empty it. 
     
        if (!OpenClipboard(hwndMain)) 
            return FALSE; 
        EmptyClipboard(); 
     
        // Get a pointer to the structure for the selected label. 
     
        pbox = (PLABELBOX) GetWindowLong(hwndSelected, 0); 
     
        // If text is selected, copy it using the CF_TEXT format. 
     
        if (pbox->fEdit) 
        { 
            if (pbox->ichSel == pbox->ichCaret)     // zero length
            {   
                CloseClipboard();                   // selection 
                return FALSE; 
            } 
     
            if (pbox->ichSel < pbox->ichCaret) 
            { 
                ich1 = pbox->ichSel; 
                ich2 = pbox->ichCaret; 
            } 
            else 
            { 
                ich1 = pbox->ichCaret; 
                ich2 = pbox->ichSel; 
            } 
            cch = ich2 - ich1; 
     
            // Allocate a global memory object for the text. 
     
            hglbCopy = GlobalAlloc(GMEM_MOVEABLE, 
                (cch + 1) * sizeof(TCHAR)); 
            if (hglbCopy == NULL) 
            { 
                CloseClipboard(); 
                return FALSE; 
            } 
     
            // Lock the handle and copy the text to the buffer. 
     
            lptstrCopy = GlobalLock(hglbCopy); 
            memcpy(lptstrCopy, &pbox->atchLabel[ich1], 
                cch * sizeof(TCHAR)); 
            lptstrCopy[cch] = (TCHAR) 0;    // null character 
            GlobalUnlock(hglbCopy); 
     
            // Place the handle on the clipboard. 
     
            SetClipboardData(CF_TEXT, hglbCopy); 
        } 
     
        // If no text is selected, the label as a whole is copied. 
     
        else 
        { 
            // Save a copy of the selected label as a local memory 
            // object. This copy is used to render data on request. 
            // It is freed in response to the WM_DESTROYCLIPBOARD 
            // message. 
     
            pboxLocalClip = (PLABELBOX) LocalAlloc( 
                LMEM_FIXED, 
                sizeof(LABELBOX) 
            ); 
            if (pboxLocalClip == NULL) 
            { 
                CloseClipboard(); 
                return FALSE; 
            } 
            memcpy(pboxLocalClip, pbox, sizeof(LABELBOX)); 
            pboxLocalClip->fSelected = FALSE; 
            pboxLocalClip->fEdit = FALSE; 
     
            // Place a registered clipboard format, the owner-display 
            // format, and the CF_TEXT format on the clipboard using 
            // delayed rendering. 
     
            SetClipboardData(uLabelFormat, NULL); 
            SetClipboardData(CF_OWNERDISPLAY, NULL); 
            SetClipboardData(CF_TEXT, NULL); 
        } 
     
        // Close the clipboard. 
     
        CloseClipboard(); 
     
        return TRUE; 
    }
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  10. #10
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,068
    Originally posted by Devil Panther
    But can someone explain me what's with the GlobalLock(), etc. ?
    When you do GlobalAlloc with GMEM_MOVEABLE you get a handle not a pointer to your data. You have to use GlobalLock to get a pointer to the data. Use that pointer and then use GlobalUnlock to unlock the handle again.

  11. #11
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899
    Originally posted by Marc G
    When you do GlobalAlloc with GMEM_MOVEABLE you get a handle not a pointer to your data. You have to use GlobalLock to get a pointer to the data. Use that pointer and then use GlobalUnlock to unlock the handle again.
    but that doesn't solve the problem ...
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  12. #12
    The problem was that SetClipboardData(), only recieved handle, and not data (string, for example).

    And with GlobalLock() you get that data pointer.
    Borland C++ 5.5 forever!

    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.TheDP.tk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


HTML5 Development Center