Copy and Paste files to/from clipboard lazarus / 21-General ---------------------------------------------------------------------- geraldholdsworth · 2021-05-19 16:17 Hi all, I'm wanting to copy, from my application, a file (or number of files) to the clipboard so that they are available to be pasted into Explorer/Finder (so is multi-platform too). The file is held in a byte array (but could easily be in a TStream), and the filename would be in a String. I've found a number of functions (well, they all appear to be the same) that just store the filename(s) on the clipboard. But, what about the data? Would I need, therefore, to save the data to a temporary file(s) and put these names (paths) onto the clipboard? But then, what about the original filename? Cheers, Gerald. ---------------------------------------------------------------------- Remy Lebeau · 2021-05-19 16:25 Quote from: geraldholdsworth on May 19, 2021, 06:17:16 pmI've found a number of functions (well, they all appear to be the same) that just store the filename(s) on the clipboard. But, what about the data? On Windows at least, you can use the CFSTR_FILECONTENTS and CFSTR_FILEDESCRIPTOR clipboard formats to accomplish what you want.  Where CFSTR_FILECONTENTS will contain either an IStream, IStorage, or HGlobal for your byte data, and CFSTR_FILEDESCRIPTOR will contain the filename and attributes associated with the data. See Handling Shell Data Transfer Scenarios on MSDN, in particular Copying the Contents of a Dropped File into an Application and Using the CFSTR_FILECONTENTS Format to Extract Data from a File. Quote from: geraldholdsworth on May 19, 2021, 06:17:16 pmWould I need, therefore, to save the data to a temporary file(s) and put these names (paths) onto the clipboard? That would certainly be an easier option.  But that is not required. Quote from: geraldholdsworth on May 19, 2021, 06:17:16 pmBut then, what about the original filename? Just give the temporary file the original filename that you want.  Nothing requires you to use a temporary filename provided by the OS. ---------------------------------------------------------------------- geraldholdsworth · 2021-05-21 15:48 Thank you Remy. I haven't had a chance to look into this again, but I did notice that the code I'd come across was written by yourself, so I'm pretty confident of this working out. ---------------------------------------------------------------------- geraldholdsworth · 2021-05-22 13:21 Started well, but the problem with a lot of code that is posted is that it usually does not show which units are required. As such, I'm falling at the first hurdle as Lazarus does not know about identifier CF_HDROP, and I haven't got a clue which unit defines it - I've tried a few, all result in 'Cannot find used by MainUnit of the Project Inspector'. I could probably add the list of filenames to the clipboard, now I know which format 'name' is used for Windows, macOS and Linux (using a clipboard viewer I found on this forum). ---------------------------------------------------------------------- avra · 2021-05-22 14:17 Quote from: geraldholdsworth on May 22, 2021, 03:21:59 pmLazarus does not know about identifier CF_HDROP, and I haven't got a clue which unit defines itHave you tried to search files in your FPC directory? CF_HDROP has a value of 15, and can be found in \fpcsrc\rtl\win\wininc\defines.inc which is included in \fpcsrc\rtl\win32\windows.pp. In short, it will be enough to include windows unit. ---------------------------------------------------------------------- geraldholdsworth · 2021-05-22 14:48 Thank you. Much appreciated. Quote from: avra on May 22, 2021, 04:17:22 pmIn short, it will be enough to include windows unit. That was one I tried, and was told it couldn't find it. Just tried LCLIntf too, but it still doesn't know about CF_HDROP. I should've pointed out that I'm pottering away with this on a Mac, but the intention is to make it multi-platform (well Windows, Mac and Linux anyway). Maybe I should potter away on Windows, then try and get it going on macOS and Linux afterwards. ---------------------------------------------------------------------- geraldholdsworth · 2021-05-23 16:00 Following up, and this is just more of a blog for those who stumble across this thread. I've written (well, used dbannon's code and modified it) a Clipboard Viewer to see what the OS does when you copy a file. I modified the code so it displays the format ID instead of the format name (and therefore, does not skip it if blank). I have confirmed what I already knew in that each OS does it differently. Windows and Linux seem easier as the format IDs stay the same. But, macOS has to be difficult - the format ID differs after each reboot...so I'll need to use the format name for these. I have found that format ID 15, defined to be CF_HDROP, (which had a blank name) only occurred with Windows - but thank you again for giving me that snippet of info, so I knew what to look for. I have attached some text files which are the outputs of the clipboard viewer. Each is named for the OS it was running on, and the second one (donated by the '(2)') is a repeat of the first but after a reboot. So, with this info, I can see what needs changed in Remy's code to accommodate Linux, and an almost complete re-write to accommodate macOS.