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
  • GHD MK4 Guld

    Posted by motherdhmm on 05/30/2013 01:23pm

    [url=http://www.buy-beatsdrdre.com/category/2012-dr-dre-beats-outlet]dr dre beats[/url] Masser af enkeltpersoner butik på internettet inden for ord, der involverer lethed og komfort som et alternativ til køber bistand. Kunde plan online giver forbedret alligevel den faktiske individuelle del af off-line indtjening fortsætter med at være mangel på. En række art vedrørende organisation bliver salon eller blot en fase spa bygger op med bestemt person lidenskabelig forbindelse. Hver eneste solo valg forbundet med disse former er forbundet med dejlige stylers kan godt forekomme at arbejde med smukke person, temps beskyttelse quiltet håndtaske som lysarmaturer farve sammen sammen med dinGhd Fladjern er du nødt til at få ram på den samme tid til at få set næsten enhver dårlig situation, som i turn repræsenterer du eventuelt kunne gemme disse mennesker specielt præcis, hvor du vælger at bevæge sig fremad. Mens absolut alle fuldt ud at forstå god hår dage er virkelig identificeret sammenlignes for at hjælpe en fantastisk virksomhed, der vil ofte leverer din ideelle køber bistand, som vil betyder du vil være i stand til regelmæssigt at generere komme i kontakt med med den slags forbundet med. rød-farvet Lust Gode hår dage vil uden tvivl øge taget i betragtning tilsluttet låse system. [url=http://www.buy-beatsdrdre.com/]beats by dre[/url] En ideel kvinde er ofte afbilledet til at være en person med en slank figur med langt hår. Hår har altid været vigtigt for hver kvinde, fordi det en eller anden måde afspejler deres personlighed. Have lange skinnende hår kan forføre en mand, kan det lander en kvinde i en shampoo kommercielle, eller få komplimenter fra andre. Det kan afspejle en kvindes sundhed og giver et indtryk af, at hun er sund, og at hun tager sig godt af sig selv. Selvfølgelig ville det ikke alle kvinder vil have langt hår. [url=http://www.buy-beatsdrdre.com/]beats by dre headphones[/url] GHD Fladjern elsker i øjnene af beskueren, den mest romantiske Valentinsdag i Kina, som udgør deres egen skønhed. Nyt tøj, nye sko, et godt sind er identificeret, så er der tilbringer en vis indsats på håret. Willow-lignende swing, elegant 3000 sort hår, der viser en kvinde, den smukkeste side. Hair Salon Hair Design tænke over, hvad slags kanaler i håret af smukke kvinder over ..

    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

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • Savvy enterprises are discovering that the cloud holds the power to transform IT processes and support business objectives. IT departments can use the cloud to redefine the continuum of development and operations—a process that is becoming known as DevOps. Download the Executive Brief DevOps: Why IT Operations Managers Should Care About the Cloud—prepared by Frost & Sullivan and sponsored by IBM—to learn how IBM SmartCloud Application services provide a robust platform that streamlines …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds