Question

At the moment I'm writing a Shell extension, because regular way to extend file context menu doesn't fit my needs, however I face the same problem here.

If I right click single shortcut (*.lnk file) I get its target path, if I select many files and right click on shortcut I only get one file - shortcuts target file.

My shell extension is not finished yet, but part of code which enumerates files is this:

HRESULT CFileContextMenuExt::Initialize(PCIDLIST_ABSOLUTE pidlFolder, IDataObject *pdtobj, HKEY hkeyProgID)
{
    HRESULT hr = E_INVALIDARG;
    if (NULL == pdtobj)
    {
        return hr;
    }

    FORMATETC fe = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
    STGMEDIUM stm = {};

    // pDataObj contains the objects being acted upon. In this example, 
    // we get an HDROP handle for enumerating the selected files.
    if (SUCCEEDED(pdtobj->GetData(&fe, &stm)))
    {
        // Get an HDROP handle.
        HDROP hDrop = static_cast<HDROP>(GlobalLock(stm.hGlobal));
        if (hDrop != NULL)
        {
            // Determine how many files are involved in this operation.
            UINT nFiles = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
            if (nFiles != 0)
            {
                m_selectedFiles.clear();

                //Enumerates the selected files and directories.
                for (UINT i = 0; i < nFiles; i++)
                {
                    // Get the next filename.
                    int size = DragQueryFile(hDrop, i, NULL, 0) + 1;
                    string_t str;
                    str.resize(size);
                    if (DragQueryFile(hDrop, i, &str[0], size) == 0)
                        continue;

                    m_selectedFiles.push_back(str);
                }
                hr = S_OK;
            }

            GlobalUnlock(stm.hGlobal);
        }

        ReleaseStgMedium(&stm);
    }

    // If any value other than S_OK is returned from the method, the context 
    // menu is not displayed.
    return hr;
}

Can someone advice how to get exact paths instead of targets?

Was it helpful?

Solution

(I am not sure that the following solution is absolutely right and maybe in some cases it will not work correctly but in standard case I have tested it works fine)

You must register you context menu handler under * and lnkfile. It means when user right click on shortcut QueryContextMenu will be called twice. First time for shortcut file target and second time for shortcut file itself. But there is small difference. For shortcut file target shell always pass CMF_VERBSONLY and this flag is absent for shortcut file itself. So just check this flag and add nothing if present.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top