Search code examples
c++winapitextcopy-pasteselected

How to retrive text that was copied (with the CTRL + C command)


I have a Windows application written in C++. I want to add a paste option, so that on request the application can retrieve whatever text the user previously copied (i.e., with the control-C command).

Is there a way to do this?


Solution

  • You need to use the functions OpenClipboard(), GetClipboardData() and CloseClipboard().

    From MSDN:

    Pasting Information from the Clipboard

    1. Open the clipboard by calling the OpenClipboard function.

    2. Determine which of the available clipboard formats to retrieve.

    3. Retrieve the handle to the data in the selected format by calling the GetClipboardData function.

    4. Insert a copy of the data into the document.

      The handle returned by GetClipboardData is still owned by the clipboard, so an application must not free it or leave it locked.

    5. Close the clipboard by calling the CloseClipboard function.