Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine clipboard data size?

I have a custom data format in the clipboard (it is put there by a third-party program). I get the size of the data from the clipboard using:

HANDLE data = GetClipboardData(format);
DWORD len = GlobalSize(data);

The problem is, I get incorrect data size (about plus 100 bytes of what it actually should be). I mean, the size of real "valid" data in the clipboard is different. Moreover, I get different (!) data sizes from one call to another (although, the data itself does not change). Looks mysterious. I mean, if I just keep executing the above two lines in a loop, I'll be getting different values in "len" for this format.

So I have two questions basically...

  1. How comes that "GlobalSize" may return non-constant values from one call to another for the very same data?

  2. How do I get the "real" data size for the clipboard data in this format?

Thank you in advance!

like image 805
Nikolay Avatar asked Feb 27 '14 17:02

Nikolay


1 Answers

From the MSDN page on the GlobalSize function:

The size of a memory block may be larger than the size requested when the memory was allocated

So you can not rely on GlobalSize to return the requested size of the allocation. Instead, you should pass the size as part of the data object itself (e.g. begin with a DWORD containing the size, followed by your data).

like image 198
Jonathan Potter Avatar answered Sep 22 '22 21:09

Jonathan Potter