Where can I view the size (in bytes) of whatever my clipboard is currently holding in Microsoft Windows 7?
2 Answers
You can use the win32clipboard module in python and perform many functions with the data in the Windows clipboard.
You would have to make it from scratch but it's possible to make a function that reads a copy of the data into the VM and examines its size.
- 30,396
- 15
- 136
- 260
- 129
- 2
In .NET framework the clipboard is manipulated by Windows.Forms.Clipboard in Windows.Forms or Windows.Clipboard in PresentationCore
Since PowerShell can interact with any .NET components, you can use it to work with clipboard contents directly without any 3rd party software. Save the following snippet as a *.ps1 script, for example clipboardsize.ps1, and run. You can try it with anything like a list of copied files in Explorer or a Word object.
function printClipboard($obj)
{
if ($obj.Length -ne $null) {
echo "length: $($obj.Length)"
} elseif ($obj.Size -ne $null) {
echo "size: $($obj.Size)"
}
}
Add-Type -AssemblyName PresentationCore
$dataObject = [Windows.Clipboard]::GetDataObject()
or
Add-Type -AssemblyName System.Windows.Forms
$dataObject = [Windows.Forms.Clipboard]::GetDataObject()
foreach ($format in $dataObject.GetFormats()) {
try {
$obj = $dataObject.GetData($format);
Write-Host -NoNewLine `
"Format '$format', type $($obj.GetType().ToString())"
# If data is a list then print length of each element
if ($obj -is [System.Array])
{
echo " [$($obj.Length)]"
foreach ($o in $obj) {
Write-Host -NoNewLine " "
printClipboard($o)
}
} else {
Write-Host -NoNewLine ", "
printClipboard($obj)
}
} catch {
echo "Format '$format' - failed to obtain data"
}
}
Here's a sample output for a piece of text copied from a website
PS C:\Users> .\clipboardsize.ps1
Format 'text/html', type System.IO.MemoryStream, length: 9040
Format 'HTML Format', type System.String, length: 4793
Format 'text/_moz_htmlcontext', type System.IO.MemoryStream, length: 766
Format 'text/_moz_htmlinfo', type System.IO.MemoryStream, length: 8
Format 'Text', type System.String, length: 642
Format 'UnicodeText', type System.String, length: 642
Format 'System.String', type System.String, length: 642
Format 'text/x-moz-url-priv', type System.IO.MemoryStream, length: 192
and here's the sample output when I right click an image in Firefox and select Copy Image
Format 'text/html', type System.IO.MemoryStream, length: 672
Format 'HTML Format', type System.String, length: 502
Format 'text/_moz_htmlinfo', type System.IO.MemoryStream, length: 8
Format 'text/_moz_htmlcontext', type System.IO.MemoryStream, length: 2
Format 'application/x-moz-file-promise-url', type System.IO.MemoryStream, length: 130
Format 'application/x-moz-file-promise-dest-filename', type System.IO.MemoryStream, length: 48
Format 'FileDrop', type System.String[] [1]
length: 57
Format 'FileNameW', type System.String[] [1]
length: 57
Format 'FileName', type System.String[] [1]
length: 57
Format 'Preferred DropEffect', type System.IO.MemoryStream, length: 4
Format 'application/x-moz-nativeimage' - failed to obtain data
Format 'Format17', type System.IO.MemoryStream, length: 1350124
Format 'DeviceIndependentBitmap', type System.IO.MemoryStream, length: 1350040
As you can see, the clipboard isn't a simple data stream but a collection of various things that the source program puts into. For example if you copy a range of cells in Excel lots of formats will be put into the clipboard: text (raw Unicode, raw ANSI, RTF, HTML, XML, CSV...), image (EMF, WMF, BMP...), sheet (BIFF5, BIFF8, BIFF12...)...
Strings in .NET are actually encoded in UTF-16 so their length must be multiplied by 2 to get the size of the string (excluding metadata size). Streams are byte-based so the length is the real content's size in bytes
A similar question in VBA: How to get the size of the contents of MSForms.DataObject in MS Access VBA
Now what you see above isn't necessarily what's in the clipboard, so checking the size may not reflect what's being hold actually. There are many reasons:
Programs can use lazy rendering to avoid wasting memory and rendering time on something that you won't use in the future. This is commonly done for big objects like an Excel spreadsheet. So sometimes even if you see the data in clipboard, it doesn't consume any memory
Windows will automatically convert between data formats if possible. For example between image formats or text encodings. The source program may also convert between formats before putting into clipboard (as seen above), and the runtime you're running on may also does that (.NET converts raw texts to
System.String, images toSystem.Drawing.Imaging.Metafile,System.Drawing.Bitmap,System.Windows.Media.Imaging.BitmapSource...)If you copy Unicode text Windows will convert it to ANSI text when you request the ANSI version and vice versa. The same thing happens when you copy bitmaps. That means you'll never know whether the text has been put into clipboard in Unicode or ANSI, or the image was BMP, PNG or EMF. Notice the "Text" and "Unicode Text" formats above. The .NET framework converts both of them to Unicode due to the nature of .NET strings, but if you get the content from C++ you'll see 2 different strings with different sizes.
- 30,396
- 15
- 136
- 260