简体   繁体   中英

Getting Clipboard data in raw format in c++

I've been playing around with the windows clipboard. I noticed that you can only view the clipboard if you supply a format. I've seen programs that can dump the raw contents of the clipboard. Look at http://www.autohotkey.com/docs/misc/Clipboard.htm#ClipboardAll for an example of what I mean.

Is there a way to do something similar, what I want to do is be able to back up the clipboard, manipulate it, then restore it when my program is done.

I'm looking for a non-.net solution if that's actually a thing

EDIT:

I tried this so far:

struct clipData {
 vector<void*> data;
 vector<int> size;
};

struct clipData saveClipboard(int &size) {
 clipData ret;
 UINT currentFormat = 0;
 HGLOBAL hData;
 if (OpenClipboard(0)) {

  while(currentFormat = EnumClipboardFormats(currentFormat)) {
   hData = GetClipboardData(currentFormat);
   int currentClipboardFormatSize = GlobalSize(hData); //Only works with text formats. Help!
   char *savedClipboardData = new char[currentClipboardFormatSize];
   char *ptrToData = (char*) GlobalLock(hData);
   memcpy(savedClipboardData, ptrToData, currentClipboardFormatSize);
   ret.data.push_back(savedClipboardData);
   ret.size.push_back(currentClipboardFormatSize);
   }
  CloseClipboard();
 }
 return ret;
}

But the problem is theres no way to tell how big the clipboard is in each format

There's no "raw" data involved. Just enumerate all the formats currently on the clipboard , and fetch and save the contents of each format. But be careful of automatic format conversions.

If you carefully read the autohotkey documentation you linked, it even tells you that it's retrieving each format separately, and that it may only succeed in retrieving a subset of the formats.

MSDN具有使用Clipboard API操作剪贴板数据所需的所有示例。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM