Win 10 clipboard history, VB6 IDE, Hyper-V Manager's VM Connection

Post your working scripts, libraries and tools.
ludamo
Posts: 44
Joined: 25 Mar 2015, 02:21

Win 10 clipboard history, VB6 IDE, Hyper-V Manager's VM Connection

Post by ludamo » 19 Sep 2021, 00:16

I have been using Visual Basic 6 lately and found that the Win10 (v 21H1 19043.1237 x64) clipboard history behaves oddly when copying and pasting in the VB6 IDE code window. I.e. the text of copied code is not visible in the clipboard history window though it is there as a blank entry and pastes OK. After trying to troubleshoot this issue within Windows 10 and VB6 itself, looking for compatibility fixes or any interfering VB6 add-ins I resorted to using AHK to help.

Using v2 I used the OnClipboardChange to call a function to copy the clipboard contents to a variable, then emptied the clipboard, then copied the variable back to the clipboard. Fortunately this now made the contents visible when using the clipboard history in VB6.

But I found that if I pasted the non-last entry from the clipboard history (in other programs as well, not just VB6) that it seemed to trigger the OnClipboardChange event also and I now had the pasted entry duplicated in the top spot on the clipboard history viewer. (Perhaps Win10 does an internal copy to the 'classic' clipboard from the 'newer with history' clipboard). Looking into this further I looked at the WM_CLIPBOARDUPDATE (0x031D) msg with VS Spy++ (this msg is also received by AHK if the OnClipboardChange function is used) and found that with a normal copy operation the wParam = 6 and with a Win10 clipboard history paste of any non-last entry the wParam = 8. All this even though the MS docs read that the wParam is not used and must be zero.

So I have the following code which seems to get around the issues outlined above, and am interested in any comments.

Code: Select all

#Requires AutoHotkey v2.0-a

OnClipboardChange ClpBrd_Change, 1

OnMessage 0x031D, wm_ClpBrdUpdate				; WM_ClipboardUpdate = 0x031D wParam=6 with classic copy, wParam=8 with paste from Win10 Clipboard History of non-last entry

wm_ClpBrdUpdate(wParam, lParam, msg, hWnd) {
	global ClpBrdUpdate := wParam
}

ClpBrd_Change(iType) {
	; workaround for VB6 Clipboard bug - entries present but no preview on Win10 Clipboard history 
	; this function gets called when Class IDEOwner (VB6) is active
	; and clipboard contents change also when pasting from Win10 Clipboard History
	; therefore deactivate OnClipboardChange when Clipboard History appears - see ShellHookMessage OR
	; use ClpBrdUpdate (= wParam) to distinguish between classic copy (=6) or Win10 Clip history paste of non-last entry (8)
	global ClpBrdUpdate

	if ClpBrdUpdate = 6 and iType = 1 and WinActive("ahk_class wndclass_desked_gsk")	; code pane Class for VB6 (and VBA7)
	{
		OnClipboardChange ClpBrd_Change, 0		; turn off so that function is not called recursively
		cb .= A_Clipboard		; save text content of Clipboard. Compare with ClipboardAll
		Sleep 20
		A_Clipboard := ""		; Empty Clipboard of recent VB6 copy because they don't show in Clipboard history
		Sleep 20
		A_Clipboard := cb		; copy saved contents back to Clipboard from AHK which will show in Clipboard history
		Sleep 20
		ToolTip cb				; to indicate this function is active

		OnClipboardChange ClpBrd_Change, 1	; turn back on
	}
	; Tooltip "ClpBrd changed"
}
Last edited by ludamo on 28 Oct 2021, 17:04, edited 1 time in total.
lexikos
Posts: 9494
Joined: 30 Sep 2013, 04:07
Contact:

Re: Windows 10 clipboard history and Visual Basic 6 IDE

Post by lexikos » 24 Sep 2021, 23:59

As far as the majority of applications are concerned, the "clipboard history" doesn't exist. There is only one clipboard, with only a single item represented by data potentially in multiple formats (like ANSI text, Unicode text, HTML, rich text, etc.). To paste a given item, the clipboard manager would empty the clipboard (EmptyClipboard) and place the item on the clipboard (SetClipboardData). This naturally triggers any clipboard listeners, because it is the same operation used to place data on the clipboard from any other source.

What you have found with wParam is interesting and may be helpful to others.

WM_CLIPBOARDDATA is received by windows that have registered for it with AddClipboardFormatListener. It is exactly the means by which OnClipboardChange works.

The ability to concatenate to an uninitialized variable with cb .= is not intended, and should raise an error. There is no reason to concatenate in this case; you should use a normal := assignment instead.

The "AutoHotkey v2 Development" forum is for topics about development of AutoHotkey v2 itself. Since this appears to be a functional solution to a problem, I have moved the topic to v2 Scripts.
Adventurer
Posts: 23
Joined: 18 Apr 2019, 06:24

Re: Windows 10 clipboard history and Visual Basic 6 IDE

Post by Adventurer » 27 Oct 2021, 18:14

Thank you a lot for this script. Changing line 19 to

Code: Select all

if ClpBrdUpdate = 8 and iType = 1 and WinActive("ahk_exe vmconnect.exe")
solves the issue of Hyper-V Manager's Virtual Machine Connection not saving copied text to Windows 10's clipboard history.

I recommend changing the topic slightly to indicate that this can fix issues with any app that doesn't seem to cooperate with the clipboard history feature.
Post Reply

Return to “Scripts and Functions (v2)”