IContextMenu::InvokeCommand error Code 0x80270000 on windows 7

1

i have the following code to delete the file. this code is work fine on winXP. but when i run on windows 7 it will not delete the file. i get the Error code 0x80270000 when InvokeCommand statement is executed.

HRESULT hr = m_psfCurFolder->GetUIObjectOf(AfxGetMainWnd()->m_hWnd,
    uiCount, 
    (LPCITEMIDLIST*)aPidls, 
    IID_IContextMenu, 
    NULL, 
    (LPVOID*)&pcm);

    if (SUCCEEDED (hr))
    {
        HMENU hPopup = CreatePopupMenu();
        hr = pcm->QueryContextMenu(hPopup, 0, 1, 0x7fff, CMF_NORMAL);

        if (SUCCEEDED (hr))
        {
                CMINVOKECOMMANDINFO cmi;
                cmi.cbSize = sizeof(CMINVOKECOMMANDINFO);
                cmi.fMask =   CMIC_MASK_ASYNCOK;
                cmi.hwnd = (HWND) GetParent();
                cmi.lpVerb = (LPCSTR)(INT_PTR)(ID_MENU_DELETE - 1);
                cmi.lpParameters = NULL;
                cmi.lpDirectory = NULL;
                cmi.nShow = SW_SHOWNORMAL;
                cmi.dwHotKey = 0;
                cmi.hIcon = NULL;

                BOOL bUndoDel = FALSE;
                if (!(GetKeyState(VK_SHIFT) & SHIFTED))
                {
                    bUndoDel = TRUE;
                }
                hr = pcm->InvokeCommand (&cmi);


                if (FAILED(hr))
                {
                    _com_error error(hr);
                    LPCTSTR errorText = error.ErrorMessage();

                    AfxMessageBox(_T("Unable to perform action"));
                }
}
c++
asked on Stack Overflow Apr 25, 2013 by Abdul jalil

1 Answer

1

I had discovered the same problem and posted the details and solution at Microsoft Visual Studio Language forum and Microsoft Connect.

In a nutshell, this line...

    cmi.hwnd = (HWND) GetParent();

...should be replaced with:

    cmi.hwnd = GetParent()->GetSafeHwnd();

Even though the casting of a CWnd* to an HWND yields an identical numerical value, for some unknown reason however, it yields different results between Windows XP (where it works) and Windows 7 (where it doesn't). Using GetSafeHwnd() fixes the problem so that the 'Delete File' confirmation dialog box now is displayed. It also works on Windows XP.

answered on Stack Overflow Jul 16, 2013 by avogler

User contributions licensed under CC BY-SA 3.0