-1

am working in C++ Win32 Application and I implementing DragAndDrop operation, I can able to drag a files and folders from explorer to my application using WndProc calls with WM_DROPFILES, Now I trying for DragAndDrop files and folders from Win32 application to explorer.

I write a sample code to drag a text from application to explorer using Karlsson’s Drag-and-Drop C Library its working fine for text drop but my requirement is drag and drop files and folders from win32 application to explorer. below is my sample code for drop text.

case WM_LBUTTONDOWN:
{
    char* text = "Hello, World! This is the drop source.";
    HANDLE text_on_heap;
    PMYDROPSOURCE text_drop_source;
    CLIPFORMAT cf[1] = { CF_TEXT };
    text_on_heap = GlobalAlloc(GMEM_FIXED, strlen(text) + 1);
    if (NULL == text_on_heap) break;
    strcpy((char*)text_on_heap, text);
    text_drop_source = CreateMyDropSource(FALSE, cf, &text_on_heap, 1);
    if (NULL != text_drop_source)
    {
     /* Drop source created, so begin the drag. This will block. */
      MyDragDropSource(text_drop_source);
     /* Drag-drop is done so destroy the drop source. */
      FreeMyDropSource(text_drop_source);
    }
     /* Free up the buffer; drag-drop is finished. */
    GlobalFree(text_on_heap);
   return TRUE;
}
Krish
  • 376
  • 3
  • 14
  • Your code here doesn't attempt to operate with files – David Heffernan Oct 09 '17 at 06:46
  • @DavidHeffernan Yes. its a sample code for drag and drop text so am looking for drag and drop files and folders. same way i tried for CF_HDROP but its not working. – Krish Oct 09 '17 at 06:50
  • @Krish: Read the documentation: [Handling Shell Data Transfer Scenarios: Creating and Importing Scrap Files](https://msdn.microsoft.com/en-us/library/windows/desktop/bb776904.aspx#scrap). If you want to drag text and expect Explorer to store it in a file, `CF_TEXT` by itself is not enough. Also see [Shell Clipboard Formats](https://msdn.microsoft.com/en-us/library/windows/desktop/bb776902.aspx), in particular `CF_HDROP` and `CFSTR_FILEDESCRIPTOR+CFSTR_FILECONTENTS`. – Remy Lebeau Oct 09 '17 at 17:36
  • 1
    @DavidHeffernan when comes to Drag and Drop concept while write in win32 application its not that easy everything should be write manually like IDataObject, IDataSource, IDropSource and IDropTarget etc. and pass a proper structure to the DODragDrop call. google have a less source for this topics so that I put my question here, Hope this one will helps others. and this my solution i posted for [DoDragDrop](https://stackoverflow.com/questions/45365935/how-to-get-dropsource-from-dodragdrop-api-hooking) API Hooking – Krish Oct 10 '17 at 05:43

1 Answers1

3

To have Explorer accept dragged files, all we have to do is create some CF_HDROP data and put it in a data object. So you have to use IDropSource and IDropTarget.

So you need the DROPFILES struct. It is a bit tricky to create (since it's not always the same size).

Than you have to create an IDataSource and perform a DoDragDrop.

You find an overview in the MSDN

Here is a full sample that shows the operation with the MFC. Look into the last section.

Code to a plain Win32 version is here. It uses a small library public on sourceforge

xMRi
  • 14,982
  • 3
  • 26
  • 59
  • Thanks for the solution. yep its absolutely correct i will go for it. – Krish Oct 09 '17 at 06:57
  • 2
    Note that `CF_HDROP` only works for physical files on the filesytem. To drag&drop virtual files, use `CFSTR_FILEDESCRIPTOR` and `CFSTR_FILECONTENTS` instead. – Remy Lebeau Oct 09 '17 at 17:39
  • @RemyLebeau and xMRi can anyone share me the samples if u have please, google have very less resource for Win32 drag&drop from application to explore, even I write my own and I fallow the steps as mentioned by xMRi but no result. – Krish Oct 11 '17 at 12:17
  • I got this to work by using SHCreateStdEnumFmtEtc. For some reason, windows explorer does not count my custom IEnumFormatEtc object. Since CodeBlocks does not declare the function in its shlobj.h file, I had to declare the function myself. – user13947194 Mar 16 '22 at 22:49