Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1141 lines
33 KiB

  1. #include "shellprv.h"
  2. #pragma hdrstop
  3. #include <dpa.h>
  4. #include "ids.h"
  5. #include "idlcomm.h"
  6. #include "recdocs.h"
  7. #include "datautil.h"
  8. #include "mtpt.h"
  9. #include <cowsite.h>
  10. typedef struct _DKAITEM { // dkai
  11. HKEY hk;
  12. TCHAR sz[CCH_KEYMAX];
  13. } DKAITEM, *PDKAITEM;
  14. typedef const DKAITEM * PCDKAITEM;
  15. class CDKA : public CDSA<DKAITEM>
  16. {
  17. public:
  18. ~CDKA();
  19. UINT AddKeys(HKEY hk, LPCTSTR pszSubKey, PCTSTR pszDefaultOrder);
  20. PCTSTR ExposeName(int id)
  21. { return GetItemPtr(id)->sz; }
  22. HKEY ExposeKey(int id)
  23. { return GetItemPtr(id)->hk; }
  24. HRESULT GetValue(int id,
  25. PCTSTR pszSubKey,
  26. PCTSTR pszValue,
  27. DWORD *pdwType,
  28. void *pvData,
  29. DWORD *pcbData);
  30. BOOL DeleteItem(int id);
  31. BOOL DeleteAllItems();
  32. void Reset()
  33. { if ((HDSA)this) DestroyCallback(_ReleaseItem, NULL); }
  34. BOOL HasDefault(HKEY hkProgid);
  35. protected:
  36. BOOL _AppendItem(HKEY hk, PDKAITEM pdkai);
  37. void _AddOrderedKeys(HKEY hk, PCTSTR pszDefOrder);
  38. void _AddEnumKeys(HKEY hk);
  39. static int CALLBACK _ReleaseItem(PDKAITEM pdkai, void *pv);
  40. protected:
  41. TRIBIT _tbHasDefault;
  42. };
  43. BOOL CDKA::_AppendItem(HKEY hk, PDKAITEM pdkai)
  44. {
  45. BOOL fRet = FALSE;
  46. // Verify that the key exists before adding it to the list
  47. if (RegOpenKeyEx(hk, pdkai->sz, 0L, KEY_READ, &pdkai->hk) == ERROR_SUCCESS)
  48. {
  49. fRet = (AppendItem(pdkai) >= 0);
  50. if (!fRet)
  51. RegCloseKey(pdkai->hk);
  52. }
  53. return fRet;
  54. }
  55. void CDKA::_AddOrderedKeys(HKEY hk, PCTSTR pszDefOrder)
  56. {
  57. // First, add the subkeys from the value of the specified key
  58. // This should never fail, since we just opened this key
  59. DKAITEM dkai;
  60. TCHAR szOrder[CCH_KEYMAX * 5];
  61. LONG cbOrder = CbFromCch(ARRAYSIZE(szOrder));
  62. *szOrder = 0;
  63. RegQueryValue(hk, NULL, szOrder, &cbOrder);
  64. if (*szOrder)
  65. {
  66. // now we must find something in this string in order to have a default
  67. _tbHasDefault = TRIBIT_FALSE;
  68. }
  69. else if (pszDefOrder)
  70. {
  71. // If there is no value, use the order requested
  72. // typically "Open" or "Explore Open" in explorer mode
  73. StrCpyN(szOrder, pszDefOrder, ARRAYSIZE(szOrder));
  74. }
  75. PTSTR psz = szOrder;
  76. while (psz && *psz)
  77. {
  78. // skip the space or comma characters
  79. while(*psz==TEXT(' ') || *psz==TEXT(','))
  80. psz++; // NLS Notes: OK to ++
  81. if (*psz)
  82. {
  83. // Search for the space or comma character
  84. LPTSTR pszNext = psz + StrCSpn(psz, TEXT(" ,"));
  85. if (*pszNext) {
  86. *pszNext++=0; // NLS Notes: OK to ++
  87. }
  88. StrCpyN(dkai.sz, psz, ARRAYSIZE(dkai.sz));
  89. if (_AppendItem(hk, &dkai))
  90. _tbHasDefault = TRIBIT_TRUE;
  91. psz = pszNext;
  92. }
  93. }
  94. }
  95. void CDKA::_AddEnumKeys(HKEY hk)
  96. {
  97. DKAITEM dkai;
  98. // Then, append the rest if they are not in the list yet.
  99. for (int i = 0; RegEnumKey(hk, i, dkai.sz, ARRAYSIZE(dkai.sz)) == ERROR_SUCCESS; i++)
  100. {
  101. // Check if the key is already in the list.
  102. for (int idsa = 0; idsa < GetItemCount(); idsa++)
  103. {
  104. PDKAITEM pdkai = GetItemPtr(idsa);
  105. if (lstrcmpi(dkai.sz, pdkai->sz)==0)
  106. break;
  107. }
  108. // we made it throug our array
  109. // so this isnt in there
  110. if (idsa == GetItemCount())
  111. _AppendItem(hk, &dkai);
  112. }
  113. }
  114. UINT CDKA::AddKeys(HKEY hkRoot, LPCTSTR pszSubKey, PCTSTR pszDefaultOrder)
  115. {
  116. UINT cKeys = GetItemCount();
  117. HKEY hk;
  118. if (ERROR_SUCCESS == RegOpenKeyEx(hkRoot, pszSubKey, 0L, KEY_READ, &hk))
  119. {
  120. _AddOrderedKeys(hk, pszDefaultOrder);
  121. _AddEnumKeys(hk);
  122. RegCloseKey(hk);
  123. }
  124. return GetItemCount() - cKeys;
  125. }
  126. int CALLBACK CDKA::_ReleaseItem(PDKAITEM pdkai, void *pv)
  127. {
  128. if (pdkai->hk)
  129. {
  130. RegCloseKey(pdkai->hk);
  131. pdkai->hk = NULL;
  132. }
  133. return 1;
  134. }
  135. CDKA::~CDKA()
  136. {
  137. Reset();
  138. }
  139. // override this DSA methods to get release
  140. BOOL CDKA::DeleteItem(int id)
  141. {
  142. PDKAITEM p = GetItemPtr(id);
  143. if (p)
  144. {
  145. _ReleaseItem(p, NULL);
  146. return CDSA<DKAITEM>::DeleteItem(id);
  147. }
  148. return FALSE;
  149. }
  150. // override this DSA methods to get release
  151. BOOL CDKA::DeleteAllItems()
  152. {
  153. EnumCallback(_ReleaseItem, NULL);
  154. return CDSA<DKAITEM>::DeleteAllItems();
  155. }
  156. HRESULT CDKA::GetValue(int id,
  157. PCTSTR pszSubKey,
  158. PCTSTR pszValue,
  159. DWORD *pdwType,
  160. void *pvData,
  161. DWORD *pcbData)
  162. {
  163. DWORD err = SHGetValue(GetItemPtr(id)->hk, pszSubKey, pszValue, pdwType, pvData, pcbData);
  164. return HRESULT_FROM_WIN32(err);
  165. }
  166. BOOL CDKA::HasDefault(HKEY hkProgid)
  167. {
  168. if (_tbHasDefault == TRIBIT_UNDEFINED)
  169. {
  170. HKEY hk;
  171. if (ERROR_SUCCESS== RegOpenKeyEx(hkProgid, L"ShellFolder", 0, MAXIMUM_ALLOWED, &hk))
  172. {
  173. // APPCOMPAT - regitems need to have the open verb - ZekeL - 30-JAN-2001
  174. // so that the IQA and ICM will behave the same,
  175. // and regitem folders will always default to
  176. // folder\shell\open unless they implement open
  177. // or specify default verbs.
  178. //
  179. _tbHasDefault = TRIBIT_FALSE;
  180. RegCloseKey(hk);
  181. }
  182. else
  183. {
  184. _tbHasDefault = TRIBIT_TRUE;
  185. }
  186. }
  187. return _tbHasDefault == TRIBIT_TRUE;
  188. }
  189. typedef HRESULT (__stdcall *LPFNADDPAGES)(IDataObject *, LPFNADDPROPSHEETPAGE, LPARAM);
  190. class CShellExecMenu : public IShellExtInit, public IContextMenu, public IShellPropSheetExt, CObjectWithSite
  191. {
  192. public:
  193. // IUnknown
  194. STDMETHODIMP QueryInterface(REFIID riid, void **ppv);
  195. STDMETHODIMP_(ULONG) AddRef(void);
  196. STDMETHODIMP_(ULONG) Release(void);
  197. // IShellExtInit
  198. STDMETHODIMP Initialize(LPCITEMIDLIST pidlFolder, IDataObject *pdtobj, HKEY hkeyProgID);
  199. // IContextMenu
  200. STDMETHODIMP QueryContextMenu(HMENU hmenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags);
  201. STDMETHODIMP InvokeCommand(LPCMINVOKECOMMANDINFO pici);
  202. STDMETHODIMP GetCommandString(UINT_PTR idCmd, UINT wFlags, UINT *pwRes, LPSTR pszName, UINT cchMax);
  203. // IShellPropSheetExt
  204. STDMETHODIMP AddPages(LPFNADDPROPSHEETPAGE, LPARAM);
  205. STDMETHODIMP ReplacePage(UINT, LPFNADDPROPSHEETPAGE, LPARAM);
  206. CShellExecMenu(LPFNADDPAGES pfnAddPages);
  207. protected: // methods
  208. ~CShellExecMenu();
  209. void _Cleanup();
  210. HRESULT _InsureVerbs(UINT idVerb = 0);
  211. UINT _VerbCount();
  212. LPCTSTR _GetVerb(UINT id);
  213. UINT _FindIndex(LPCTSTR pszVerb);
  214. DWORD _BrowseFlagsFromVerb(UINT idVerb);
  215. BOOL _GetMenuString(UINT id, BOOL fExtended, LPTSTR pszMenu, UINT cchMax);
  216. BOOL _IsExplorerMode();
  217. BOOL _SupportsType(UINT idVerb);
  218. BOOL _IsRestricted(UINT idVerb);
  219. BOOL _IsVisible(BOOL fExtended, UINT idVerb);
  220. BOOL _RemoveVerb(UINT idVerb);
  221. BOOL _VerbCanDrop(UINT idVerb, CLSID *pclsid);
  222. HRESULT _DoDrop(REFCLSID clsid, UINT idVerb, LPCMINVOKECOMMANDINFOEX pici);
  223. HRESULT _MapVerbForInvoke(CMINVOKECOMMANDINFOEX *pici, UINT *pidVerb);
  224. HRESULT _TryBrowseObject(LPCITEMIDLIST pidl, DWORD uFlags);
  225. void _DoRecentStuff(LPCITEMIDLIST pidl, LPCTSTR pszPath);
  226. HRESULT _InvokeOne(CMINVOKECOMMANDINFOEX *pici, UINT idVerb, LPCITEMIDLIST pidl);
  227. HRESULT _InvokeMany(CMINVOKECOMMANDINFOEX *pici, UINT idVerb, LPIDA pida);
  228. HRESULT _InvokeEach(LPCITEMIDLIST pidl, CMINVOKECOMMANDINFOEX *pici);
  229. HRESULT _PromptUser(CMINVOKECOMMANDINFOEX *pici, UINT idVerb, LPIDA pida);
  230. HRESULT _MapVerbForGCS(UINT_PTR idCmd, UINT uType, UINT *pidVerb);
  231. HRESULT _GetHelpText(UINT idVerb, UINT uType, LPSTR pszName, UINT cchMax);
  232. private: // members
  233. LONG _cRef;
  234. IDataObject *_pdtobj;
  235. HKEY _hkeyProgID;
  236. CDKA _dka;
  237. LPFNADDPAGES _pfnAddPages;
  238. UINT _uFlags;
  239. };
  240. CShellExecMenu::CShellExecMenu(LPFNADDPAGES pfnAddPages) : _pfnAddPages(pfnAddPages), _cRef(1)
  241. {
  242. }
  243. CShellExecMenu::~CShellExecMenu()
  244. {
  245. _Cleanup();
  246. }
  247. void CShellExecMenu::_Cleanup()
  248. {
  249. _dka.Reset();
  250. if (_hkeyProgID)
  251. {
  252. RegCloseKey(_hkeyProgID);
  253. _hkeyProgID = NULL;
  254. }
  255. ATOMICRELEASE(_pdtobj);
  256. }
  257. STDMETHODIMP CShellExecMenu::QueryInterface(REFIID riid, void **ppvObj)
  258. {
  259. static const QITAB qit[] = {
  260. QITABENT(CShellExecMenu, IShellExtInit),
  261. QITABENT(CShellExecMenu, IContextMenu),
  262. QITABENT(CShellExecMenu, IShellPropSheetExt),
  263. QITABENT(CShellExecMenu, IObjectWithSite),
  264. { 0 },
  265. };
  266. return QISearch(this, qit, riid, ppvObj);
  267. }
  268. STDMETHODIMP_(ULONG) CShellExecMenu::AddRef()
  269. {
  270. return InterlockedIncrement(&_cRef);
  271. }
  272. STDMETHODIMP_(ULONG) CShellExecMenu::Release()
  273. {
  274. if (InterlockedDecrement(&_cRef))
  275. return _cRef;
  276. delete this;
  277. return 0;
  278. }
  279. HRESULT CShellExecMenu::_InsureVerbs(UINT idVerb)
  280. {
  281. // the idVerb is the minimum verb that we need to succeed
  282. if (!(HDSA)_dka && _hkeyProgID)
  283. {
  284. // create either "open" or "explore open"
  285. if (_dka.Create(4))
  286. {
  287. _dka.AddKeys(_hkeyProgID, c_szShell, _IsExplorerMode() ? TEXT("Explore open") : TEXT("open"));
  288. // WARNING - some verbs are not valid and need to be removed
  289. for (int id = 0; id < _dka.GetItemCount(); id++)
  290. {
  291. if (_RemoveVerb(id))
  292. _dka.DeleteItem(id);
  293. }
  294. }
  295. }
  296. return ((HDSA)_dka && idVerb < (UINT)_dka.GetItemCount()) ? S_OK : E_FAIL;
  297. }
  298. // Descriptions:
  299. // This function generates appropriate menu string from the given
  300. // verb key string. This function is called if the verb key does
  301. // not have the value.
  302. BOOL _MenuString(LPCTSTR pszVerbKey, LPTSTR pszMenuString, UINT cchMax)
  303. {
  304. // Table look-up (verb key -> menu string mapping)
  305. const static struct
  306. {
  307. LPCTSTR pszVerb;
  308. UINT id;
  309. } sVerbTrans[] = {
  310. c_szOpen, IDS_MENUOPEN,
  311. c_szExplore, IDS_MENUEXPLORE,
  312. TEXT("edit"),IDS_MENUEDIT,
  313. c_szFind, IDS_MENUFIND,
  314. c_szPrint, IDS_MENUPRINT,
  315. c_szOpenAs, IDS_MENUOPEN,
  316. TEXT("runas"),IDS_MENURUNAS
  317. };
  318. for (int i = 0; i < ARRAYSIZE(sVerbTrans); i++)
  319. {
  320. if (lstrcmpi(pszVerbKey, sVerbTrans[i].pszVerb) == 0)
  321. {
  322. if (LoadString(HINST_THISDLL, sVerbTrans[i].id, pszMenuString, cchMax))
  323. return TRUE;
  324. break;
  325. }
  326. }
  327. // Worst case: Just put '&' on the top.
  328. pszMenuString[0] = TEXT('&');
  329. pszMenuString++;
  330. cchMax--;
  331. lstrcpyn(pszMenuString, pszVerbKey, cchMax);
  332. return TRUE;
  333. }
  334. // Checks to see if there is a user policy in place that disables this key,
  335. //
  336. // For example, in the registry:
  337. //
  338. // CLSID_MyComputer
  339. // +---Shell
  340. // +---Manage
  341. // (Default) = "Mana&ge"
  342. // SuppressionPolicy = REST_NOMANAGEMYCOMPUTERVERB
  343. //
  344. // (Where REST_NOMANAGEMYCOMPUTERVERB is the DWORD value of that particular policy)
  345. BOOL CShellExecMenu::_IsRestricted(UINT idVerb)
  346. {
  347. RESTRICTIONS rest;
  348. BOOL fRestrict = FALSE;
  349. if (0 == lstrcmpi(TEXT("runas"), _dka.ExposeName(idVerb)))
  350. {
  351. rest = REST_HIDERUNASVERB;
  352. fRestrict = TRUE;
  353. }
  354. else
  355. {
  356. DWORD cb = sizeof(rest);
  357. fRestrict = SUCCEEDED(_dka.GetValue(idVerb, NULL, TEXT("SuppressionPolicy"), NULL, &rest, &cb));
  358. }
  359. return fRestrict && SHRestricted(rest);
  360. }
  361. HRESULT _GetAppSource(HKEY hk, PCWSTR pszVerb, IQuerySource **ppqs)
  362. {
  363. CComPtr<IAssociationElement> spae;
  364. HRESULT hr = AssocElemCreateForKey(&CLSID_AssocShellElement, hk, &spae);
  365. if (SUCCEEDED(hr))
  366. {
  367. CComPtr<IObjectWithQuerySource> spowqsApp;
  368. hr = spae->QueryObject(AQVO_APPLICATION_DELEGATE, pszVerb, IID_PPV_ARG(IObjectWithQuerySource, &spowqsApp));
  369. if (SUCCEEDED(hr))
  370. {
  371. hr = spowqsApp->GetSource(IID_PPV_ARG(IQuerySource, ppqs));
  372. }
  373. }
  374. return hr;
  375. }
  376. BOOL CShellExecMenu::_SupportsType(UINT idVerb)
  377. {
  378. BOOL fRet = TRUE;
  379. if (SUCCEEDED(_dka.GetValue(idVerb, NULL, TEXT("CheckSupportedTypes"), NULL, NULL, NULL)))
  380. {
  381. // need to check the supported types for this application
  382. // get the first item and then check it against SupportedFileExtensions
  383. CComPtr<IShellItem> spsi;
  384. if (SUCCEEDED(DataObj_GetIShellItem(_pdtobj, &spsi)))
  385. {
  386. SFGAOF sfgao;
  387. if (S_OK == spsi->GetAttributes(SFGAO_STREAM, &sfgao))
  388. {
  389. CSmartCoTaskMem<OLECHAR> spszName;
  390. if (SUCCEEDED(spsi->GetDisplayName(SIGDN_PARENTRELATIVEPARSING, &spszName)))
  391. {
  392. PWSTR pszExt = PathFindExtension(spszName);
  393. if (*pszExt)
  394. {
  395. CComPtr<IQuerySource> spqs;
  396. if (SUCCEEDED(_GetAppSource(_hkeyProgID, _dka.ExposeName(idVerb), &spqs)))
  397. {
  398. fRet = SUCCEEDED(spqs->QueryValueExists(L"SupportedTypes", pszExt));
  399. }
  400. }
  401. }
  402. }
  403. }
  404. }
  405. return fRet;
  406. }
  407. //
  408. // LegacyDisable
  409. // LegacyDisable is set, then the verb exists only for legacy reasons, and
  410. // is actually superceded by a context menu extension or some other behavior
  411. // it there only to retain legacy behavior for external clients that require
  412. // the existence of a verb.
  413. //
  414. BOOL CShellExecMenu::_RemoveVerb(UINT idVerb)
  415. {
  416. if (SUCCEEDED(_dka.GetValue(idVerb, NULL, TEXT("LegacyDisable"), NULL, NULL, NULL)))
  417. return TRUE;
  418. if (!_SupportsType(idVerb))
  419. return TRUE;
  420. return (_IsRestricted(idVerb));
  421. }
  422. BOOL CShellExecMenu::_IsVisible(BOOL fExtended, UINT idVerb)
  423. {
  424. // this is not an extended verb, or
  425. // the request includes extended verbs
  426. if (!fExtended && SUCCEEDED(_dka.GetValue(idVerb, NULL, TEXT("Extended"), NULL, NULL, NULL)))
  427. return FALSE;
  428. static const struct {
  429. LPCTSTR pszVerb;
  430. } sVerbIgnore[] = {
  431. c_szPrintTo
  432. };
  433. for (int i = 0; i < ARRAYSIZE(sVerbIgnore); i++)
  434. {
  435. if (lstrcmpi(_dka.ExposeName(idVerb), sVerbIgnore[i].pszVerb) == 0)
  436. {
  437. return FALSE;
  438. }
  439. }
  440. return TRUE;
  441. }
  442. BOOL CShellExecMenu::_GetMenuString(UINT id, BOOL fExtended, LPTSTR pszMenu, UINT cchMax)
  443. {
  444. BOOL bRet = FALSE;
  445. // other verbs are hidden and just shouldnt be shown.
  446. if (SUCCEEDED(_InsureVerbs(id)) && _IsVisible(fExtended, id))
  447. {
  448. DWORD cbVerb = CbFromCch(cchMax);
  449. *pszMenu = 0;
  450. // try the MUIVerb value first
  451. // if that fails use the default value
  452. // either of these can actually have an MUI string
  453. if (FAILED(_dka.GetValue(id, NULL, TEXT("MUIVerb"), NULL, pszMenu, &cbVerb)))
  454. {
  455. cbVerb = CbFromCch(cchMax);
  456. _dka.GetValue(id, NULL, NULL, NULL, pszMenu, &cbVerb);
  457. }
  458. if (!*pszMenu || FAILED(SHLoadIndirectString(pszMenu, pszMenu, cchMax, NULL)))
  459. {
  460. // If it does not have the value, generate it.
  461. bRet = _MenuString(_dka.ExposeName(id), pszMenu, cchMax);
  462. }
  463. else
  464. {
  465. // use the value
  466. bRet = TRUE;
  467. }
  468. ASSERT(!bRet || *pszMenu);
  469. }
  470. return bRet;
  471. }
  472. STDMETHODIMP CShellExecMenu::Initialize(LPCITEMIDLIST pidlFolder, IDataObject *pdtobj, HKEY hkeyProgID)
  473. {
  474. // new behavior: good context menus should interpret a NULL pidlFolder/hkeyProgID on a re-init
  475. // as meaning they should use the ones they already have.
  476. if (hkeyProgID)
  477. {
  478. _Cleanup(); // cleans up hkey and hdka, pdtobj too but that's ok
  479. _hkeyProgID = SHRegDuplicateHKey(hkeyProgID); // make a copy
  480. }
  481. IUnknown_Set((IUnknown **)&_pdtobj, pdtobj);
  482. return S_OK;
  483. }
  484. UINT CShellExecMenu::_VerbCount()
  485. {
  486. return SUCCEEDED(_InsureVerbs()) ? _dka.GetItemCount() : 0;
  487. }
  488. UINT CShellExecMenu::_FindIndex(LPCTSTR pszVerb)
  489. {
  490. for (UINT i = 0; i < _VerbCount(); i++)
  491. {
  492. if (!lstrcmpi(pszVerb, _dka.ExposeName(i)))
  493. return i; // found it!
  494. }
  495. return -1;
  496. }
  497. STDMETHODIMP CShellExecMenu::QueryContextMenu(HMENU hmenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags)
  498. {
  499. UINT cVerbs = 0;
  500. _uFlags = uFlags; // caller may force explorer mode (CMF_EXPLORE) here
  501. TCHAR szMenu[CCH_MENUMAX];
  502. for (UINT idCmd = idCmdFirst;
  503. idCmd <= idCmdLast && (idCmd - idCmdFirst) < _VerbCount(); idCmd++)
  504. {
  505. UINT uMenuFlags = MF_BYPOSITION | MF_STRING;
  506. if (_GetMenuString(idCmd - idCmdFirst, uFlags & CMF_EXTENDEDVERBS, szMenu, ARRAYSIZE(szMenu)))
  507. {
  508. InsertMenu(hmenu, indexMenu + cVerbs, uMenuFlags, idCmd, szMenu);
  509. cVerbs++;
  510. }
  511. }
  512. if (cVerbs && (GetMenuDefaultItem(hmenu, MF_BYPOSITION, 0) == -1))
  513. {
  514. if (_dka.HasDefault(_hkeyProgID))
  515. {
  516. // if there is a default verb on this key,
  517. // trust that it was the first one that the CDKA added
  518. SetMenuDefaultItem(hmenu, indexMenu, MF_BYPOSITION);
  519. }
  520. }
  521. return ResultFromShort(_VerbCount());
  522. }
  523. LPCTSTR CShellExecMenu::_GetVerb(UINT id)
  524. {
  525. return SUCCEEDED(_InsureVerbs()) ? _dka.ExposeName(id) : NULL;
  526. }
  527. STATIC BOOL s_fAbortInvoke = FALSE;
  528. // This private export allows the folder code a way to cause the main invoke
  529. // loops processing several different files to abort.
  530. STDAPI_(void) SHAbortInvokeCommand()
  531. {
  532. DebugMsg(DM_TRACE, TEXT("AbortInvokeCommand was called"));
  533. s_fAbortInvoke = TRUE;
  534. }
  535. // Call shell exec (for the folder class) using the given file and the
  536. // given pidl. The file will be passed as %1 in the dde command and the pidl
  537. // will be passed as %2.
  538. STDAPI _InvokePidl(LPCMINVOKECOMMANDINFOEX pici, DWORD dwAttribs, LPCTSTR pszPath, LPCITEMIDLIST pidl, HKEY hkClass)
  539. {
  540. SHELLEXECUTEINFO ei;
  541. HRESULT hr = ICIX2SEI(pici, &ei);
  542. pszPath = (dwAttribs & SFGAO_FILESYSTEM) ? pszPath : NULL;
  543. if (SUCCEEDED(hr))
  544. {
  545. ei.fMask |= SEE_MASK_IDLIST;
  546. ei.lpFile = pszPath;
  547. ei.lpIDList = (void *)pidl;
  548. // if a directory is specifed use that, else make the current
  549. // directory be the folder it self. UNLESS it is a AUDIO CDRom, it
  550. // should never be the current directory (causes CreateProcess errors)
  551. if (!ei.lpDirectory && (dwAttribs & SFGAO_FOLDER))
  552. ei.lpDirectory = pszPath;
  553. if (pszPath && ei.lpDirectory)
  554. {
  555. INT iDrive = PathGetDriveNumber(ei.lpDirectory);
  556. CMountPoint* pmtpt = CMountPoint::GetMountPoint(iDrive);
  557. if (pmtpt)
  558. {
  559. if (pmtpt->IsAudioCDNoData())
  560. {
  561. ei.lpDirectory = NULL;
  562. }
  563. pmtpt->Release();
  564. }
  565. }
  566. if (hkClass)
  567. {
  568. ei.hkeyClass = hkClass;
  569. ei.fMask |= SEE_MASK_CLASSKEY;
  570. }
  571. else
  572. ei.fMask |= SEE_MASK_INVOKEIDLIST;
  573. if (ShellExecuteEx(&ei))
  574. hr = S_OK;
  575. else
  576. hr = HRESULT_FROM_WIN32(GetLastError());
  577. }
  578. return hr;
  579. }
  580. BOOL _QuitInvokeLoop()
  581. {
  582. MSG msg;
  583. // Try to give the user a way to escape out of this
  584. if (s_fAbortInvoke || GetAsyncKeyState(VK_ESCAPE) < 0)
  585. return TRUE;
  586. // And the next big mondo hack to handle CAD of our window
  587. // because the user thinks it is hung.
  588. if (PeekMessage(&msg, NULL, WM_CLOSE, WM_CLOSE, PM_NOREMOVE))
  589. return TRUE; // Lets also bail..
  590. return FALSE;
  591. }
  592. #define CMINVOKE_VERBT(pici) (pici)->lpVerbW
  593. HRESULT CShellExecMenu::_MapVerbForInvoke(CMINVOKECOMMANDINFOEX *pici, UINT *pidVerb)
  594. {
  595. LPCTSTR pszVerbKey;
  596. // is pici->lpVerb specifying the verb index (0-based).
  597. if (IS_INTRESOURCE(pici->lpVerb))
  598. {
  599. // find it in the CDKA
  600. *pidVerb = LOWORD((ULONG_PTR)pici->lpVerb);
  601. pszVerbKey = _GetVerb(*pidVerb);
  602. CMINVOKE_VERBT(pici) = pszVerbKey; // alias into the CDKA
  603. RIPMSG(pszVerbKey != NULL, "CShellExecMenu::InvokeCommand() passed an invalid verb id");
  604. }
  605. else
  606. {
  607. pszVerbKey = CMINVOKE_VERBT(pici);
  608. if (pszVerbKey)
  609. {
  610. *pidVerb = _FindIndex(pszVerbKey);
  611. if (-1 == *pidVerb)
  612. pszVerbKey = NULL; // not in our list
  613. }
  614. }
  615. ASSERT(!pszVerbKey || *pidVerb != -1);
  616. return pszVerbKey ? S_OK : E_INVALIDARG;
  617. }
  618. BOOL CShellExecMenu::_IsExplorerMode()
  619. {
  620. BOOL bRet = (_uFlags & CMF_EXPLORE);
  621. if (!bRet)
  622. {
  623. bRet = IsExplorerModeBrowser(_punkSite);
  624. if (bRet)
  625. _uFlags |= CMF_EXPLORE;
  626. }
  627. return bRet;
  628. }
  629. DWORD CShellExecMenu::_BrowseFlagsFromVerb(UINT idVerb)
  630. {
  631. DWORD dwFlags = 0;
  632. DWORD cbFlags = sizeof(dwFlags);
  633. _dka.GetValue(idVerb, NULL, _IsExplorerMode() ? TEXT("ExplorerFlags") : TEXT("BrowserFlags"), NULL, &dwFlags, &cbFlags);
  634. return dwFlags;
  635. }
  636. HRESULT CShellExecMenu::_TryBrowseObject(LPCITEMIDLIST pidl, DWORD uFlags)
  637. {
  638. HRESULT hr = S_FALSE;
  639. IShellBrowser *psb;
  640. if (SUCCEEDED(IUnknown_QueryService(_punkSite, SID_SShellBrowser, IID_PPV_ARG(IShellBrowser, &psb))))
  641. {
  642. hr = psb->BrowseObject(pidl, (UINT) uFlags);
  643. psb->Release();
  644. }
  645. return hr;
  646. }
  647. HRESULT _CanTryBrowseObject(DWORD dwAttribs, CMINVOKECOMMANDINFOEX* pici)
  648. {
  649. HRESULT hr = S_FALSE;
  650. if (dwAttribs & SFGAO_FOLDER)
  651. {
  652. // we need to sniff the iciex here to see if there is anything special in it
  653. // that cannot be conveyed to IShellBrowser::BrowseObject() (eg the nShow parameter)
  654. if ((pici->nShow == SW_SHOWNORMAL) ||
  655. (pici->nShow == SW_SHOW))
  656. {
  657. // nothing special in the ICIEX, should be safe to discard it and use
  658. // IShellBrowser::BrowseObject() instead of ShellExecuteEx
  659. hr = S_OK;
  660. }
  661. }
  662. return hr;
  663. }
  664. BOOL CShellExecMenu::_VerbCanDrop(UINT idVerb, CLSID *pclsid)
  665. {
  666. TCHAR sz[GUIDSTR_MAX];
  667. DWORD cb = sizeof(sz);
  668. return (SUCCEEDED(_dka.GetValue(idVerb, L"DropTarget", L"Clsid", NULL, sz, &cb))
  669. && GUIDFromString(sz, pclsid));
  670. }
  671. HRESULT CShellExecMenu::_DoDrop(REFCLSID clsid, UINT idVerb, LPCMINVOKECOMMANDINFOEX pici)
  672. {
  673. // i think i need to persist the pici into the _pdtobj
  674. // and probably add some values under the pqs
  675. // we assume that the app will do something appropriate
  676. // QueryService(_punkSite, clsid) might be useful
  677. return SHSimulateDropOnClsid(clsid, _punkSite, _pdtobj);
  678. }
  679. STDMETHODIMP CShellExecMenu::InvokeCommand(LPCMINVOKECOMMANDINFO pici)
  680. {
  681. CMINVOKECOMMANDINFOEX ici;
  682. void *pvFree;
  683. HRESULT hr = ICI2ICIX(pici, &ici, &pvFree); // thunk incomming params
  684. if (SUCCEEDED(hr))
  685. {
  686. UINT idVerb;
  687. hr = _MapVerbForInvoke(&ici, &idVerb);
  688. if (SUCCEEDED(hr))
  689. {
  690. CLSID clsid;
  691. if (_VerbCanDrop(idVerb, &clsid))
  692. {
  693. hr = _DoDrop(clsid, idVerb, &ici);
  694. }
  695. else
  696. {
  697. STGMEDIUM medium;
  698. LPIDA pida = DataObj_GetHIDA(_pdtobj, &medium);
  699. if (pida)
  700. {
  701. if (pida->cidl == 1)
  702. {
  703. LPITEMIDLIST pidl = IDA_FullIDList(pida, 0);
  704. if (pidl)
  705. {
  706. hr = _InvokeOne(&ici, idVerb, pidl);
  707. ILFree(pidl);
  708. }
  709. else
  710. hr = E_OUTOFMEMORY;
  711. }
  712. else
  713. {
  714. hr = _InvokeMany(&ici, idVerb, pida);
  715. }
  716. HIDA_ReleaseStgMedium(pida, &medium);
  717. }
  718. else
  719. hr = E_OUTOFMEMORY;
  720. }
  721. }
  722. if (pvFree)
  723. LocalFree(pvFree);
  724. }
  725. return hr;
  726. }
  727. HRESULT CShellExecMenu::_InvokeOne(CMINVOKECOMMANDINFOEX *pici, UINT idVerb, LPCITEMIDLIST pidl)
  728. {
  729. HRESULT hr = S_FALSE;
  730. TCHAR szPath[MAX_PATH];
  731. DWORD dwAttrib = SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_LINK;
  732. SHGetNameAndFlags(pidl, SHGDN_FORPARSING, szPath, ARRAYSIZE(szPath), &dwAttrib);
  733. if (S_OK == _CanTryBrowseObject(dwAttrib, pici))
  734. {
  735. DWORD uFlags = _BrowseFlagsFromVerb(idVerb);
  736. if (uFlags)
  737. {
  738. // if we did the site based navigation, we are done
  739. hr = _TryBrowseObject(pidl, uFlags);
  740. }
  741. }
  742. if (hr != S_OK)
  743. {
  744. hr = _InvokePidl(pici, dwAttrib, szPath, pidl, _hkeyProgID);
  745. // only set recent on non-folders (SFGAO_STREAM?)
  746. // and non-link since we know those should never be added
  747. if (SUCCEEDED(hr) && !(dwAttrib & (SFGAO_FOLDER | SFGAO_LINK)))
  748. {
  749. AddToRecentDocs(pidl, szPath);
  750. }
  751. }
  752. return hr;
  753. }
  754. BOOL _ShouldPrompt(DWORD cItems)
  755. {
  756. DWORD dwMin, cb = sizeof(dwMin);
  757. if (SHRegGetUSValue(REGSTR_PATH_EXPLORER, TEXT("MultipleInvokePromptMinimum"), NULL, &dwMin, &cb, FALSE, NULL, 0) != ERROR_SUCCESS)
  758. dwMin = 15;
  759. return cItems > dwMin;
  760. }
  761. HRESULT CShellExecMenu::_PromptUser(CMINVOKECOMMANDINFOEX *pici, UINT idVerb, LPIDA pida)
  762. {
  763. HRESULT hr = S_FALSE;
  764. if (pici->hwnd && !(pici->fMask & CMIC_MASK_FLAG_NO_UI)
  765. && _ShouldPrompt(pida->cidl))
  766. {
  767. // prompt the user with the verb and count
  768. // we make a better experience if we keyed off
  769. // homo/hetero types and had different behaviors
  770. // but its not worth it. instead we should
  771. // switch to using AutoPlay sniffing and dialog.
  772. TCHAR szVerb[64];
  773. TCHAR szNum[10];
  774. wnsprintf(szNum, ARRAYSIZE(szNum), TEXT("%d"), pida->cidl);
  775. hr = _GetHelpText(idVerb, GCS_HELPTEXT, (PSTR)szVerb, ARRAYSIZE(szVerb));
  776. if (SUCCEEDED(hr))
  777. {
  778. hr = E_OUTOFMEMORY;
  779. PTSTR pszTitle = ShellConstructMessageString(HINST_THISDLL, MAKEINTRESOURCE(IDS_MULTIINVOKEPROMPT_TITLE), szVerb);
  780. if (pszTitle)
  781. {
  782. PTSTR pszMsg = ShellConstructMessageString(HINST_THISDLL, MAKEINTRESOURCE(IDS_MULTIINVOKEPROMPT_MESSAGE), szVerb, szNum);
  783. if (pszMsg)
  784. {
  785. int iRet = SHMessageBoxCheck(pici->hwnd, pszMsg, pszTitle, (MB_OKCANCEL | MB_ICONEXCLAMATION), IDOK, TEXT("MultipleInvokePrompt"));
  786. hr = iRet == IDOK ? S_OK : HRESULT_FROM_WIN32(ERROR_CANCELLED);
  787. LocalFree(pszMsg);
  788. }
  789. LocalFree(pszTitle);
  790. }
  791. }
  792. }
  793. return hr;
  794. }
  795. HRESULT CShellExecMenu::_InvokeEach(LPCITEMIDLIST pidl, CMINVOKECOMMANDINFOEX *pici)
  796. {
  797. HRESULT hr = E_OUTOFMEMORY;
  798. HMENU hmenu = CreatePopupMenu();
  799. if (hmenu)
  800. {
  801. CComPtr<IContextMenu> spcm;
  802. hr = SHGetUIObjectOf(pidl, NULL, IID_PPV_ARG(IContextMenu, &spcm));
  803. if (SUCCEEDED(hr))
  804. {
  805. if (_punkSite)
  806. IUnknown_SetSite(spcm, _punkSite);
  807. hr = spcm->QueryContextMenu(hmenu, 0, CONTEXTMENU_IDCMD_FIRST, CONTEXTMENU_IDCMD_LAST, _uFlags);
  808. if (SUCCEEDED(hr))
  809. {
  810. hr = spcm->InvokeCommand((CMINVOKECOMMANDINFO *)pici);
  811. }
  812. if (_punkSite)
  813. IUnknown_SetSite(spcm, NULL);
  814. }
  815. DestroyMenu(hmenu);
  816. }
  817. return hr;
  818. }
  819. HRESULT CShellExecMenu::_InvokeMany(CMINVOKECOMMANDINFOEX *pici, UINT idVerb, LPIDA pida)
  820. {
  821. HRESULT hr = _PromptUser(pici, idVerb, pida);
  822. if (SUCCEEDED(hr))
  823. {
  824. USES_CONVERSION;
  825. s_fAbortInvoke = FALSE; // reset this global for this run...
  826. // we want to alter the pici
  827. // so that each item is handled individually
  828. pici->hwnd = NULL;
  829. pici->fMask |= CMIC_MASK_FLAG_NO_UI;
  830. // NTBUG #502223 - MSI apps with DDE start multiple copies - ZekeL 2001-DEC-07
  831. // ShellExec() will create a new thread for MSI apps to
  832. // avoid a deadlock with the MSI APIs calling SHChangeNotify().
  833. // this is described in NTBUG #200961
  834. // however in the multiple invoke case we create one thread
  835. // for each item in the invoke, which results in several processes
  836. // contending for the DDE conversation.
  837. //
  838. // this is a half fix. we prefer to have the buggy behavior in 502223
  839. // over the deadlock behavior in 200961 (a definite PSS call).
  840. // since the deadlock case should only occur for the desktop,
  841. // the rest of the time we will force a synchronous invoke.
  842. IBindCtx *pbcRelease = NULL;
  843. if (!IsDesktopBrowser(_punkSite))
  844. {
  845. TBCRegisterObjectParam(TBCDIDASYNC, SAFECAST(this, IContextMenu *), &pbcRelease);
  846. }
  847. pici->lpVerb = T2A(_dka.ExposeName(idVerb));
  848. pici->lpVerbW = _dka.ExposeName(idVerb);
  849. for (UINT iItem = 0; !_QuitInvokeLoop() && (iItem < pida->cidl); iItem++)
  850. {
  851. LPITEMIDLIST pidl = IDA_FullIDList(pida, iItem);
  852. if (pidl)
  853. {
  854. hr = _InvokeEach(pidl, pici);
  855. ILFree(pidl);
  856. }
  857. else
  858. hr = E_OUTOFMEMORY;
  859. if (hr == E_OUTOFMEMORY)
  860. break;
  861. }
  862. ATOMICRELEASE(pbcRelease);
  863. }
  864. return hr;
  865. }
  866. HRESULT CShellExecMenu::_GetHelpText(UINT idVerb, UINT uType, LPSTR pszName, UINT cchMax)
  867. {
  868. // TODO - shouldnt we let the registry override?
  869. HRESULT hr = E_OUTOFMEMORY;
  870. TCHAR szMenuString[CCH_MENUMAX];
  871. if (_GetMenuString(idVerb, TRUE, szMenuString, ARRAYSIZE(szMenuString)))
  872. {
  873. SHStripMneumonic(szMenuString);
  874. // NOTE on US, IDS_VERBHELP is the same as "%s"
  875. // do we want some better description?
  876. LPTSTR pszHelp = ShellConstructMessageString(HINST_THISDLL, MAKEINTRESOURCE(IDS_VERBHELP), szMenuString);
  877. if (pszHelp)
  878. {
  879. if (uType == GCS_HELPTEXTA)
  880. SHTCharToAnsi(pszHelp, pszName, cchMax);
  881. else
  882. SHTCharToUnicode(pszHelp, (LPWSTR)pszName, cchMax);
  883. LocalFree(pszHelp);
  884. hr = S_OK;
  885. }
  886. }
  887. return hr;
  888. }
  889. HRESULT CShellExecMenu::_MapVerbForGCS(UINT_PTR idCmd, UINT uType, UINT *pidVerb)
  890. {
  891. HRESULT hr = _InsureVerbs();
  892. if (SUCCEEDED(hr))
  893. {
  894. if (IS_INTRESOURCE(idCmd))
  895. *pidVerb = (UINT)idCmd;
  896. else
  897. {
  898. *pidVerb = -1;
  899. if (!(uType & GCS_UNICODE))
  900. {
  901. USES_CONVERSION;
  902. *pidVerb = _FindIndex(A2W((LPCSTR)idCmd));
  903. }
  904. // we fall back to the TCHAR version regardless
  905. // of what the caller passed in uType
  906. if (*pidVerb == -1)
  907. {
  908. if (!IsBadStringPtrW((LPCWSTR)idCmd, (UINT)-1))
  909. *pidVerb = _FindIndex((LPCWSTR)idCmd);
  910. }
  911. }
  912. hr = *pidVerb < _VerbCount() ? S_OK : E_INVALIDARG;
  913. }
  914. // VALIDATE returns S_FALSE for bad verbs
  915. if (FAILED(hr) && (uType == GCS_VALIDATEA || uType == GCS_VALIDATEW))
  916. hr = S_FALSE;
  917. return hr;
  918. }
  919. STDMETHODIMP CShellExecMenu::GetCommandString(UINT_PTR idCmd, UINT uType, UINT *pwRes, LPSTR pszName, UINT cchMax)
  920. {
  921. UINT idVerb;
  922. HRESULT hr = _MapVerbForGCS(idCmd, uType, &idVerb);
  923. if (SUCCEEDED(hr))
  924. {
  925. // the verb is good!
  926. switch (uType)
  927. {
  928. case GCS_HELPTEXTA:
  929. case GCS_HELPTEXTW:
  930. hr = _GetHelpText(idVerb, uType, pszName, cchMax);
  931. break;
  932. case GCS_VERBA:
  933. case GCS_VERBW:
  934. {
  935. if (uType == GCS_VERBA)
  936. SHTCharToAnsi(_dka.ExposeName(idVerb), pszName, cchMax);
  937. else
  938. SHTCharToUnicode(_dka.ExposeName(idVerb), (LPWSTR)pszName, cchMax);
  939. hr = S_OK;
  940. }
  941. break;
  942. case GCS_VALIDATEA:
  943. case GCS_VALIDATEW:
  944. // the hr from MapVerb is good enough
  945. break;
  946. default:
  947. hr = E_NOTIMPL;
  948. break;
  949. }
  950. }
  951. return hr;
  952. }
  953. STDMETHODIMP CShellExecMenu::AddPages(LPFNADDPROPSHEETPAGE pfnAddPage, LPARAM lParam)
  954. {
  955. return _pfnAddPages(_pdtobj, pfnAddPage, lParam);
  956. }
  957. STDMETHODIMP CShellExecMenu::ReplacePage(UINT uPageID, LPFNADDPROPSHEETPAGE pfnReplaceWith, LPARAM lParam)
  958. {
  959. return E_NOTIMPL;
  960. }
  961. STDAPI CShellExecMenu_CreateInstance(LPFNADDPAGES pfnAddPages, REFIID riid, void **ppv)
  962. {
  963. HRESULT hr;
  964. CShellExecMenu *pdext = new CShellExecMenu(pfnAddPages);
  965. if (pdext)
  966. {
  967. hr = pdext->QueryInterface(riid, ppv);
  968. pdext->Release();
  969. }
  970. else
  971. {
  972. *ppv = NULL;
  973. hr = E_OUTOFMEMORY;
  974. }
  975. return hr;
  976. }
  977. // these handlers slime off of CShellExecMenu's IShellPropSheetExt implementation
  978. STDAPI FileSystem_AddPages(IDataObject *pdtobj, LPFNADDPROPSHEETPAGE pfnAddPage, LPARAM lParam);
  979. STDAPI CShellFileDefExt_CreateInstance(IUnknown *punkOuter, REFIID riid, void **ppv)
  980. {
  981. return CShellExecMenu_CreateInstance(FileSystem_AddPages, riid, ppv);
  982. }
  983. STDAPI CDrives_AddPages(IDataObject *pdtobj, LPFNADDPROPSHEETPAGE pfnAddPage, LPARAM lParam);
  984. STDAPI CShellDrvDefExt_CreateInstance(IUnknown *punkOuter, REFIID riid, void **ppv)
  985. {
  986. return CShellExecMenu_CreateInstance(CDrives_AddPages, riid, ppv);
  987. }
  988. STDAPI PIF_AddPages(IDataObject *pdtobj, LPFNADDPROPSHEETPAGE pfnAddPage, LPARAM lParam);
  989. STDAPI CProxyPage_CreateInstance(IUnknown *punkOuter, REFIID riid, void **ppv)
  990. {
  991. return CShellExecMenu_CreateInstance(PIF_AddPages, riid, ppv);
  992. }