Get clipboard size without handling the data in AHK?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
william_ahk
Posts: 512
Joined: 03 Dec 2018, 20:02

Get clipboard size without handling the data in AHK?

Post by william_ahk » 01 Feb 2024, 01:21

Code: Select all

ClipAll := ClipboardAll
ClipSize := VarSetCapacity(ClipAll)
Msgbox % ClipSize
I can get the clipboard size by saving the data to a variable, and get the granted capacity from VarSetCapacity. But with this method, sometimes when the data is large, the memory usage would spike up so much that it jams my computer because AHK is copying the data in memory, which defeats my purpose of skip processing clipboard copies that exceed certain size.

User avatar
lmstearn
Posts: 698
Joined: 11 Aug 2016, 02:32
Contact:

Re: Get clipboard size without handling the data in AHK?

Post by lmstearn » 03 Feb 2024, 02:20

Nothing much has changed in Clipboard since this thread:
ClipboardAll works by iterating through the available clipboard formats, storing the data of each format. Some programs use delayed rendering and possibly other tricks, so the full data being copied isn't actually in the clipboard. I'm not sure whether the methods used by AutoHotkey will trigger delayed rendering; if not, perhaps that's the problem. However, MSDN seems to imply that it should be automatic:
The system sends the clipboard owner a WM_RENDERFORMAT message when a request is received for a specific format that has not been rendered.
Not sure exactly how to go with it, might be quicker to save it to binary file with FileAppend, else venture down the burrow of delayed rendering as to what Petzold suggests:
If your program can transfer only one format of data to the clipboard (text, for instance), you can combine the
WM_RENDERALLFORMATS and WM_RENDERFORMAT processing. The code will look something like this:
case WM_RENDERALLFORMATS :
OpenClipboard (hwnd) ;
EmptyClipboard () ;
// fall through
case WM_RENDERFORMAT :
[put text into global memory block]
SetClipboardData (CF_TEXT, hGlobal) ;
if (message == WM_RENDERALLFORMATS)
CloseClipboard () ;
return 0 ;
HTH
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH

william_ahk
Posts: 512
Joined: 03 Dec 2018, 20:02

Re: Get clipboard size without handling the data in AHK?

Post by william_ahk » 03 Feb 2024, 09:59

FileAppend, %ClipboardAll% also has to handle the data in order to write it to a file. Delayed rendering isn't an option for me as I have no control over the clipboard setters, my script reads and processes the clipboard.

Alas, it seems there is no way around the wretched idiosyncrasies of Michaelsoft.

Post Reply

Return to “Ask for Help (v1)”