Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I just created an MFC application to find the file names from the clipboard.This code is working fine when we copy 1 file, but when we copy multiple files it only shows the first file. Is there is any method to find out all the file names that copied to the clipboard?

What I have tried:

AddClipboardFormatListener(AfxGetApp()->m_pMainWnd->m_hWnd);

LRESULT Cfile_trackerDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
    case WM_CLIPBOARDUPDATE:
        {
            AfxBeginThread(FileArrival, NULL);
            break;
        }
    case WM_CHANGECBCHAIN:
        {
            AfxBeginThread(FileArrival, NULL);
            break;
        }
    }
    return CDialog::WindowProc(message, wParam, lParam);
}

UINT FileArrival(LPVOID param)
{
    TCHAR lpszFileName[MAX_PATH];
    char *szTime;
    time_t thistime;
        OpenClipboard(0);
        HGLOBAL hGlobal = (HGLOBAL)GetClipboardData(CF_HDROP);
        if (hGlobal)
        {
            HDROP hDrop = (HDROP)GlobalLock(hGlobal);
            if (hDrop)
            {
                time(&thistime);
                szTime = ctime(&thistime);
                DragQueryFile(hDrop, 0, lpszFileName, MAX_PATH);
                WriteLog((char*)lpszFileName,1);
                GlobalUnlock(hGlobal);
            }
        CloseClipboard();
    }
    return 0;
}
Posted
Updated 25-Apr-17 0:42am

1 solution

See the documentation: DragQueryFile function (Windows)[^].

Call DragQueryFile first with iFile set to 0xFFFFFFFF. That call will return the number of file names. Then use a loop to get each file name:
UINT fileCount = DragQueryFile(hDrop, 0xffffffff, NULL, 0);
for (UINT i = 0; i < fileCount; i++)
{
    DragQueryFile(hDrop, i, lpszFileName, MAX_PATH);
    /* Do something with lpszFileName here */
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900