Copying a CString to the clipboard - Programmer

This is a discussion on Copying a CString to the clipboard - Programmer ; All, can someone please enlighten me as to what I am doing wrong with the following code. All I am trying to do is copy a CString to the clipboard. strTemp is the variable I whose contents I want copied ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Copying a CString to the clipboard

  1. Copying a CString to the clipboard

    All, can someone please enlighten me as to what I am doing wrong with the
    following code.
    All I am trying to do is copy a CString to the clipboard.
    strTemp is the variable I whose contents I want copied to the clipboad.
    Here is my code:

    // Copy the text to the clipboard
    if (strTemp.GetLength() > 0 && OpenClipboard())
    {
    HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, (strTemp.GetLength()) *
    sizeof(TCHAR));

    if (hglbCopy != NULL && EmptyClipboard())
    {
    // Place the handle on the clipboard.
    HANDLE hSuccess = SetClipboardData(CF_TEXT, hglbCopy);

    }
    // Close the clipboard.
    CloseClipboard();

    } else {
    CloseClipboard();
    }

    Any ideas



  2. Re: Copying a CString to the clipboard

    It would help to copy the text into hglbCopy!

    OpenClipboard();
    char *Buffer = GlobalLock(hglbCopy);
    strcpy(Buffer,strTemp);
    GlobalUnlock(hglbCopy);
    SetClipboardData(CF_TEXT,hglbCopy);
    CloseClipboard();
    GlobalFree(hglbCopy);

    Ali R.

    "Paul Tomlinson" wrote in message
    news:bspot9$4ejo$1@ID-116287.news.uni-berlin.de...
    > All, can someone please enlighten me as to what I am doing wrong with the
    > following code.
    > All I am trying to do is copy a CString to the clipboard.
    > strTemp is the variable I whose contents I want copied to the clipboad.
    > Here is my code:
    >
    > // Copy the text to the clipboard
    > if (strTemp.GetLength() > 0 && OpenClipboard())
    > {
    > HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, (strTemp.GetLength())

    *
    > sizeof(TCHAR));
    >
    > if (hglbCopy != NULL && EmptyClipboard())
    > {
    > // Place the handle on the clipboard.
    > HANDLE hSuccess = SetClipboardData(CF_TEXT, hglbCopy);
    >
    > }
    > // Close the clipboard.
    > CloseClipboard();
    >
    > } else {
    > CloseClipboard();
    > }
    >
    > Any ideas
    >
    >




+ Reply to Thread