Read/Write text on clipboard


Building the Right Environment to Support AI, Machine Learning and Deep Learning

	CString source; 
	//put your text in source
		HGLOBAL clipbuffer;
		char * buffer;
		clipbuffer = GlobalAlloc(GMEM_DDESHARE, source.GetLength()+1);
		buffer = (char*)GlobalLock(clipbuffer);
		strcpy(buffer, LPCSTR(source));

How to get text off of the clipboard

This is easy really but here it is for completeness
	char * buffer;
		buffer = (char*)GetClipboardData(CF_TEXT);
		//do something with buffer here 
		//before it goes out of scope



  • OpenClipboard

    Posted by DevWin32 on 12/13/2006 03:43pm

    Hi: First thank you for this article which had hepled me to learn how to deal with clipboard. I've a remark, the OpenClipboard function should take an argument (HWND) which it is not the case here.

  • get data from applet window

    Posted by Legacy on 04/17/2003 12:00am

    Originally posted by: erhan

    How can I get data from i.e explorer applet window..
    input box element or list element text area.


    • Get data from applet

      Posted by Tscheulin on 10/16/2008 07:35am

      Dear erhan, have you got an answer ? I am very interested about it. Thanks, Hansruedi Tscheulin tscheulin@bluewin.ch

  • Modifying F2, F3 and F4 to work as Cut, Copy & Paste

    Posted by Legacy on 04/01/2003 12:00am

    Originally posted by: Timothy C. Alvord

    I am looking for help in developing a SysTray app that will catch the pressing of the three Function keys F2, F3 and F4 and perform the same functions as CTRL-X, CTRL-C and CTRL-V. Any help would be greatly appreciated.


  • How to paste from clipboard 2 bmp file?

    Posted by Legacy on 03/27/2003 12:00am

    Originally posted by: Sikachev Peter

    If you have some ideas send'em 2 my mail, please.
    mail_to: garret-lit@yandex.ru

  • Access Violation

    Posted by Legacy on 01/15/2003 12:00am

    Originally posted by: Radu


    1) At first sight the code worked fine. However after some copy operations I got an access violation on the line:

    buffer = (char*)GlobalLock(clipbuffer);

    2) Something else seems to be wrong here. The "buffer" is initialized with some crap in it. I had a "," at the beginning of the buffer and in some cases also some other characters.

    Any idea why?

    Thanks for your effort.

  • whats the format used for ActiveX Control

    Posted by Legacy on 10/17/2001 12:00am

    Originally posted by: mercy

    hi all,

    ihave a ActiveX control in the clipboard.what is the Format that i should use in the fn GetClipboardData(UINT uFormat) to get that control.


  • Does not work in Windows CE

    Posted by Legacy on 06/28/2001 12:00am

    Originally posted by: YY

    I tried this in Windows CE 3.0 with embdedded visual tool 3.0 and it does not seem to be putting stuff on the clipboard:

    char source[100] = "Try This one";
    if ( OpenClipboard (NULL) )
    HLOCAL clipbuffer;
    char * buffer;
    EmptyClipboard ();
    clipbuffer = LocalAlloc (0, sizeof(source) + 1);
    buffer = (char*) LocalLock (clipbuffer);
    strcpy (buffer, LPSTR (source) );
    LocalUnlock (clipbuffer);
    SetClipboardData (CF_TEXT, clipbuffer);
    CloseClipboard ();
    return (0);

  • Checking the clipboard format before copying the text data

    Posted by Legacy on 05/14/2001 12:00am

    Originally posted by: S.K.Pradhan

    Thanx a lot for this posting. Given below is the code which will check for TEXT data in clipboard before copying to the destination string.

    UINT cbFormat=0;
    bool bTextFormat=0;
    char *myDestStr;
    if (cbFormat==CF_TEXT) bTextFormat=1;
    else AfxMessageBox("NO Text data in clipboard");
    else AfxMessageBox("Could not open clipboard, some other task might be in use.");


  • Binary data in the clipboard.

    Posted by Legacy on 04/15/2001 12:00am

    Originally posted by: Michel Bouchet

    Here is the way I solved the problem (mentioned by myself in the previous article) :

    1) I used RegisterClipboardFormat to create my own format, since none of the standard (CF_TEXT ...) seems to correspond to my needs :

    UINT CF_BINFormat;
    2) then I changed :
    to become :
    and :
    buffer = (char*)GetClipboardData(CF_TEXT);
    to become :
    buffer =(unsigned char*)GetClipboardData(CF_BINFormat);

    Then I basically followed the example using memcpy() instead of strcpy() since my data are not necessarily NULL terminated.

    If somebody thinks this is not the proper way to deal with the clipboard. Please let me know.

  • Clipboard (Set & Get Data)

    Posted by Legacy on 04/02/2001 12:00am

    Originally posted by: Michel Bouchet

    Thanks for posting this example.
    It helped me to understand the basic way to use a clipboard in an application.

    But what if I need to store arbitrary data in the clipboard. I mean not only text but any string of bytes which might also of course include 0x00 or any other values.

    Thanks for any clue.

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date