Search code examples
contextmenuwindows-shell

Create windows right click context menu for SPECIFIC folders


How can I create a context menu that appears for files/folders inside a particular folder.
Say there is a directory "D:\RandomCodes"
How do I create a custom context menu item "Open in MyApp" for any file/folder inside this? This menu item should not appear for any other directory. I know if I add the entry in HKCR/Directory/Shell, it'll work, but then it'll appear for all files and folders everywhere. Please guide me through this.


Solution

  • Is possible modifing your code for IShellExtInit:

        STDMETHODIMP CShellExt::Initialize(LPCITEMIDLIST pidl,LPDATAOBJECT pDataObj,HKEY hk)
        {
        // Initialize can be called more than once
    
        // If Initialize has already been called, release the old
        // IDataObject pointer.
        if (m_pDataObj)
        { 
            m_pDataObj->Release(); 
        }
    
        // If a data object pointer was passed in, save it and
        // extract the file name. 
        if (pDataObj == NULL)
            return E_INVALIDARG;
    
            m_pDataObj = pDataObj; 
            pDataObj->AddRef(); 
    
            STGMEDIUM   medium;
            FORMATETC   fe = {CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
            UINT        uCount;
    
            HRESULT hr = pDataObj->GetData(&fe, &medium);
            if (FAILED(hr))
                return E_INVALIDARG;
    
            // save the file name
            if (DragQueryFile((HDROP) medium.hGlobal, 0xFFFFFFFF, NULL, 0)==1) 
            {
                DragQueryFile((HDROP) medium.hGlobal, 0, m_szFile, 
                    sizeof(m_szFile));
    
                if (lstrcmpi(m_szFile, "D:\\RandomCodes") == 0) 
                {
                    hr = NOERROR;
                }
                else 
                    hr = E_INVALIDARG;
            }
            else
                hr = E_INVALIDARG;
    
            ReleaseStgMedium(&medium);
    
            return hr;
    
    }