Copy Text via DllCall Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
buttshark
Posts: 62
Joined: 22 Apr 2017, 20:57

Copy Text via DllCall

16 Oct 2018, 22:01

Hi all,

I would like to make a function that copies highlighted text, but cannot use ^c because that is a defined hotkey, and I don't want to trigger it. If you instead have an idea of how to do that instead, that'd be great, but keep in mind:
  1. I can't use $^c because I DO want other parts to trigger the ^c hotkey
  2. I can't use SendPlay ^c because that does nothing
  3. I can't use #InputLevel/SendLevel because I want ^c on level 0.
I tried to follow this guide, but I don't know enough about DllCall to figure it out myself, thus my post.

I figured I need something like the following:

Code: Select all

DllCall("user32.dll\OpenClipboard", "Ptr", 0)
DllCall("user32.dll\EmptyClipboard")
VarSetCapacity(txt, some_number)
DllCall("user32.dll\SetClipboardData", "UInt", 1, "UPtr", &txt)
DllCall("user32.dll\CloseClipboard")
But, surprise, it doesn't work. A couple notes/thoughts/questions
  1. I think I need to set a capacity for txt before I call SetClipboardData, but I have no idea how to know how long the copied string is before I copy it.
  2. Looking at the documentation of SetClipboardData, it looks like both the second parameter and the return value are the same thing. Please correct me if I'm wrong.
  3. Even after using some dummy number for some_number and checking both txt AND the return value, I still get nothing promising.
  4. A_LastError turns out to be 6. I have no idea what that means or how to figure it out.
  5. I'm not sure if I need an additional parameter for the DllCall for SetClipboardData. Using something like "UPtr" doesn't seem to change the output at all.
I'm a noob to this stuff, so any insights would me much appreciated. Thanks in advance :⁠)
I have no idea what I'm doing.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Copy Text via DllCall  Topic is solved

16 Oct 2018, 22:19

- I have this function, however, I don't think the approach would quite work. The text belongs to an external process and so you need the external process to put that text onto the clipboard. The external process needs to have a 'clipboard set text' function of some kind, not your script. And normally, you make it use such a function by doing, guess what, sending Ctrl+C to it.
- So, the function below puts text onto the clipboard, but how do I actually get that text into memory, before putting it onto the clipboard? If it's an AHK script I might use a GUI command to retrieve some control text.

Code: Select all

;vOpt: e (empty clipboard before setting contents)
JEE_ClipboardSetText(vText, vOpt:="")
{
	local
	;GMEM_ZEROINIT := 0x40, GMEM_MOVEABLE := 0x2
	hBuf := DllCall("kernel32\GlobalAlloc", UInt,0x42, UPtr,(StrLen(vText)+2)*2, Ptr)
	pBuf := DllCall("kernel32\GlobalLock", Ptr,hBuf, Ptr)
	StrPut(vText, pBuf, StrLen(vText)+1, "UTF-16")

	;CF_LOCALE := 0x10 ;CF_UNICODETEXT := 0xD
	;CF_OEMTEXT := 0x7 ;CF_TEXT := 0x1
	hWnd := A_ScriptHwnd ? A_ScriptHwnd : WinExist("ahk_pid " DllCall("kernel32\GetCurrentProcessId", UInt))
	DllCall("kernel32\GlobalUnlock", Ptr,hBuf)
	DllCall("user32\OpenClipboard", Ptr,hWnd)
	if InStr(vOpt, "e")
		DllCall("user32\EmptyClipboard")
	DllCall("user32\SetClipboardData", UInt,0xD, Ptr,hBuf, Ptr)
	DllCall("user32\CloseClipboard")
}
- You could try sending Ctrl+Insert.
- Sometimes you can invoke a menu item to copy.
- You could try SendMessage with WM_COPY := 0x301.
Last edited by jeeswg on 16 Oct 2018, 22:36, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
buttshark
Posts: 62
Joined: 22 Apr 2017, 20:57

Re: Copy Text via DllCall

16 Oct 2018, 22:35

I had no idea Ctrl+Insert was a thing!

From what I can tell, that seems to be good enough for my uses, though I'm still curious how one would go about solving my original problem.

Thanks again :⁠)
I have no idea what I'm doing.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Google [Bot] and 111 guests