Access the clipboard with idataobject

Source: Internet
Author: User

An example of a simple access to Clipboard through OLE:

 
Winoleapi olegetclipboard (idataobject ** ppdataobj );

This simple Windows API call is used to return an idataobject, which provides a good interface for Clean Access to Windows clipboard content. Note: In this example, we do not need to implement the idataobject interface. We only need to know how the interface works, a simple access to the clipboard content.ProgramAs follows:

# Include <windows. h> # include <objbase. h ># include <iostream> using namespace STD; void displaydataobject (idataobject * pdataobject) {formatetc fmtetc = {cf_text, 0, dvaspect_content,-1, tymed_hglobal}; stgmedium; if (pdataobject-> getdata (& fmtetc, & stgmedium) = s_ OK) {char * Data = (char *) globallock (stgmedium. hglobal); cout <data <Endl; globalunlock (stgmedium. hglobal); releasestgmedium (& stgmedium) ;}} int Main () {If (oleinitialize (0 )! = S_ OK) return 0; idataobject * pdataobject; If (olegetclipboard (& pdataobject) = s_ OK) {displaydataobject (pdataobject); pdataobject-> release ();} oleuninitialize (); return 0 ;}

The aboveCodeDemonstrate the most common method to access idataobject. Data is requested by calling idataobject: getdata to construct a formatetc object, which specifies the type of data to be accessed. In this example, the standard cf_text data buffer is stored as an hglobal memory object. The data is returned to the provided stgmedium struct. Once we lock and display the data, clean up and call the standard releasestgmedium API to release the data stored in the stgmedium structure.

note : The Code only works when the text is selected as a clipboard. That is to say, if no cf_text is stored in the clipboard, the calling of the clipboard idataobject: getdata program will fail. nothing is printed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.