Read/Write text on clipboard


	CString source; 
	//put your text in source
	if(OpenClipboard())
	{
		HGLOBAL clipbuffer;
		char * buffer;
		EmptyClipboard();
		clipbuffer = GlobalAlloc(GMEM_DDESHARE, source.GetLength()+1);
		buffer = (char*)GlobalLock(clipbuffer);
		strcpy(buffer, LPCSTR(source));
		GlobalUnlock(clipbuffer);
		SetClipboardData(CF_TEXT,clipbuffer);
		CloseClipboard();
	}

How to get text off of the clipboard

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

	CloseClipboard(); 



Comments

  • 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.

    Reply
  • 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.

    thanks.

    • 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

      Reply
    Reply
  • 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.

    Thanks,
    Tim

    Reply
  • 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

    Reply
  • Access Violation

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

    Originally posted by: Radu

    Hi,


    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.

    Reply
  • 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.

    thanks
    mercy

    Reply
  • 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);

    Reply
  • 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(OpenClipboard())
    {
    while(cbFormat=EnumClipboardFormats(cbFormat))
    {
    if (cbFormat==CF_TEXT) bTextFormat=1;
    }
    if(bTextFormat)
    {
    myDestStr=(char*)GetClipboardData(CF_TEXT);
    AfxMessageBox(myDestStr);
    }
    else AfxMessageBox("NO Text data in clipboard");
    }
    else AfxMessageBox("Could not open clipboard, some other task might be in use.");

    Regards
    SKP

    Reply
  • 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;
    CF_BINFormat=RegisterClipboardFormat("CF_MYBINFORMAT");
    .....
    2) then I changed :
    SetClipboardData(CF_TEXT,....
    to become :
    SetClipboardData(CF_BINFormat,......
    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.

    Reply
  • 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.

    Reply
  • 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.

Top White Papers and Webcasts

  • Event Date: April 15, 2014 The ability to effectively set sales goals, assign quotas and territories, bring new people on board and quickly make adjustments to the sales force is often crucial to success--and to the field experience! But for sales operations leaders, managing the administrative processes, systems, data and various departments to get it all right can often be difficult, inefficient and manually intensive. Register for this webinar and learn how you can: Align sales goals, quotas and …

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds