0

I've run into a situation that is not so unique (others have been asking exact same question) Offsite similar question..

Basically, for some reason, the code in IShellExtInit::Initialize implementation that is supposed to be invoked once after each right-click on a file, ends up being invoked 4 times.

STDMETHODIMP My_ShellExtInit::Initialize (LPCITEMIDLIST pidlFolder, LPDATAOBJECT pDataObj, HKEY hProgID ) {
    FORMATETC fmt = { CF_HDROP, NULL, DVASPECT_CONTENT,
                      -1, TYMED_HGLOBAL };
    STGMEDIUM stg = { TYMED_HGLOBAL };
    HDROP     hDrop;

    if ( FAILED( pDataObj->GetData ( &fmt, &stg ) ))
        return E_INVALIDARG;

    hDrop = (HDROP) GlobalLock ( stg.hGlobal );

    if ( NULL == hDrop )
        return E_INVALIDARG;

    UINT uNumFiles = DragQueryFile ( hDrop, 0xFFFFFFFF, NULL, 0 );
    HRESULT hr = S_OK;

    if ( 0 == uNumFiles )    {
        GlobalUnlock ( stg.hGlobal );
        ReleaseStgMedium ( &stg );
        return E_INVALIDARG;
    }

    if ( 0 == DragQueryFile ( hDrop, 0, m_szFile, MAX_PATH ) )
        hr = E_INVALIDARG;

    system("echo INVOKED >> log.txt");
    // QMessageBox::warning(NULL, "Foo!", TCHARToQString(m_szFile));

    GlobalUnlock ( stg.hGlobal );
    ReleaseStgMedium ( &stg );

    return hr;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
qdot
  • 6,195
  • 5
  • 44
  • 95

1 Answers1

2

Depending on the file/type, your context menu handler is called multiple times:

  • for the file/folder itself
  • for the parent folder of the file
  • for the folder background
  • in case of a *.lnk file also for the target it points to

And if explorer shows the tree view, then that part also calls your handler.

Stefan
  • 43,293
  • 10
  • 75
  • 117
  • That's.. quite amazing :) Do you know of a good place where I can read about why/what else is lurking in there? – qdot Jul 23 '14 at 22:26
  • most of it is documented in some way. For example you have to remember that a context menu handler is not just called to show the context menu but also to find out the verbs available, or to execute a verb, or to get the command description, ... – Stefan Jul 24 '14 at 09:18
  • Hi @Stefan I have run into similar problem. Initialize is getting called twice for .lnk files. How can I restrict it, so that its called only once for .lnk file? – Teja Aug 23 '16 at 16:30