Copying Files into Explorer

Monday Apr 5th 1999 by M Basava Raju

Copying Files into Explorer

Following my previous article on copying filenames from explorer, I received many requests on how to copy the files into the explorer. I have/had the same request for a long time. After a lot of debugging and breaking my head, I finally solved the problem (hopefully).

The clue I got was that, there is a DROPFILES structure in shlobj.h. However, there is no documentation on how to use it. Actually, it is the structure, whose pointer is the one we are receiving as HDROP handle. That is the mystery.

Just debug HDROP value before calling DragQueryFile function. It always says, 20. It is so because the DROPFILES structure's first member is the offset where the files are placed. Because the files are always attached to the end of the structure, the 20 is the size of the DROPFILES structure.

One important issue is that, the DROPFILES's last parameter specifies whether the appended string is a Multi/Single Byte character string. Windows NT uses Multi byte Character set. To suit to this requirement, we will also use Multi Byte character format for the appended file names.

So, do the following steps to get the job done.

1. First, prepare a string of null separated file names and pad with an extra null. The extra null is the requirement to indicate the end of file names.

2. Next, prepare a HGLOBAL of size equal to the sum of length of string of the file names and the size of the DROPFILES. Account for NULLs in between and at the end.

The HGLOBAL should be created with these flags: GMEM_ZEROINIT|GMEM_MOVEABLE|GMEM_DDESHARE

3. Lock the HGLOBAL to get a memory pointer. Fill up the first 20 bytes memory with the DROPFILES structure values.

4. Copy the file names to the rest of the memory. Once copying is done, unlock the HGLOBAL.

5. Open the clipboard, empty it and do SetClipboardData with CF_HDROP format and your HGLOBAL.

6. Close the clipboard, but don't free the HGLOBAL.

7. That's it. Your job is done.

Note: Please note that I have done this in hurry. There will be better methods to prepare a multibyte character string from a single byte character string. What I have done is one of the simple ways to take care of NULLs etc.

8. Now go to Explorer. Open one directory, do right-click. Paste will be enabled. Select paste. Your files will be copied to that directory.

#include "shlobj.h"

void CMyWnd::OnSetFilesToClipboard()
char sFiles[] = "C:\\Temp\\file1.txt\0"
DROPFILES dobj = { 20, { 0, 0 }, 0, 1 };
int nLen = sizeof(sFiles);
int nGblLen = sizeof(dobj) + nLen*2 + 5;//lots of nulls and multibyte_char
char* sData = (char*)::GlobalLock(hGbl);
memcpy( sData, &dobj, 20 );
char* sWStr = sData+20;
for( int i = 0; i < nLen*2; i += 2 )
sWStr[i] = sFiles[i/2];

if( OpenClipboard() )
SetClipboardData( CF_HDROP, hGbl );

Date Last Updated: April 5, 1999

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved