Shell Extension: IShellExtInit::Initialize called 4 times

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++
shell
com
activex
shell-extensions
asked on Stack Overflow Jul 22, 2014 by qdot • edited Nov 8, 2018 by Cœur

1 Answer

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.

answered on Stack Overflow Jul 23, 2014 by Stefan

User contributions licensed under CC BY-SA 3.0