Page 1 of 1

How to Copy Handle of any File or Folder to Clipboard

Posted: 20 Mar 2018, 09:19
by digiraiter
Hello
I want to Copy some Specific (mp3) and (jpg) files to clipboard one by one to add them to another program
without a need to open File Explorer
but I can't find a function to do that
for Exampe:

Code: Select all

FileCopy, D:\0.jpg, Clipboard
and if i press "Ctrl + V" in any Directory, it be pasted in there
This Topic Say the Same thing, but solution won't work for me
I wonder if some command been changed since 2007
https://autohotkey.com/board/topic/2316 ... clipboard/

Code: Select all

FileToClipboard(PathToCopy)
{
    ; Expand to full path:
    Loop, %PathToCopy%, 1
        PathToCopy := A_LoopFileLongPath
    
    ; Allocate some movable memory to put on the clipboard.
    ; This will hold a DROPFILES struct, the string, and an (extra) null terminator
    ; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)
    hPath := DllCall("GlobalAlloc","uint",0x42,"uint",StrLen(PathToCopy)+22)
    
    ; Lock the moveable memory, retrieving a pointer to it.
    pPath := DllCall("GlobalLock","uint",hPath)
    
    NumPut(20, pPath+0) ; DROPFILES.pFiles = offset of file list
    
    ; Copy the string into moveable memory.
    DllCall("lstrcpy","uint",pPath+20,"str",PathToCopy)
    
    ; Unlock the moveable memory.
    DllCall("GlobalUnlock","uint",hPath)
    
    DllCall("OpenClipboard","uint",0)
    ; Empty the clipboard, otherwise SetClipboardData may fail.
    DllCall("EmptyClipboard")
    ; Place the data on the clipboard. CF_HDROP=0xF
    DllCall("SetClipboardData","uint",0xF,"uint",hPath)
    DllCall("CloseClipboard")
}
Thanks

Re: How to Copy Handle of any File or Folder to Clipboard

Posted: 20 Mar 2018, 12:25
by gregster
In 2007 there was only a 32 bit ANSI version of AHK, now you are probably running 32 or 64 bit Unicode AHK. So, I guess someone who knows their DllCalls well enough will have to take a look at this.

Re: How to Copy Handle of any File or Folder to Clipboard

Posted: 20 Mar 2018, 12:44
by jeeswg
See JEE_ClipboardSetPaths:
GUI COMMANDS: COMPLETE RETHINK - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 42#p144342

Re: How to Copy Handle of any File or Folder to Clipboard

Posted: 21 Mar 2018, 10:57
by digiraiter
gregster wrote:In 2007 there was only a 32 bit ANSI version of AHK, now you are probably running 32 or 64 bit Unicode AHK. So, I guess someone who knows their DllCalls well enough will have to take a look at this.
I Have no Experiment on DLL programming, so I had a Hard time to Learn about it.
But what you said was a big help and Interestingly I found the answer in the Topic I Post Here in Page 4
I put the Code here. Posted by [maraskan_user]:
That script was from before unicode and x64 support was added to Autohotkey, and depending on which version of Autohotkey and Windows 7 you're using, might not work anymore. [fragman] posted a version that should support those features
It's a Script that can copy several file into clipboard to be pasted in somewhere else manually

Code: Select all

FileToClipboard(PathToCopy,Method="copy")
	{
	FileCount:=0
	PathLength:=0

	; Count files and total string length
	Loop,Parse,PathToCopy,`n,`r
		{
		FileCount++
		PathLength+=StrLen(A_LoopField)
		}

	pid:=DllCall("GetCurrentProcessId","uint")
	hwnd:=WinExist("ahk_pid " . pid)
	; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)
	hPath := DllCall("GlobalAlloc","uint",0x42,"uint",20 + (PathLength + FileCount + 1) * 2,"UPtr")
	pPath := DllCall("GlobalLock","UPtr",hPath)
	NumPut(20,pPath+0),pPath += 16 ; DROPFILES.pFiles = offset of file list
	NumPut(1,pPath+0),pPath += 4 ; fWide = 0 -->ANSI,fWide = 1 -->Unicode
	Offset:=0
	Loop,Parse,PathToCopy,`n,`r ; Rows are delimited by linefeeds (`r`n).
		offset += StrPut(A_LoopField,pPath+offset,StrLen(A_LoopField)+1,"UTF-16") * 2

	DllCall("GlobalUnlock","UPtr",hPath)
	DllCall("OpenClipboard","UPtr",hwnd)
	DllCall("EmptyClipboard")
	DllCall("SetClipboardData","uint",0xF,"UPtr",hPath) ; 0xF = CF_HDROP

	; Write Preferred DropEffect structure to clipboard to switch between copy/cut operations
	; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)
	mem := DllCall("GlobalAlloc","uint",0x42,"uint",4,"UPtr")
	str := DllCall("GlobalLock","UPtr",mem)

	if (Method="copy")
		DllCall("RtlFillMemory","UPtr",str,"uint",1,"UChar",0x05)
	else if (Method="cut")
		DllCall("RtlFillMemory","UPtr",str,"uint",1,"UChar",0x02)
	else
		{
		DllCall("CloseClipboard")
		return
		}

	DllCall("GlobalUnlock","UPtr",mem)

	cfFormat := DllCall("RegisterClipboardFormat","Str","Preferred DropEffect")
	DllCall	("SetClipboardData","uint",cfFormat,"UPtr",mem)
	DllCall("CloseClipboard")
	return
	}

#F9::FileToClipboard("D:\aadfadf.txt")
and It's Working for me on Windows 7 64Bits SP1 & AHK V.1.1.28.00