CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Apr 2011
    Posts
    22

    SetClipboardData() to Copy File - Solved

    Hi guys,

    I want to copy a file to the clipboard but I'm not sure what data to pass to SetClipboardData(). The input would be the full path of the file I want on the clipboard so that I can later paste it in Explorer (making a copy.)

    I'm using old-school Win32.

    // create global memory, what type?

    DWORD dwSize = (???);
    HANDLE hGlobal = GlobalAlloc(GHND, dwSize);
    (???) data = (???)GlobalLock(hGlobal);
    // fill it in
    GlobalUnlock(hGlobal);

    // clipboard stuff

    OpenClipboard(NULL);
    EmptyClipboard();
    SetClipboardData(CF_HDROP ???, hGlobal);
    CloseClipboard();

    All I really need to know is what's in the global memory I'm passing to SetClipboardData(), I think it's a list of files but how do I set it up?

    Thanks, and it's nice to see this forum is still alive.

    ********************************

    Okay guys, I figured it out. I hope this helps the community.

    How to copy a file path to the clipboard so you can later paste it somewhere like Explorer (making a copy of the file.)

    char* path = "c:\\images\\0001.jpg";

    HGLOBAL hGlobal = GlobalAlloc(GHND, sizeof(DROPFILES) + strlen(path) + 2);

    if (!hGlobal)
    return;

    DROPFILES* dropfiles = (DROPFILES*)GlobalLock(hGlobal);

    if (!dropfiles) {
    GlobalFree(hGlobal);
    return;
    }

    dropfiles->pFiles = sizeof(DROPFILES);
    dropfiles->fNC = TRUE;
    dropfiles->fWide = FALSE;

    memcpy(&dropfiles[1], path, strlen(path));

    GlobalUnlock(hGlobal);

    if (!OpenClipboard(NULL)) {
    GlobalFree(hGlobal);
    return;
    }

    if (!EmptyClipboard()) {
    GlobalFree(hGlobal);
    return;
    }

    if (!SetClipboardData(CF_HDROP, hGlobal)) {
    GlobalFree(hGlobal);
    return;
    }

    GlobalFree(hGlobal);

    CloseClipboard();
    Last edited by endemoniada; August 4th, 2020 at 07:38 PM. Reason: solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured