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.