Page 1 of 1

SetClipboardPrivate()

Posted: 29 Nov 2021, 19:13
by Xuerian
Windows 10+ introduced Clipboard History and syncing.
Useful, but it makes transferring data with the clipboard problematic.

I couldn't find any examples of avoiding this without disabling these tools entirely, but programs like password managers already were.

You'll have to handle the clipboard directly. After you set your actual data, you must also set some 0 values with specific formats:

Code: Select all

DllCall("SetClipboardData", "Int", DllCall("RegisterClipboardFormat", "Str", "ExcludeClipboardContentFromMonitorProcessing"), "Ptr", 0)
DllCall("SetClipboardData", "Int", DllCall("RegisterClipboardFormat", "Str", "CanIncludeInClipboardHistory"), "Ptr", 0)
DllCall("SetClipboardData", "Int", DllCall("RegisterClipboardFormat", "Str", "CanUploadToCloudClipboard"), "Ptr", 0)
Here's a full function based on SetClipboardHTML():

Code: Select all

SetClipboardPrivate(str)
{
   Bytes := StrPut(str, "utf-16")
   hMemTXT := DllCall("GlobalAlloc", "Int", 0x42, "Ptr", (Bytes*2)+8, "Ptr")
   pMem := DllCall("GlobalLock", "Ptr", hMemTXT, "Ptr")
   StrPut(str, pMem, Bytes, "utf-16")
   DllCall("GlobalUnlock", "Ptr", hMemTXT)
   DllCall("OpenClipboard", "Ptr", A_ScriptHwnd)
   DllCall("EmptyClipboard")
   DllCall("SetClipboardData", "Int", 13, "Ptr", hMemTXT)
   DllCall("SetClipboardData", "Int", DllCall("RegisterClipboardFormat", "Str", "ExcludeClipboardContentFromMonitorProcessing"), "Ptr", 0)
   DllCall("SetClipboardData", "Int", DllCall("RegisterClipboardFormat", "Str", "CanIncludeInClipboardHistory"), "Ptr", 0)
   DllCall("SetClipboardData", "Int", DllCall("RegisterClipboardFormat", "Str", "CanUploadToCloudClipboard"), "Ptr", 0)
   DllCall("CloseClipboard")
}
Any corrections are welcome, I'm just gluing stuff together here.