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"
"C:\\Temp\\file2.txt";
DROPFILES dobj = { 20, { 0, 0 }, 0, 1 };
int nLen = sizeof(sFiles);
int nGblLen = sizeof(dobj) + nLen*2 + 5;//lots of nulls and multibyte_char
HGLOBAL hGbl = GlobalAlloc(GMEM_ZEROINIT|GMEM_MOVEABLE|GMEM_DDESHARE, nGblLen);
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];
}
::GlobalUnlock(hGbl);

if( OpenClipboard() )
{
EmptyClipboard();
SetClipboardData( CF_HDROP, hGbl );
CloseClipboard();
}
}

Date Last Updated: April 5, 1999



Comments

  • Wonderful

    Posted by H smith on 01/29/2014 05:36am

    Very helpful mate, works a treat

    Reply
  • Great article

    Posted by Matteo on 04/05/2012 05:12am

    Great article sir thank you !

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date