ContextMenu Handler in Explorer's left panel - c++

I'm trying to write a context menu handler with c++. I would like the handler to work on all files and all folders, also those in the left pane o the explorer.
I started based on a CodeProject Project that I'm trying to adapt to my needs. I got it from here.
Until now everything seems to work as expected, except for the folders of the Explorer's left pane.
There I get an AccessViolationException. The exception occures between the right click and the display of the context menu.
I removed the code in the different functions until it works again and it seems that the problem is coming from the QueryContextMenu method.
Does someone know what I'm doing wrong?
Here is my current code that works (somewhat obvious because no item is added):
STDMETHODIMP ShellExt::Initialize (LPCITEMIDLIST pidlFolder, LPDATAOBJECT pDataObj, HKEY hProgID )
{
FORMATETC fmt = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
STGMEDIUM stg = { TYMED_HGLOBAL };
HDROP hDrop;
// Look for CF_HDROP data in the data object.
if ( FAILED( pDataObj->GetData ( &fmt, &stg ) ))
{
// Nope! Return an "invalid argument" error back to Explorer.
return E_INVALIDARG;
}
// Get a pointer to the actual data.
hDrop = (HDROP) GlobalLock ( stg.hGlobal );
// Make sure it worked.
if ( NULL == hDrop )
return E_INVALIDARG;
// Sanity check - make sure there is at least one filename.
UINT uNumFiles = DragQueryFile ( hDrop, 0xFFFFFFFF, NULL, 0 );
HRESULT hr = S_OK;
if ( 0 == uNumFiles )
{
GlobalUnlock ( stg.hGlobal );
ReleaseStgMedium ( &stg );
return E_INVALIDARG;
}
// Get the name of the first file and store it in our member variable m_szFile.
if ( 0 == DragQueryFile ( hDrop, 0, m_szFile, MAX_PATH ) )
hr = E_INVALIDARG;
GlobalUnlock ( stg.hGlobal );
ReleaseStgMedium ( &stg );
return hr;
}
STDMETHODIMP ShellExt::QueryContextMenu (HMENU hmenu, UINT uMenuIndex, UINT uidFirstCmd, UINT uidLastCmd, UINT uFlags )
{
return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, 0 );
}
STDMETHODIMP ShellExt::GetCommandString (UINT_PTR idCmd, UINT uFlags, UINT* pwReserved, LPSTR pszName, UINT cchMax)
{
return E_INVALIDARG;
}
STDMETHODIMP ShellExt::InvokeCommand (LPCMINVOKECOMMANDINFO pCmdInfo)
{
return S_OK;
}
But now if I add an item in ShellExt::QueryContextMenu the Explorer's left pane throws the exception, while files and folders in the right pane work great.
I tried the following two version I found on the internet, but both have the same problem:
STDMETHODIMP ShellExt::QueryContextMenu (HMENU hmenu, UINT uMenuIndex, UINT uidFirstCmd, UINT uidLastCmd, UINT uFlags )
{
// If the flags include CMF_DEFAULTONLY then we shouldn't do anything.
if ( uFlags & CMF_DEFAULTONLY )
return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, 0 );
InsertMenu ( hmenu, uMenuIndex, MF_BYPOSITION, uidFirstCmd, _T("Test Item") );
return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, 1 );
}
and
STDMETHODIMP ShellExt::QueryContextMenu (HMENU hmenu, UINT uMenuIndex, UINT uidFirstCmd, UINT uidLastCmd, UINT uFlags )
{
// If the flags include CMF_DEFAULTONLY then we shouldn't do anything.
if ( uFlags & CMF_DEFAULTONLY )
return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, 0 );
UINT uID = uidFirstCmd;
UINT pos = uMenuIndex;
MENUITEMINFO mii = { sizeof(mii) };
mii.fMask = MIIM_STRING | MIIM_FTYPE | MIIM_ID | MIIM_STATE;
mii.fType = MFT_STRING;
mii.dwTypeData = _T("Menu 1");
mii.fState = MFS_ENABLED;
mii.wID = uID++;
if (!InsertMenuItem(hmenu, pos++, TRUE, &mii))
{
return HRESULT_FROM_WIN32(GetLastError());
}
return MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, uID - uidFirstCmd);
}
Parameters I get in QueryContextMenu:
- hmenu = 0x0000000001480405
- uMenuIndex = 3
- uidFirstCmd = 148
- uidLastCmd = 32762
- uFlags = 1044
Exception Details.
The code in QueryContextMenu and GetCommandString gets executed. The exception occures somewhere after this but still before the contextmenu gets visible. Unfortunaly the details do not mean very much to me:
Exception thrown at 0x00007FF9B1C4C613 (shell32.dll) in explorer.exe: 0xC0000005: Access violation reading location 0x00007FF9776D9868. occurred
Stacktrace:
shell32.dll!00007ff9b1c4c613()
ExplorerFrame.dll!00007ff98d24993a()
ExplorerFrame.dll!00007ff98d2c69a4()
ExplorerFrame.dll!00007ff98d24de6c()
ExplorerFrame.dll!00007ff98d251286()
ExplorerFrame.dll!00007ff98d1fbad0()
ExplorerFrame.dll!00007ff98d1ad5b3()
ExplorerFrame.dll!00007ff98d180f2e()
user32.dll!00007ff9b079ca66()
user32.dll!00007ff9b079c34b()
comctl32.dll!00007ff99d78b0da()
comctl32.dll!00007ff99d78b017()
ExplorerFrame.dll!00007ff98d187521()
ExplorerFrame.dll!00007ff98d17d2e0()
comctl32.dll!00007ff99d78b0da()
comctl32.dll!00007ff99d78aef2()
user32.dll!00007ff9b079ca66()
user32.dll!00007ff9b079c34b()
duser.dll!00007ff98106dff5()
atlthunk.dll!00007ff983061208()
user32.dll!00007ff9b079ca66()
user32.dll!00007ff9b079c78c()
user32.dll!00007ff9b07afa83()
ntdll.dll!00007ff9b34833a4()
win32u.dll!00007ff9b0071184()
user32.dll!00007ff9b079bfbe()
user32.dll!00007ff9b079be38()
comctl32.dll!00007ff99d77a099()
comctl32.dll!00007ff99d80b556()
comctl32.dll!00007ff99d7b1302()
user32.dll!00007ff9b079ca66()
user32.dll!00007ff9b079c34b()
comctl32.dll!00007ff99d78b0da()
comctl32.dll!00007ff99d78b017()
ExplorerFrame.dll!00007ff98d1bf33b()
ExplorerFrame.dll!00007ff98d1bf27b()
comctl32.dll!00007ff99d78b0da()
comctl32.dll!00007ff99d78aef2()
user32.dll!00007ff9b079ca66()
user32.dll!00007ff9b079c582()
ExplorerFrame.dll!00007ff98d1748a3()
ExplorerFrame.dll!00007ff98d1747a9()
ExplorerFrame.dll!00007ff98d1746f6()
ExplorerFrame.dll!00007ff98d175a12()
ExplorerFrame.dll!00007ff98d1870c2()
windows.storage.dll!00007ff9af8db38c()
windows.storage.dll!00007ff9af8db045()
windows.storage.dll!00007ff9af8daf25()
SHCore.dll!00007ff9b164c315()
kernel32.dll!00007ff9b06281f4()
ntdll.dll!00007ff9b344a251()

It seems I resolved the problem. I reworked the remaining part of the project, i.e. the implementation of the IClassFactory and also the implementation of the IUnknown interface of the shellextension class, with the objective to understand what these "things" are for in that project.
While doing so I found some Bugs, uninitialized variables, casts to wrong types, etc. I also changed some counters from UINT to LONG so that I can use InterlockedIncrement instead of ++. And after that the problem seems to be gone. I can't say exactly what bug caused the AccessViolationException anymore, but at the end it was not in the QueryContextMenu method as I thought...

Related

CreateDC() method,while setting up a Printer fails for certain PRINTERs as well as in certain Windows Environment

I have installed a HP Printer and added it to my list of Printer devices.
I am trying to use the following code:
QString printerName = "HP Designjet 500 24+HPGL2 Card";
DWORD infoSize, numBytes;
HANDLE hPrinter;
bool ok = OpenPrinter( ( LPWSTR )printerName.utf16(), ( LPHANDLE )&hPrinter, 0 );
if ( !ok )
{
qErrnoWarning( "QWin32PrintEngine::initialize: OpenPrinter failed" );
return;
}
GetPrinter( hPrinter, 2, NULL, 0, &infoSize );
HGLOBAL hMem;
hMem = GlobalAlloc( GHND, infoSize );
PRINTER_INFO_2 *pInfo;
pInfo = ( PRINTER_INFO_2* )GlobalLock( hMem );
ok = GetPrinter( hPrinter, 2, ( LPBYTE )pInfo, infoSize, &numBytes );
if ( !ok )
{
qErrnoWarning( "QWin32PrintEngine::initialize: GetPrinter failed" );
}
DEVMODE *devMode;
devMode = pInfo->pDevMode;
HDC hdc = NULL;
hdc = CreateDC( NULL, ( LPCWSTR )printerName.utf16(), 0, devMode );
Now,the CreateDC() method fails.I even tried to return the Error using GetLastError() method, and it returned as "203",which corresponds to "ERROR_ENVVAR_NOT_FOUND".
I am completely clueless as of now.
I would be really glad,if someone can help me regarding this.
Thanks in Advance.

Shell Extension: IShellExtInit::Initialize called 4 times

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;
}
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.

Obtaining the Excel.Application IDispatch* within a dll that's been loaded into Excel

Does anyone know how to get hold of the Excel.Application IDispatch* pointer associated with an excel process into which an dll has been loaded?
A key thing here is that the process is excel.exe, and the pointer I need must belong to that process. Using the Running Object Table will not fly since Excel only registers its first instance with that.
I'm hoping there is some low-level COM trickery, but I'm not an expert in that field.
EDITED II Code is under the WTFPL license version 2.
EDITED: Add PID parameter to allow filtering when several Excel processes are currently running, as per comment suggestion from #EricBrown.
I managed to get a working IDispatch* to an Excel "Application" object without using the ROT. The trick is to use MSAA. My code works as a stand alone console application, but I think that if the code is executed in an Excel process, via DLL Injection, it MAY works fine. You may have to be in a dedicated thread. Let me know if you want me to push the expriment to the DLL injection level.
Tested OK on Window7 64b, with a UNICODE builds (32 bits and 64 bits).
Excel version 2010 64 bits (version "14")
I get the IDispatch via the "application" property from an "Worksheet" object. Consequence: there must be an opened worksheet. In order to find the good MSSA Window, I need the class name of the Top Level Excel Frame Window. In Excel 2010, it's "XLMAIN". The class name for worksheets is "EXCEL7" and that seems to be a "standard".
I was not able to directly get a working IDispatch* from the main Excel Window, but have not tried very hard. That may involve #import with a automation DLL from Excel, in order to QueryInterface the IDispatch that MSAA gives for the main Window (that IDispatch is NOT for an Application object)
#include <atlbase.h>
#pragma comment( lib, "Oleacc.lib" )
HRESULT GetExcelAppDispatch( CComPtr<IDispatch> & spIDispatchExcelApp, DWORD dwExcelPID ) {
struct ew {
struct ep {
_TCHAR* pszClassName;
DWORD dwPID;
HWND hWnd;
};
static BOOL CALLBACK ewp( HWND hWnd, LPARAM lParam ) {
TCHAR szClassName[ 64 ];
if ( GetClassName( hWnd, szClassName, 64 ) ) {
ep* pep = reinterpret_cast<ep*>( lParam );
if ( _tcscmp( szClassName, pep->pszClassName ) == 0 ) {
if ( pep->dwPID == 0 ) {
pep->hWnd = hWnd;
return FALSE;
} else {
DWORD dwPID;
if ( GetWindowThreadProcessId( hWnd, &dwPID ) ) {
if ( dwPID == pep->dwPID ) {
pep->hWnd = hWnd;
return FALSE;
}
}
}
}
}
return TRUE;
}
};
ew::ep ep;
ep.pszClassName = _TEXT( "XLMAIN" );
ep.dwPID = dwExcelPID;
ep.hWnd = NULL;
EnumWindows( ew::ewp, reinterpret_cast<LPARAM>( &ep ) );
HWND hWndExcel = ep.hWnd;
if ( ep.hWnd == NULL ) {
printf( "Can't Find Main Excel Window with EnumWindows\n" );
return -1;
}
ep.pszClassName = _TEXT( "EXCEL7" );
ep.dwPID = 0;
ep.hWnd = NULL;
EnumChildWindows( hWndExcel, ew::ewp, reinterpret_cast<LPARAM>( &ep ) );
HWND hWndWorkSheet = ep.hWnd;
if ( hWndWorkSheet == NULL ) {
printf( "Can't Find a WorkSheet with EnumChildWindows\n" );
return -1;
}
CComPtr<IDispatch> spIDispatchWorkSheet;
HRESULT hr = AccessibleObjectFromWindow( hWndWorkSheet, OBJID_NATIVEOM, IID_IDispatch,
reinterpret_cast<void**>( &spIDispatchWorkSheet ) );
if ( FAILED( hr ) || ( spIDispatchWorkSheet == 0 ) ) {
printf( "AccessibleObjectFromWindow Failed\n" );
return hr;
}
CComVariant vExcelApp;
hr = spIDispatchWorkSheet.GetPropertyByName( CComBSTR( "Application" ), &vExcelApp );
if ( SUCCEEDED( hr ) && ( vExcelApp.vt == VT_DISPATCH ) ) {
spIDispatchExcelApp = vExcelApp.pdispVal;
return S_OK;
}
return hr;
}
int _tmain(int argc, _TCHAR* argv[])
{
DWORD dwExcelPID = 0;
if ( argc > 1 ) dwExcelPID = _ttol( argv[ 1 ] );
HRESULT hr = CoInitialize( NULL );
bool bCoUnInitializeTodo = false;
if ( SUCCEEDED( hr ) ) {
bCoUnInitializeTodo = true;
CComPtr<IDispatch> spDispatchExcelApp;
hr = GetExcelAppDispatch( spDispatchExcelApp, dwExcelPID );
if ( SUCCEEDED( hr ) && spDispatchExcelApp ) {
CComVariant vExcelVer;
hr = spDispatchExcelApp.GetPropertyByName( CComBSTR( "Version" ), &vExcelVer );
if ( SUCCEEDED( hr ) && ( vExcelVer.vt == VT_BSTR ) ) {
wprintf( L"Excel Version is %s\n", vExcelVer.bstrVal );
}
}
}
if ( bCoUnInitializeTodo ) CoUninitialize();
return 0;
}
You should be able to find out how to do this by reviewing the code in ExcelDNA. This project contains code that hooks back into Excel from the extension library. The code is likely to be more elaborate that you need, but will implement the reference you require.
This is how I do it: (acknowledge #manuell). dispatch_wrapper is a class, here is the constructor to set m_disp_application:
dispatch_wrapper(void)
{
DWORD target_process_id = ::GetProcessId(::GetCurrentProcess());
if (getProcessName() == "excel.exe"){
HWND hwnd = ::FindWindowEx(0, 0, "XLMAIN", NULL);
while (hwnd){
DWORD process_id;
::GetWindowThreadProcessId(hwnd, &process_id);
if (process_id == target_process_id){
HWND hwnd_desk = ::FindWindowEx(hwnd, 0, "XLDESK", NULL);
HWND hwnd_7 = ::FindWindowEx(hwnd_desk, 0, "EXCEL7", NULL);
IDispatch* p = nullptr;
if (SUCCEEDED(::AccessibleObjectFromWindow(hwnd_7, OBJID_NATIVEOM, IID_IDispatch, (void**)&p))){
LPOLESTR name[1] = {L"Application"};
DISPID dispid;
if (SUCCEEDED(p->GetIDsOfNames(IID_NULL, name, 1U, LOCALE_SYSTEM_DEFAULT, &dispid))){
CComVariant v;
DISPPARAMS dp;
::memset(&dp, NULL, sizeof(DISPPARAMS));
EXCEPINFO ei;
::memset(&ei, NULL, sizeof(EXCEPINFO));
if (SUCCEEDED(p->Invoke(dispid, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &dp, &v, &ei, NULL))){
if (v.vt == VT_DISPATCH){
m_disp_application = v.pdispVal;
m_disp_application->AddRef();
return;
}
}
}
}
}
hwnd = ::FindWindowEx(0, hwnd, "XLMAIN", NULL);
}
}
m_disp_application = nullptr;
}
getProcessName() returns lower case.
Because Office applications register their documents in the ROT, you can attach to instances beside the first one (which is already in the ROT) by getting IDispatch for documents in the ROT, then you can use document.Application.hwnd (this is VBA, you need to translate to IDispatch::GetIDsOfNames and IDispatch::Invoke with DISPATCH_PROPERTYGET) to get the window handles of all Excel instances.
Now you have a mapping between IDispatch and Windows handles of all Excel instances, it is time to find your own Excel instance. You can call GetWindowThreadProcessId on the window handles to get the process ids, then compare to your own process id returned by GetCurrentProcessId to see which excel window belongs to your current process, and look up in the HWND to IDispatch mapping to find your current Excel application's IDispatch interface.

Cascade submenus inside context menu shell extension with c++

Hello i'm trying to get cascade window inside context menu in shell extension. I add two submenus inside context menu of .dll extension, but want to make one cascade submenu (i like to open first menu and after clicking on some menu inside, want to open next submenu).
How to get cascade submenus from this code, where did i make mistake?
// OpenWithCtxMenuExt.cpp : Implementation of COpenWithCtxMenuExt
#include "stdafx.h"
#include "OpenWithExt.h"
#include "OpenWithCtxMenuExt.h"
#pragma comment(lib,"shlwapi")
/////////////////////////////////////////////////////////////////////////////
// COpenWithCtxMenuExt
HRESULT COpenWithCtxMenuExt::Initialize ( LPCITEMIDLIST pidlFolder,
LPDATAOBJECT pDataObj,
HKEY hProgID )
{
FORMATETC fmt = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
STGMEDIUM stg = { TYMED_HGLOBAL };
HDROP hDrop;
// Look for CF_HDROP data in the data object.
if ( FAILED( pDataObj->GetData ( &fmt, &stg )))
{
// Nope! Return an "invalid argument" error back to Explorer.
return E_INVALIDARG;
}
// Get a pointer to the actual data.
hDrop = (HDROP) GlobalLock ( stg.hGlobal );
// Make sure it worked.
if ( NULL == hDrop )
return E_INVALIDARG;
// Sanity check - make sure there is at least one filename.
UINT uNumFiles = DragQueryFile ( hDrop, 0xFFFFFFFF, NULL, 0 );
if ( 0 == uNumFiles )
{
GlobalUnlock ( stg.hGlobal );
ReleaseStgMedium ( &stg );
return E_INVALIDARG;
}
HRESULT hr = S_OK;
// Get the name of the first file and store it in our member variable m_szFile.
if ( 0 == DragQueryFile ( hDrop, 0, m_szSelectedFile, MAX_PATH ))
hr = E_INVALIDARG;
else
{
// Quote the name if it contains spaces (needed so the cmd line is built properly)
PathQuoteSpaces ( m_szSelectedFile );
}
GlobalUnlock ( stg.hGlobal );
ReleaseStgMedium ( &stg );
return hr;
}
HRESULT COpenWithCtxMenuExt::QueryContextMenu ( HMENU hmenu, UINT uMenuIndex,
UINT uidFirstCmd, UINT uidLastCmd,
UINT uFlags )
{
// If the flags include CMF_DEFAULTONLY then we shouldn't do anything.
if ( uFlags & CMF_DEFAULTONLY )
return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, 0 );
// First, create and populate a submenu.
HMENU hSubmenu = CreatePopupMenu();
HMENU hSubmenu1 = CreatePopupMenu();
UINT uID = uidFirstCmd;
InsertMenu ( hSubmenu, 0, MF_BYPOSITION, uID++, _T("&Notepad") );
InsertMenu ( hSubmenu, 1, MF_BYPOSITION, uID++, _T("&Internet Explorer") );
InsertMenu ( hSubmenu, 2, MF_BYPOSITION, uID++, _T("&Mspaint") );
InsertMenu ( hSubmenu, 3, MF_BYPOSITION, uID++, _T("&Pop") );
// provjeriti uID da se ne zbraja
InsertMenu ( hSubmenu1, 0, MF_BYPOSITION, uID++, _T("&Notepad") );
InsertMenu ( hSubmenu1, 1, MF_BYPOSITION, uID++, _T("&Mspaint") );
// Insert the submenu into the ctx menu provided by Explorer.
MENUITEMINFO mii = { sizeof(MENUITEMINFO) };
mii.fMask = MIIM_SUBMENU | /*MIIM_STRING*/ 0x00000040 | MIIM_ID;
mii.wID = uID++;
mii.hSubMenu = hSubmenu;
mii.dwTypeData = _T("C&P Open With");
InsertMenuItem ( hmenu, uMenuIndex, TRUE, &mii );
// Insert the submenu into the ctx menu provided by Explorer.
MENUITEMINFO mii1 = { sizeof(MENUITEMINFO) };
mii1.fMask = MIIM_SUBMENU | /*MIIM_STRING*/ 0x00000040 | MIIM_ID;
mii1.wID = uID++;
mii1.hSubMenu = hSubmenu;
mii1.dwTypeData = _T("C&P pod_folder");
InsertMenuItem ( hmenu, uMenuIndex, TRUE, &mii1 );
return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, uID - uidFirstCmd );
}
HRESULT COpenWithCtxMenuExt::GetCommandString ( UINT idCmd, UINT uFlags,
UINT* pwReserved, LPSTR pszName,
UINT cchMax )
{
USES_CONVERSION;
// Check idCmd, it must be 0 or 1 since we have two menu items.
if ( idCmd > 3 )
return E_INVALIDARG;
// If Explorer is asking for a help string, copy our string into the
// supplied buffer.
if ( uFlags & GCS_HELPTEXT )
{
LPCTSTR szNotepadText = _T("Open the selected file in Notepad");
LPCTSTR szIEText = _T("Open the selected file in Internet Explorer");
LPCTSTR szPintText = _T("Open the selected file with Mspaint");
LPCTSTR szPopText = _T("Popout");
LPCTSTR szNotepad1Text = _T("Open the selected file in Notepad");
LPCTSTR szPint1Text = _T("Open the selected file with Mspaint");
//LPCTSTR pszText = (0 == idCmd) ? szNotepadText : szIEText;
LPCTSTR pszText;
if(idCmd == 0){
pszText = szNotepadText;
}
if(idCmd == 1){
pszText = szIEText;
}
if(idCmd == 2){
pszText = szPintText;
}
if(idCmd == 3){
pszText = szPopText;
}
if(idCmd == 4){
pszText = szNotepad1Text;
}
if(idCmd == 5){
pszText = szPint1Text;
}
if ( uFlags & GCS_UNICODE )
{
// We need to cast pszName to a Unicode string, and then use the
// Unicode string copy API.
lstrcpynW ( (LPWSTR) pszName, T2CW(pszText), cchMax );
}
else
{
// Use the ANSI string copy API to return the help string.
lstrcpynA ( pszName, T2CA(pszText), cchMax );
}
return S_OK;
}
return E_INVALIDARG;
}
HRESULT COpenWithCtxMenuExt::InvokeCommand ( LPCMINVOKECOMMANDINFO pCmdInfo )
{
// If lpVerb really points to a string, ignore this function call and bail out.
if ( 0 != HIWORD( pCmdInfo->lpVerb ))
return E_INVALIDARG;
// Get the command index.
switch ( LOWORD( pCmdInfo->lpVerb ))
{
case 0:
{
ShellExecute ( pCmdInfo->hwnd, _T("open"), _T("notepad.exe"),
m_szSelectedFile, NULL, SW_SHOW );
return S_OK;
}
break;
case 1:
{
ShellExecute ( pCmdInfo->hwnd, _T("open"), _T("iexplore.exe"),
m_szSelectedFile, NULL, SW_SHOW );
return S_OK;
}
break;
case 2:
{
ShellExecute ( pCmdInfo->hwnd, _T("open"), _T("mspaint.exe"),
m_szSelectedFile, NULL, SW_SHOW );
return S_OK;
}
break;
case 3:
{
ShellExecute ( pCmdInfo->hwnd, _T("open"), _T("mspaint.exe"),
m_szSelectedFile, NULL, SW_SHOW );
return S_OK;
}
break;
case 4:
{
ShellExecute ( pCmdInfo->hwnd, _T("open"), _T("notepad.exe"),
m_szSelectedFile, NULL, SW_SHOW );
return S_OK;
}
break;
case 5:
{
ShellExecute ( pCmdInfo->hwnd, _T("open"), _T("mspaint.exe"),
m_szSelectedFile, NULL, SW_SHOW );
return S_OK;
}
break;
default:
return E_INVALIDARG;
break;
}
}
After some time found solution for making cascade context menu inside of existing context menu ... here is code:
// OpenWithCtxMenuExt.cpp : Implementation of COpenWithCtxMenuExt
#include "stdafx.h"
#include "OpenWithExt.h"
#include "OpenWithCtxMenuExt.h"
#pragma comment(lib,"shlwapi")
/////////////////////////////////////////////////////////////////////////////
// COpenWithCtxMenuExt
HRESULT COpenWithCtxMenuExt::Initialize ( LPCITEMIDLIST pidlFolder,
LPDATAOBJECT pDataObj,
HKEY hProgID )
{
FORMATETC fmt = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
STGMEDIUM stg = { TYMED_HGLOBAL };
HDROP hDrop;
// Look for CF_HDROP data in the data object.
if ( FAILED( pDataObj->GetData ( &fmt, &stg )))
{
// Nope! Return an "invalid argument" error back to Explorer.
return E_INVALIDARG;
}
// Get a pointer to the actual data.
hDrop = (HDROP) GlobalLock ( stg.hGlobal );
// Make sure it worked.
if ( NULL == hDrop )
return E_INVALIDARG;
// Sanity check - make sure there is at least one filename.
UINT uNumFiles = DragQueryFile ( hDrop, 0xFFFFFFFF, NULL, 0 );
if ( 0 == uNumFiles )
{
GlobalUnlock ( stg.hGlobal );
ReleaseStgMedium ( &stg );
return E_INVALIDARG;
}
HRESULT hr = S_OK;
// Get the name of the first file and store it in our member variable m_szFile.
if ( 0 == DragQueryFile ( hDrop, 0, m_szSelectedFile, MAX_PATH ))
hr = E_INVALIDARG;
else
{
// Quote the name if it contains spaces (needed so the cmd line is built properly)
PathQuoteSpaces ( m_szSelectedFile );
}
GlobalUnlock ( stg.hGlobal );
ReleaseStgMedium ( &stg );
return hr;
}
HRESULT COpenWithCtxMenuExt::QueryContextMenu ( HMENU hmenu, UINT uMenuIndex,
UINT uidFirstCmd, UINT uidLastCmd,
UINT uFlags )
{
// If the flags include CMF_DEFAULTONLY then we shouldn't do anything.
if ( uFlags & CMF_DEFAULTONLY )
return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, 0 );
// First, create and populate a submenu.
HMENU hSubmenu = CreatePopupMenu();
HMENU hSub = CreatePopupMenu();
UINT uID = uidFirstCmd;
InsertMenu ( hSubmenu, 0, MF_BYPOSITION, uID++, _T("&Notepad") );
InsertMenu ( hSubmenu, 1, MF_BYPOSITION, uID++, _T("&Internet Explorer") );
InsertMenu ( hSubmenu, 2, MF_BYPOSITION, uID++, _T("&Mspaint") );
InsertMenu ( hSubmenu, 3, MF_BYPOSITION, uID++, _T("&Pop") );
InsertMenu ( hSub, 4, MF_BYPOSITION, uID++, _T("&Case") );
InsertMenu ( hSub, 5, MF_BYPOSITION, uID++, _T("&Case") );
// Insert the submenu into the ctx menu provided by Explorer.
MENUITEMINFO mii = { sizeof(MENUITEMINFO) };
mii.fMask = MIIM_SUBMENU | /*MIIM_STRING*/ 0x00000040 | MIIM_ID;
mii.wID = uID++;
mii.hSubMenu = hSubmenu;
mii.dwTypeData = _T("Open With&x");
InsertMenuItem ( hmenu, uMenuIndex, TRUE, &mii );
mii.hSubMenu = hSub;
mii.dwTypeData = _T("Novi Subm&enu");
InsertMenuItem ( hSubmenu, uMenuIndex, TRUE, &mii );
return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, uID - uidFirstCmd );
}
HRESULT COpenWithCtxMenuExt::GetCommandString ( UINT idCmd, UINT uFlags,
UINT* pwReserved, LPSTR pszName,
UINT cchMax )
{
USES_CONVERSION;
// Check idCmd, it must be 0 or 1 since we have two menu items.
if ( idCmd > 4 )
return E_INVALIDARG;
// If Explorer is asking for a help string, copy our string into the
// supplied buffer.
if ( uFlags & GCS_HELPTEXT )
{
LPCTSTR szNotepadText = _T("Open the selected file in Notepad");
LPCTSTR szIEText = _T("Open the selected file in Internet Explorer");
LPCTSTR szPintText = _T("Open the selected file with Mspaint");
LPCTSTR szPopText = _T("Popout");
//LPCTSTR pszText = (0 == idCmd) ? szNotepadText : szIEText;
LPCTSTR pszText;
if(idCmd == 0){
pszText = szNotepadText;
}
if(idCmd == 1){
pszText = szIEText;
}
if(idCmd == 2){
pszText = szPintText;
}
if(idCmd == 3){
pszText = szPopText;
}
if(idCmd == 4){
pszText = szPopText;
}
if ( uFlags & GCS_UNICODE )
{
// We need to cast pszName to a Unicode string, and then use the
// Unicode string copy API.
lstrcpynW ( (LPWSTR) pszName, T2CW(pszText), cchMax );
}
else
{
// Use the ANSI string copy API to return the help string.
lstrcpynA ( pszName, T2CA(pszText), cchMax );
}
return S_OK;
}
return E_INVALIDARG;
}
HRESULT COpenWithCtxMenuExt::InvokeCommand ( LPCMINVOKECOMMANDINFO pCmdInfo )
{
// If lpVerb really points to a string, ignore this function call and bail out.
if ( 0 != HIWORD( pCmdInfo->lpVerb ))
return E_INVALIDARG;
// Get the command index.
switch ( LOWORD( pCmdInfo->lpVerb ))
{
case 0:
{
ShellExecute ( pCmdInfo->hwnd, _T("open"), _T("notepad.exe"),
m_szSelectedFile, NULL, SW_SHOW );
return S_OK;
}
break;
case 1:
{
ShellExecute ( pCmdInfo->hwnd, _T("open"), _T("iexplore.exe"),
m_szSelectedFile, NULL, SW_SHOW );
return S_OK;
}
break;
case 2:
{
ShellExecute ( pCmdInfo->hwnd, _T("open"), _T("mspaint.exe"),
m_szSelectedFile, NULL, SW_SHOW );
return S_OK;
}
break;
case 4:
{
MessageBox(0, "New command from sub menu", "Case 4", 0);
return S_OK;
}
break;
case 5:
{
MessageBox(0, "New second command from sub menu", "Case 5", 0);
return S_OK;
}
break;
default:
return E_INVALIDARG;
break;
}
}

Windows 7 - Shell Extension dll Initialize method called twice on explorer left pane

I have a c++ shell extension dll. The Initialize method is called twice, if I click on the explorer window left pane tree view folders. But if I click any folders on the explorer window right pane, the Initialize method called once.
The issue is my newly added menu items shows twice in the context menu, if I click on the left pane tree view.
I am wondering, is it a window functionality?
I have commented all my implementation and tested with the below code:
IFACEMETHODIMP CMyContextMenu::QueryContextMenu(HMENU hmenu, UINT /*uIndex*/, UINT cmdFirst, UINT /*uidCmdLast*/, UINT /*uFlags*/ )
{
UINT cmdId = uidCmdFirst;
OutputDebugString(L"QueryContextMenu");
return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, cmdId - mdFirst );
}
IFACEMETHODIMP CMyContextMenu::Initialize(LPCITEMIDLIST pidlFolder, LPDATAOBJECT pDO, HKEY /*hkeyProgID*/)
{
OutputDebugString(L"Initialize");
return S_OK;
}
When I click on left pane, the DebugViewr output is:
Initialize
QueryContextMenu
Initialize
QueryContextMenu
NoRemove Directory
{
NoRemove Background
{
NoRemove ShellEx
{
NoRemove ContextMenuHandlers
{
ForceRemove myContext = s '{AE843198-3C5D-4EA6-B74F-7A41FC91D7FF}'
}
}
}
}
NoRemove Directory
{
NoRemove ShellEx
{
NoRemove ContextMenuHandlers
{
ForceRemove myContext = s '{AE843198-3C5D-4EA6-B74F-7A41FC91D7FF}'
}
}
}
The above registry entry is causing this issue in Win 7, If I remove "NoRemove Background", the context menu will be displayed once in tree view. But if I click on folder empty area Initialize method will not be invoked.
I am posting a working example from my real program (an application specific code omitted for clarity). Please try it.
STDMETHODIMP CShlExtExample::Initialize (
LPCITEMIDLIST pidlFolder, LPDATAOBJECT pDataObj, HKEY hProgID )
{
FORMATETC fmt = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
STGMEDIUM stg = { TYMED_HGLOBAL };
HDROP hDrop;
// Look for CF_HDROP data in the data object.
if ( FAILED( pDataObj->GetData ( &fmt, &stg ) ))
{
// Return an "invalid argument" error.
return E_INVALIDARG;
}
// Get a pointer to the actual data.
hDrop = (HDROP) GlobalLock ( stg.hGlobal );
if ( NULL == hDrop )
return E_INVALIDARG;
// Make sure there is at least one file to show menu for.
UINT uNumFiles = DragQueryFile ( hDrop, 0xFFFFFFFF, NULL, 0 );
HRESULT hr = S_OK;
if ( 0 == uNumFiles )
{
GlobalUnlock ( stg.hGlobal );
ReleaseStgMedium ( &stg );
return E_INVALIDARG;
}
// Application specific code.
GlobalUnlock ( stg.hGlobal );
ReleaseStgMedium ( &stg );
return hr;
}
STDMETHODIMP CShlExtExample::QueryContextMenu (
HMENU hmenu, UINT uMenuIndex, UINT uidFirstCmd,
UINT uidLastCmd, UINT uFlags )
{
// If the flags include CMF_DEFAULTONLY then do nothing.
if ( uFlags & CMF_DEFAULTONLY )
return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, 0 );
InsertMenu ( hmenu, uMenuIndex, MF_BYPOSITION, uidFirstCmd, _T("Test Item") );
return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, 1 );
}