Obtener datos del Portapapeles en formato sin procesar en 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


Intenté esto hasta ahora:

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

struct clipData saveClipboard(int &size) {
 clipData ret;
 UINT currentFormat = 0;
 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);
 return ret;

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

preguntado el 09 de enero de 11 a las 03:01

2 Respuestas

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.

Respondido el 09 de enero de 11 a las 07:01

@Dave18 I pasted what I tried so far and that's my main problem - qwertymk

The procedure is format-dependent, see Standard Clipboard Formats. Registered formats and private formats in the range (CF_GDIOBJFIRST through CF_GDIOBJLAST) all use GlobalAlloc, so your calls to GlobalSize y GlobalLock will work fine for them (but do match calls to GlobalLock es GlobalUnlock). - Ben Voigt

Be careful of formats like CF_BITMAP, CF_ENHMETAFILE, CF_HDROP -- with those, the object on the clipboard is an HBITMAP, HENHMETAFILEo HDROP, no es an HGLOBAL! - Adam Rosenfield

@qertymk EnumClipboardFormats returns you the currentFormat so what you need to check is if you have data in CF_TEXT (text format) before reading it. Link that I have provided has an example of clipboard viewer. - cpx

@Dave18: Unfortunately, that sample handles almost all formats by asking the clipboard owner to render the data, it doesn't actually handle the content directly except for a couple specific formats. - Ben Voigt

MSDN has all the examples you need to know to manipulate clipboard data using Clipboard API.

Respondido el 09 de enero de 11 a las 07:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.