Clipboard formats

Get help with using AutoHotkey and its commands and hotkeys
SunAndSuch
Posts: 40
Joined: 05 Oct 2015, 12:11

Clipboard formats

20 Nov 2018, 16:21

Hello. I have the following code, which crashes and I don't know why.

Code: Select all

#NoEnv
#MaxMem,1024
MsgBox,% Clipboard ;Shows plain text, copied from a webpage (as expected).
html1:=clip_GetHtmlString()
MsgBox,% html1 ;Shows html (as expected).
clip_SetHtmlString(html1)
MsgBox,% Clipboard ;Shows nothing (as expected).
html2:=clip_GetHtmlString() ;Crashes here - specifically, when executing the line DllCall("User32.dll\OpenClipboard","Ptr",0,"UInt")
MsgBox,% html2
;----------------------------------------------------------------------------------------------
clip_GetHtmlString()
{	if !DllCall("User32.dll\IsClipboardFormatAvailable","UInt",ClipFormatHtml:=DllCall("User32.dll\RegisterClipboardFormat","Str","HTML Format","UInt"))
		return 0
	DllCall("User32.dll\OpenClipboard","Ptr",0,"UInt")
	htmlString:=DllCall("User32.dll\GetClipboardData","UInt",ClipFormatHtml,"Str")
	DllCall("User32.dll\CloseClipboard")
	Len:=(StrLen(htmlString)*(A_IsUnicode?2:1))
	If (Len<1)
		return 0
	return StrGet(&htmlString,Len,"")
}
clip_SetHtmlString(htmlString)
{	DllCall("User32.dll\OpenClipboard","Uint",0)
	DllCall("User32.dll\EmptyClipboard")
	DllCall("User32.dll\SetClipboardData","Uint",DllCall("User32.dll\RegisterClipboardFormat","Str","HTML Format","UInt"),"Astr",htmlString)
	DllCall("User32.dll\CloseClipboard")
	return
}
Prior to running the script I select some random text on a random webpage and copy it (^C). It doesn't crash though, if I only copy a small amount of data (e.g. less than 1000 characters). Can someone help?
Windows 7, Ahk v1 x64-bit.
SunAndSuch
Posts: 40
Joined: 05 Oct 2015, 12:11

Re: Clipboard formats

21 Nov 2018, 16:59

Well, I clearly should've used something like this for the second part, I guess:

Code: Select all

clip_SetHtmlString(htmlString)
{	vSize:=StrPut(htmlString,"CP0")
	VarSetCapacity(vData,vSize,0)
	StrPut(htmlString,&vData,vSize,"CP0")
	hBuf:=DllCall("kernel32\GlobalAlloc",UInt,0x42,UPtr,vSize,Ptr)
	pBuf:=DllCall("kernel32\GlobalLock",Ptr,hBuf,Ptr)
	DllCall("msvcrt\memcpy",Ptr,pBuf,Ptr,&vData,UPtr,vSize,"Cdecl Int")
	hWnd:=A_ScriptHwnd ? A_ScriptHwnd : WinExist("ahk_pid " DllCall("kernel32\GetCurrentProcessId",UInt))
	DllCall("kernel32\GlobalUnlock",Ptr,hBuf)
	DllCall("user32\OpenClipboard",Ptr,hWnd)
	DllCall("user32\EmptyClipboard")
	DllCall("user32\SetClipboardData",UInt,DllCall("user32\RegisterClipboardFormat",Str,"HTML Format",UInt),Ptr,hBuf,Ptr)
	DllCall("user32\CloseClipboard")
}
Not 100% sure what all of that does, but at least now it is not crashing. Would still appreciate correction if there is something wrong with the code. I've used the previous version for years without having any problems and thinking everything was all right.
Windows 7, Ahk v1 x64-bit.

Return to “Ask For Help”

Who is online

Users browsing this forum: CyL0N, Google [Bot], just me, Xtra and 98 guests