0

I'm having difficulty obtaining the list of selected files in my implementation of a ContextMenu when the target of my right-click is a shortcut. The global memory handle returns a list containing only the shortcut item, and doesn't seem to know about any of the other items I've selected.

I've noticed this behavior in a number of 3rd party apps as well, including Notepad++, WinRar and UltraEdit.

What is the correct way to obtain a list of selected files when the target of the right-click is a shortcut? This isn't an issue for the file compression functionality baked into Explorer (right click > send to > compressed zipped folder).

Edit: here is a small snippet of my current method. pida->cidl becomes 1 when the target is a shortcut, otherwise it is the correct number of items I've selected:

CStgMedium me; 
pDataObj->GetData(&fe, &me); 

LPIDA pida = reinterpret_cast<LPIDA>(me.hGlobal); 
for(UINT i = 0; i < pida->cidl; i++) {
    ...
}
kurifu
  • 143
  • 4
  • 10

1 Answers1

0

Are we talking about IContextMenu interface here? If so, you should do it like that (mistakes possible, I'm writing without compiler):

Initialize(PCIDLIST_ABSOLUTE pidlFolder, IDataObject *pdtobj, HKEY hkeyProgID){

    FORMATETC fetc = { CF_HDROP, null, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
    STGMEDIUM stgm;

    pdtobj->GetData(&fetc, &stgm);
    UINT count = DragQueryFile(stgm.hGlobal, 0xFFFFFFFF, null, 0);
    char filename[255];

    for (int i=0; i<count; i++){
        DragQueryFile(stgm.hGlobal, i, filename, 255);
        // Here do something with i-th filename
    }
}

I omitted error checking for simplicity.

j_kubik
  • 6,062
  • 1
  • 23
  • 42
  • Sorry I meant `IShellView`, there is no dragging/dropping of any files or folders. I'm making the following calls at the moment: `CStgMedium me; pDataObj->GetData(&fe, &me); LPIDA pida = reinterpret_cast(me.hGlobal); UINT num = pida->cidl;` The pida->cidl value is the one that is changing; in the scenario above, this value becomes 1 when the target of my right click is a shortcut. If the target is not a shortcut it becomes the actual number of items selected. – kurifu Jul 11 '13 at 00:01
  • "This isn't an issue for the file compression functionality baked into Explorer" - that doesn't mean that it's known to general public how to do it. Microsoft has the tendency to reserve some APIs to their use only. – j_kubik Jul 11 '13 at 06:54
  • That's true, I'm hoping that isn't the case for this scenario, hopefully someone can confirm =( – kurifu Jul 12 '13 at 00:30
  • Any hints on what you are currently doing? A code snippet of how you try to get to the list of selected items? I expect that there is one documented way to do it (that you now use), and if it doesn't work then we are in the wilds - only internal, undocumented ways will work. – j_kubik Jul 17 '13 at 03:21
  • It's pretty much the snippet I posted in my first comment, I've added it to the original post. – kurifu Jul 23 '13 at 19:30
  • Have you tried using `DragQueryFile` instead of mapping the memory on your own using `reinterpret_cast(me.hGlobal)`? It is not drag-and-drop related, I know but this function apparently is capable of more than it's name would suggest. – j_kubik Jul 24 '13 at 07:30