Leaked source code of windows server 2003
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.

1151 lines
35 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. ASSERT( 0 != _cRef );
  275. ULONG cRef = InterlockedDecrement(&_cRef);
  276. if ( 0 == cRef )
  277. {
  278. delete this;
  279. }
  280. return cRef;
  281. }
  282. HRESULT CShellExecMenu::_InsureVerbs(UINT idVerb)
  283. {
  284. // the idVerb is the minimum verb that we need to succeed
  285. if (!(HDSA)_dka && _hkeyProgID)
  286. {
  287. // create either "open" or "explore open"
  288. if (_dka.Create(4))
  289. {
  290. _dka.AddKeys(_hkeyProgID, c_szShell, _IsExplorerMode() ? TEXT("Explore open") : TEXT("open"));
  291. // WARNING - some verbs are not valid and need to be removed
  292. for (int id = 0; id < _dka.GetItemCount(); id++)
  293. {
  294. if (_RemoveVerb(id))
  295. _dka.DeleteItem(id);
  296. }
  297. }
  298. }
  299. return ((HDSA)_dka && idVerb < (UINT)_dka.GetItemCount()) ? S_OK : E_FAIL;
  300. }
  301. // Descriptions:
  302. // This function generates appropriate menu string from the given
  303. // verb key string. This function is called if the verb key does
  304. // not have the value.
  305. BOOL _MenuString(LPCTSTR pszVerbKey, LPTSTR pszMenuString, UINT cchMax)
  306. {
  307. // Table look-up (verb key -> menu string mapping)
  308. const static struct
  309. {
  310. LPCTSTR pszVerb;
  311. UINT id;
  312. } sVerbTrans[] = {
  313. c_szOpen, IDS_MENUOPEN,
  314. c_szExplore, IDS_MENUEXPLORE,
  315. TEXT("edit"),IDS_MENUEDIT,
  316. c_szFind, IDS_MENUFIND,
  317. c_szPrint, IDS_MENUPRINT,
  318. c_szOpenAs, IDS_MENUOPEN,
  319. TEXT("runas"),IDS_MENURUNAS
  320. };
  321. for (int i = 0; i < ARRAYSIZE(sVerbTrans); i++)
  322. {
  323. if (lstrcmpi(pszVerbKey, sVerbTrans[i].pszVerb) == 0)
  324. {
  325. if (LoadString(HINST_THISDLL, sVerbTrans[i].id, pszMenuString, cchMax))
  326. return TRUE;
  327. break;
  328. }
  329. }
  330. // Worst case: Just put '&' on the top.
  331. pszMenuString[0] = TEXT('&');
  332. pszMenuString++;
  333. cchMax--;
  334. lstrcpyn(pszMenuString, pszVerbKey, cchMax);
  335. return TRUE;
  336. }
  337. // Checks to see if there is a user policy in place that disables this key,
  338. //
  339. // For example, in the registry:
  340. //
  341. // CLSID_MyComputer
  342. // +---Shell
  343. // +---Manage
  344. // (Default) = "Mana&ge"
  345. // SuppressionPolicy = REST_NOMANAGEMYCOMPUTERVERB
  346. //
  347. // (Where REST_NOMANAGEMYCOMPUTERVERB is the DWORD value of that particular policy)
  348. BOOL CShellExecMenu::_IsRestricted(UINT idVerb)
  349. {
  350. RESTRICTIONS rest;
  351. BOOL fRestrict = FALSE;
  352. if (0 == lstrcmpi(TEXT("runas"), _dka.ExposeName(idVerb)))
  353. {
  354. rest = REST_HIDERUNASVERB;
  355. fRestrict = TRUE;
  356. }
  357. else
  358. {
  359. DWORD cb = sizeof(rest);
  360. fRestrict = SUCCEEDED(_dka.GetValue(idVerb, NULL, TEXT("SuppressionPolicy"), NULL, &rest, &cb));
  361. }
  362. return fRestrict && SHRestricted(rest);
  363. }
  364. HRESULT _GetAppSource(HKEY hk, PCWSTR pszVerb, IQuerySource **ppqs)
  365. {
  366. CComPtr<IAssociationElement> spae;
  367. HRESULT hr = AssocElemCreateForKey(&CLSID_AssocShellElement, hk, &spae);
  368. if (SUCCEEDED(hr))
  369. {
  370. CComPtr<IObjectWithQuerySource> spowqsApp;
  371. hr = spae->QueryObject(AQVO_APPLICATION_DELEGATE, pszVerb, IID_PPV_ARG(IObjectWithQuerySource, &spowqsApp));
  372. if (SUCCEEDED(hr))
  373. {
  374. hr = spowqsApp->GetSource(IID_PPV_ARG(IQuerySource, ppqs));
  375. }
  376. }
  377. return hr;
  378. }
  379. BOOL CShellExecMenu::_SupportsType(UINT idVerb)
  380. {
  381. BOOL fRet = TRUE;
  382. if (SUCCEEDED(_dka.GetValue(idVerb, NULL, TEXT("CheckSupportedTypes"), NULL, NULL, NULL)))
  383. {
  384. // need to check the supported types for this application
  385. // get the first item and then check it against SupportedFileExtensions
  386. CComPtr<IShellItem> spsi;
  387. if (SUCCEEDED(DataObj_GetIShellItem(_pdtobj, &spsi)))
  388. {
  389. SFGAOF sfgao;
  390. if (S_OK == spsi->GetAttributes(SFGAO_STREAM, &sfgao))
  391. {
  392. CSmartCoTaskMem<OLECHAR> spszName;
  393. if (SUCCEEDED(spsi->GetDisplayName(SIGDN_PARENTRELATIVEPARSING, &spszName)))
  394. {
  395. PWSTR pszExt = PathFindExtension(spszName);
  396. if (*pszExt)
  397. {
  398. CComPtr<IQuerySource> spqs;
  399. if (SUCCEEDED(_GetAppSource(_hkeyProgID, _dka.ExposeName(idVerb), &spqs)))
  400. {
  401. fRet = SUCCEEDED(spqs->QueryValueExists(L"SupportedTypes", pszExt));
  402. }
  403. }
  404. }
  405. }
  406. }
  407. }
  408. return fRet;
  409. }
  410. //
  411. // LegacyDisable
  412. // LegacyDisable is set, then the verb exists only for legacy reasons, and
  413. // is actually superceded by a context menu extension or some other behavior
  414. // it there only to retain legacy behavior for external clients that require
  415. // the existence of a verb.
  416. //
  417. BOOL CShellExecMenu::_RemoveVerb(UINT idVerb)
  418. {
  419. if (SUCCEEDED(_dka.GetValue(idVerb, NULL, TEXT("LegacyDisable"), NULL, NULL, NULL)))
  420. return TRUE;
  421. if (!_SupportsType(idVerb))
  422. return TRUE;
  423. return (_IsRestricted(idVerb));
  424. }
  425. BOOL CShellExecMenu::_IsVisible(BOOL fExtended, UINT idVerb)
  426. {
  427. // this is not an extended verb, or
  428. // the request includes extended verbs
  429. if (!fExtended && SUCCEEDED(_dka.GetValue(idVerb, NULL, TEXT("Extended"), NULL, NULL, NULL)))
  430. return FALSE;
  431. static const struct {
  432. LPCTSTR pszVerb;
  433. } sVerbIgnore[] = {
  434. c_szPrintTo
  435. };
  436. for (int i = 0; i < ARRAYSIZE(sVerbIgnore); i++)
  437. {
  438. if (lstrcmpi(_dka.ExposeName(idVerb), sVerbIgnore[i].pszVerb) == 0)
  439. {
  440. return FALSE;
  441. }
  442. }
  443. return TRUE;
  444. }
  445. BOOL CShellExecMenu::_GetMenuString(UINT id, BOOL fExtended, LPTSTR pszMenu, UINT cchMax)
  446. {
  447. BOOL bRet = FALSE;
  448. // other verbs are hidden and just shouldnt be shown.
  449. if (SUCCEEDED(_InsureVerbs(id)) && _IsVisible(fExtended, id))
  450. {
  451. DWORD cbVerb = CbFromCch(cchMax);
  452. *pszMenu = 0;
  453. // try the MUIVerb value first
  454. // if that fails use the default value
  455. // either of these can actually have an MUI string
  456. if (FAILED(_dka.GetValue(id, NULL, TEXT("MUIVerb"), NULL, pszMenu, &cbVerb)))
  457. {
  458. cbVerb = CbFromCch(cchMax);
  459. _dka.GetValue(id, NULL, NULL, NULL, pszMenu, &cbVerb);
  460. }
  461. if (!*pszMenu || FAILED(SHLoadIndirectString(pszMenu, pszMenu, cchMax, NULL)))
  462. {
  463. // If it does not have the value, generate it.
  464. bRet = _MenuString(_dka.ExposeName(id), pszMenu, cchMax);
  465. }
  466. else
  467. {
  468. // use the value
  469. bRet = TRUE;
  470. }
  471. ASSERT(!bRet || *pszMenu);
  472. }
  473. return bRet;
  474. }
  475. STDMETHODIMP CShellExecMenu::Initialize(LPCITEMIDLIST pidlFolder, IDataObject *pdtobj, HKEY hkeyProgID)
  476. {
  477. // new behavior: good context menus should interpret a NULL pidlFolder/hkeyProgID on a re-init
  478. // as meaning they should use the ones they already have.
  479. if (hkeyProgID)
  480. {
  481. _Cleanup(); // cleans up hkey and hdka, pdtobj too but that's ok
  482. _hkeyProgID = SHRegDuplicateHKey(hkeyProgID); // make a copy
  483. }
  484. IUnknown_Set((IUnknown **)&_pdtobj, pdtobj);
  485. return S_OK;
  486. }
  487. UINT CShellExecMenu::_VerbCount()
  488. {
  489. return SUCCEEDED(_InsureVerbs()) ? _dka.GetItemCount() : 0;
  490. }
  491. UINT CShellExecMenu::_FindIndex(LPCTSTR pszVerb)
  492. {
  493. for (UINT i = 0; i < _VerbCount(); i++)
  494. {
  495. if (!lstrcmpi(pszVerb, _dka.ExposeName(i)))
  496. return i; // found it!
  497. }
  498. return -1;
  499. }
  500. STDMETHODIMP CShellExecMenu::QueryContextMenu(HMENU hmenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags)
  501. {
  502. UINT cVerbs = 0;
  503. _uFlags = uFlags; // caller may force explorer mode (CMF_EXPLORE) here
  504. TCHAR szMenu[CCH_MENUMAX];
  505. for (UINT idCmd = idCmdFirst;
  506. idCmd <= idCmdLast && (idCmd - idCmdFirst) < _VerbCount(); idCmd++)
  507. {
  508. UINT uMenuFlags = MF_BYPOSITION | MF_STRING;
  509. if (_GetMenuString(idCmd - idCmdFirst, uFlags & CMF_EXTENDEDVERBS, szMenu, ARRAYSIZE(szMenu)))
  510. {
  511. InsertMenu(hmenu, indexMenu + cVerbs, uMenuFlags, idCmd, szMenu);
  512. cVerbs++;
  513. }
  514. }
  515. if (cVerbs && (GetMenuDefaultItem(hmenu, MF_BYPOSITION, 0) == -1))
  516. {
  517. if (_dka.HasDefault(_hkeyProgID))
  518. {
  519. // if there is a default verb on this key,
  520. // trust that it was the first one that the CDKA added
  521. SetMenuDefaultItem(hmenu, indexMenu, MF_BYPOSITION);
  522. }
  523. }
  524. return ResultFromShort(_VerbCount());
  525. }
  526. LPCTSTR CShellExecMenu::_GetVerb(UINT id)
  527. {
  528. return SUCCEEDED(_InsureVerbs()) ? _dka.ExposeName(id) : NULL;
  529. }
  530. STATIC BOOL s_fAbortInvoke = FALSE;
  531. // This private export allows the folder code a way to cause the main invoke
  532. // loops processing several different files to abort.
  533. STDAPI_(void) SHAbortInvokeCommand()
  534. {
  535. DebugMsg(DM_TRACE, TEXT("AbortInvokeCommand was called"));
  536. s_fAbortInvoke = TRUE;
  537. }
  538. // Call shell exec (for the folder class) using the given file and the
  539. // given pidl. The file will be passed as %1 in the dde command and the pidl
  540. // will be passed as %2.
  541. STDAPI _InvokePidl(LPCMINVOKECOMMANDINFOEX pici, DWORD dwAttribs, LPCTSTR pszPath, LPCITEMIDLIST pidl, HKEY hkClass)
  542. {
  543. SHELLEXECUTEINFO ei;
  544. HRESULT hr = ICIX2SEI(pici, &ei);
  545. pszPath = (dwAttribs & SFGAO_FILESYSTEM) ? pszPath : NULL;
  546. if (SUCCEEDED(hr))
  547. {
  548. ei.fMask |= SEE_MASK_IDLIST;
  549. ei.lpFile = pszPath;
  550. ei.lpIDList = (void *)pidl;
  551. // if a directory is specifed use that, else make the current
  552. // directory be the folder it self. UNLESS it is a AUDIO CDRom, it
  553. // should never be the current directory (causes CreateProcess errors)
  554. if (!ei.lpDirectory && (dwAttribs & SFGAO_FOLDER))
  555. ei.lpDirectory = pszPath;
  556. if (pszPath && ei.lpDirectory)
  557. {
  558. INT iDrive = PathGetDriveNumber(ei.lpDirectory);
  559. CMountPoint* pmtpt = CMountPoint::GetMountPoint(iDrive);
  560. if (pmtpt)
  561. {
  562. if (pmtpt->IsAudioCDNoData())
  563. {
  564. ei.lpDirectory = NULL;
  565. }
  566. pmtpt->Release();
  567. }
  568. }
  569. if (hkClass)
  570. {
  571. ei.hkeyClass = hkClass;
  572. ei.fMask |= SEE_MASK_CLASSKEY;
  573. }
  574. else
  575. ei.fMask |= SEE_MASK_INVOKEIDLIST;
  576. if (ShellExecuteEx(&ei))
  577. hr = S_OK;
  578. else
  579. hr = HRESULT_FROM_WIN32(GetLastError());
  580. }
  581. return hr;
  582. }
  583. BOOL _QuitInvokeLoop()
  584. {
  585. MSG msg;
  586. // Try to give the user a way to escape out of this
  587. if (s_fAbortInvoke || GetAsyncKeyState(VK_ESCAPE) < 0)
  588. return TRUE;
  589. // And the next big mondo hack to handle CAD of our window
  590. // because the user thinks it is hung.
  591. if (PeekMessage(&msg, NULL, WM_CLOSE, WM_CLOSE, PM_NOREMOVE))
  592. return TRUE; // Lets also bail..
  593. return FALSE;
  594. }
  595. #define CMINVOKE_VERBT(pici) (pici)->lpVerbW
  596. HRESULT CShellExecMenu::_MapVerbForInvoke(CMINVOKECOMMANDINFOEX *pici, UINT *pidVerb)
  597. {
  598. LPCTSTR pszVerbKey;
  599. // is pici->lpVerb specifying the verb index (0-based).
  600. if (IS_INTRESOURCE(pici->lpVerb))
  601. {
  602. // find it in the CDKA
  603. *pidVerb = LOWORD((ULONG_PTR)pici->lpVerb);
  604. pszVerbKey = _GetVerb(*pidVerb);
  605. CMINVOKE_VERBT(pici) = pszVerbKey; // alias into the CDKA
  606. RIPMSG(pszVerbKey != NULL, "CShellExecMenu::InvokeCommand() passed an invalid verb id");
  607. }
  608. else
  609. {
  610. pszVerbKey = CMINVOKE_VERBT(pici);
  611. if (pszVerbKey)
  612. {
  613. *pidVerb = _FindIndex(pszVerbKey);
  614. if (-1 == *pidVerb)
  615. pszVerbKey = NULL; // not in our list
  616. }
  617. }
  618. ASSERT(!pszVerbKey || *pidVerb != -1);
  619. return pszVerbKey ? S_OK : E_INVALIDARG;
  620. }
  621. BOOL CShellExecMenu::_IsExplorerMode()
  622. {
  623. BOOL bRet = (_uFlags & CMF_EXPLORE);
  624. if (!bRet)
  625. {
  626. bRet = IsExplorerModeBrowser(_punkSite);
  627. if (bRet)
  628. _uFlags |= CMF_EXPLORE;
  629. }
  630. return bRet;
  631. }
  632. DWORD CShellExecMenu::_BrowseFlagsFromVerb(UINT idVerb)
  633. {
  634. DWORD dwFlags = 0;
  635. DWORD cbFlags = sizeof(dwFlags);
  636. _dka.GetValue(idVerb, NULL, _IsExplorerMode() ? TEXT("ExplorerFlags") : TEXT("BrowserFlags"), NULL, &dwFlags, &cbFlags);
  637. return dwFlags;
  638. }
  639. HRESULT CShellExecMenu::_TryBrowseObject(LPCITEMIDLIST pidl, DWORD uFlags)
  640. {
  641. HRESULT hr = S_FALSE;
  642. IShellBrowser *psb;
  643. if (SUCCEEDED(IUnknown_QueryService(_punkSite, SID_SShellBrowser, IID_PPV_ARG(IShellBrowser, &psb))))
  644. {
  645. hr = psb->BrowseObject(pidl, (UINT) uFlags);
  646. psb->Release();
  647. }
  648. return hr;
  649. }
  650. HRESULT _CanTryBrowseObject(DWORD dwAttribs, CMINVOKECOMMANDINFOEX* pici)
  651. {
  652. HRESULT hr = S_FALSE;
  653. if (dwAttribs & SFGAO_FOLDER)
  654. {
  655. // we need to sniff the iciex here to see if there is anything special in it
  656. // that cannot be conveyed to IShellBrowser::BrowseObject() (eg the nShow parameter)
  657. if ((pici->nShow == SW_SHOWNORMAL) ||
  658. (pici->nShow == SW_SHOW))
  659. {
  660. // nothing special in the ICIEX, should be safe to discard it and use
  661. // IShellBrowser::BrowseObject() instead of ShellExecuteEx
  662. hr = S_OK;
  663. }
  664. }
  665. return hr;
  666. }
  667. BOOL CShellExecMenu::_VerbCanDrop(UINT idVerb, CLSID *pclsid)
  668. {
  669. TCHAR sz[GUIDSTR_MAX];
  670. DWORD cb = sizeof(sz);
  671. return (SUCCEEDED(_dka.GetValue(idVerb, L"DropTarget", L"Clsid", NULL, sz, &cb))
  672. && GUIDFromString(sz, pclsid));
  673. }
  674. HRESULT CShellExecMenu::_DoDrop(REFCLSID clsid, UINT idVerb, LPCMINVOKECOMMANDINFOEX pici)
  675. {
  676. // i think i need to persist the pici into the _pdtobj
  677. // and probably add some values under the pqs
  678. // we assume that the app will do something appropriate
  679. // QueryService(_punkSite, clsid) might be useful
  680. return SHSimulateDropOnClsid(clsid, _punkSite, _pdtobj);
  681. }
  682. STDMETHODIMP CShellExecMenu::InvokeCommand(LPCMINVOKECOMMANDINFO pici)
  683. {
  684. CMINVOKECOMMANDINFOEX ici;
  685. void *pvFree;
  686. HRESULT hr = ICI2ICIX(pici, &ici, &pvFree); // thunk incomming params
  687. if (SUCCEEDED(hr))
  688. {
  689. UINT idVerb;
  690. hr = _MapVerbForInvoke(&ici, &idVerb);
  691. if (SUCCEEDED(hr))
  692. {
  693. CLSID clsid;
  694. if (_VerbCanDrop(idVerb, &clsid))
  695. {
  696. hr = _DoDrop(clsid, idVerb, &ici);
  697. }
  698. else
  699. {
  700. STGMEDIUM medium;
  701. LPIDA pida = DataObj_GetHIDA(_pdtobj, &medium);
  702. if (pida)
  703. {
  704. if (pida->cidl == 1)
  705. {
  706. LPITEMIDLIST pidl = IDA_FullIDList(pida, 0);
  707. if (pidl)
  708. {
  709. hr = _InvokeOne(&ici, idVerb, pidl);
  710. ILFree(pidl);
  711. }
  712. else
  713. hr = E_OUTOFMEMORY;
  714. }
  715. else
  716. {
  717. hr = _InvokeMany(&ici, idVerb, pida);
  718. }
  719. HIDA_ReleaseStgMedium(pida, &medium);
  720. }
  721. else
  722. hr = E_OUTOFMEMORY;
  723. }
  724. }
  725. if (pvFree)
  726. LocalFree(pvFree);
  727. }
  728. return hr;
  729. }
  730. HRESULT CShellExecMenu::_InvokeOne(CMINVOKECOMMANDINFOEX *pici, UINT idVerb, LPCITEMIDLIST pidl)
  731. {
  732. HRESULT hr;
  733. TCHAR szPath[MAX_PATH];
  734. DWORD dwAttrib = SFGAO_FILESYSTEM | SFGAO_FOLDER | SFGAO_LINK;
  735. hr = SHGetNameAndFlags(pidl, SHGDN_FORPARSING, szPath, ARRAYSIZE(szPath), &dwAttrib);
  736. if (SUCCEEDED(hr))
  737. {
  738. hr = S_FALSE;
  739. if (S_OK == _CanTryBrowseObject(dwAttrib, pici))
  740. {
  741. DWORD uFlags = _BrowseFlagsFromVerb(idVerb);
  742. if (uFlags)
  743. {
  744. // if we did the site based navigation, we are done
  745. hr = _TryBrowseObject(pidl, uFlags);
  746. }
  747. }
  748. if (hr != S_OK)
  749. {
  750. hr = _InvokePidl(pici, dwAttrib, szPath, pidl, _hkeyProgID);
  751. // only set recent on non-folders (SFGAO_STREAM?)
  752. // and non-link since we know those should never be added
  753. if (SUCCEEDED(hr) && !(dwAttrib & (SFGAO_FOLDER | SFGAO_LINK)))
  754. {
  755. AddToRecentDocs(pidl, szPath);
  756. }
  757. }
  758. }
  759. return hr;
  760. }
  761. BOOL _ShouldPrompt(DWORD cItems)
  762. {
  763. DWORD dwMin, cb = sizeof(dwMin);
  764. if (SHRegGetUSValue(REGSTR_PATH_EXPLORER, TEXT("MultipleInvokePromptMinimum"), NULL, &dwMin, &cb, FALSE, NULL, 0) != ERROR_SUCCESS)
  765. dwMin = 15;
  766. return cItems > dwMin;
  767. }
  768. HRESULT CShellExecMenu::_PromptUser(CMINVOKECOMMANDINFOEX *pici, UINT idVerb, LPIDA pida)
  769. {
  770. HRESULT hr = S_FALSE;
  771. if (pici->hwnd && !(pici->fMask & CMIC_MASK_FLAG_NO_UI)
  772. && _ShouldPrompt(pida->cidl))
  773. {
  774. // prompt the user with the verb and count
  775. // we make a better experience if we keyed off
  776. // homo/hetero types and had different behaviors
  777. // but its not worth it. instead we should
  778. // switch to using AutoPlay sniffing and dialog.
  779. TCHAR szVerb[64];
  780. TCHAR szNum[10];
  781. wnsprintf(szNum, ARRAYSIZE(szNum), TEXT("%d"), pida->cidl);
  782. hr = _GetHelpText(idVerb, GCS_HELPTEXT, (PSTR)szVerb, ARRAYSIZE(szVerb));
  783. if (SUCCEEDED(hr))
  784. {
  785. hr = E_OUTOFMEMORY;
  786. PTSTR pszTitle = ShellConstructMessageString(HINST_THISDLL, MAKEINTRESOURCE(IDS_MULTIINVOKEPROMPT_TITLE), szVerb);
  787. if (pszTitle)
  788. {
  789. PTSTR pszMsg = ShellConstructMessageString(HINST_THISDLL, MAKEINTRESOURCE(IDS_MULTIINVOKEPROMPT_MESSAGE), szVerb, szNum);
  790. if (pszMsg)
  791. {
  792. int iRet = SHMessageBoxCheck(pici->hwnd, pszMsg, pszTitle, (MB_OKCANCEL | MB_ICONEXCLAMATION), IDOK, TEXT("MultipleInvokePrompt"));
  793. hr = iRet == IDOK ? S_OK : HRESULT_FROM_WIN32(ERROR_CANCELLED);
  794. LocalFree(pszMsg);
  795. }
  796. LocalFree(pszTitle);
  797. }
  798. }
  799. }
  800. return hr;
  801. }
  802. HRESULT CShellExecMenu::_InvokeEach(LPCITEMIDLIST pidl, CMINVOKECOMMANDINFOEX *pici)
  803. {
  804. HRESULT hr = E_OUTOFMEMORY;
  805. HMENU hmenu = CreatePopupMenu();
  806. if (hmenu)
  807. {
  808. CComPtr<IContextMenu> spcm;
  809. hr = SHGetUIObjectOf(pidl, NULL, IID_PPV_ARG(IContextMenu, &spcm));
  810. if (SUCCEEDED(hr))
  811. {
  812. if (_punkSite)
  813. IUnknown_SetSite(spcm, _punkSite);
  814. hr = spcm->QueryContextMenu(hmenu, 0, CONTEXTMENU_IDCMD_FIRST, CONTEXTMENU_IDCMD_LAST, _uFlags);
  815. if (SUCCEEDED(hr))
  816. {
  817. hr = spcm->InvokeCommand((CMINVOKECOMMANDINFO *)pici);
  818. }
  819. if (_punkSite)
  820. IUnknown_SetSite(spcm, NULL);
  821. }
  822. DestroyMenu(hmenu);
  823. }
  824. return hr;
  825. }
  826. HRESULT CShellExecMenu::_InvokeMany(CMINVOKECOMMANDINFOEX *pici, UINT idVerb, LPIDA pida)
  827. {
  828. HRESULT hr = _PromptUser(pici, idVerb, pida);
  829. if (SUCCEEDED(hr))
  830. {
  831. USES_CONVERSION;
  832. s_fAbortInvoke = FALSE; // reset this global for this run...
  833. // we want to alter the pici
  834. // so that each item is handled individually
  835. pici->hwnd = NULL;
  836. pici->fMask |= CMIC_MASK_FLAG_NO_UI;
  837. // NTBUG #502223 - MSI apps with DDE start multiple copies - ZekeL 2001-DEC-07
  838. // ShellExec() will create a new thread for MSI apps to
  839. // avoid a deadlock with the MSI APIs calling SHChangeNotify().
  840. // this is described in NTBUG #200961
  841. // however in the multiple invoke case we create one thread
  842. // for each item in the invoke, which results in several processes
  843. // contending for the DDE conversation.
  844. //
  845. // this is a half fix. we prefer to have the buggy behavior in 502223
  846. // over the deadlock behavior in 200961 (a definite PSS call).
  847. // since the deadlock case should only occur for the desktop,
  848. // the rest of the time we will force a synchronous invoke.
  849. IBindCtx *pbcRelease = NULL;
  850. if (!IsDesktopBrowser(_punkSite))
  851. {
  852. TBCRegisterObjectParam(TBCDIDASYNC, SAFECAST(this, IContextMenu *), &pbcRelease);
  853. }
  854. pici->lpVerb = T2A(_dka.ExposeName(idVerb));
  855. pici->lpVerbW = _dka.ExposeName(idVerb);
  856. for (UINT iItem = 0; !_QuitInvokeLoop() && (iItem < pida->cidl); iItem++)
  857. {
  858. LPITEMIDLIST pidl = IDA_FullIDList(pida, iItem);
  859. if (pidl)
  860. {
  861. hr = _InvokeEach(pidl, pici);
  862. ILFree(pidl);
  863. }
  864. else
  865. hr = E_OUTOFMEMORY;
  866. if (hr == E_OUTOFMEMORY)
  867. break;
  868. }
  869. ATOMICRELEASE(pbcRelease);
  870. }
  871. return hr;
  872. }
  873. HRESULT CShellExecMenu::_GetHelpText(UINT idVerb, UINT uType, LPSTR pszName, UINT cchMax)
  874. {
  875. // TODO - shouldnt we let the registry override?
  876. HRESULT hr = E_OUTOFMEMORY;
  877. TCHAR szMenuString[CCH_MENUMAX];
  878. if (_GetMenuString(idVerb, TRUE, szMenuString, ARRAYSIZE(szMenuString)))
  879. {
  880. SHStripMneumonic(szMenuString);
  881. // NOTE on US, IDS_VERBHELP is the same as "%s"
  882. // do we want some better description?
  883. LPTSTR pszHelp = ShellConstructMessageString(HINST_THISDLL, MAKEINTRESOURCE(IDS_VERBHELP), szMenuString);
  884. if (pszHelp)
  885. {
  886. if (uType == GCS_HELPTEXTA)
  887. SHTCharToAnsi(pszHelp, pszName, cchMax);
  888. else
  889. SHTCharToUnicode(pszHelp, (LPWSTR)pszName, cchMax);
  890. LocalFree(pszHelp);
  891. hr = S_OK;
  892. }
  893. }
  894. return hr;
  895. }
  896. HRESULT CShellExecMenu::_MapVerbForGCS(UINT_PTR idCmd, UINT uType, UINT *pidVerb)
  897. {
  898. HRESULT hr = _InsureVerbs();
  899. if (SUCCEEDED(hr))
  900. {
  901. if (IS_INTRESOURCE(idCmd))
  902. *pidVerb = (UINT)idCmd;
  903. else
  904. {
  905. *pidVerb = -1;
  906. if (!(uType & GCS_UNICODE))
  907. {
  908. USES_CONVERSION;
  909. *pidVerb = _FindIndex(A2W((LPCSTR)idCmd));
  910. }
  911. // we fall back to the TCHAR version regardless
  912. // of what the caller passed in uType
  913. if (*pidVerb == -1)
  914. {
  915. if (!IsBadStringPtrW((LPCWSTR)idCmd, (UINT)-1))
  916. *pidVerb = _FindIndex((LPCWSTR)idCmd);
  917. }
  918. }
  919. hr = *pidVerb < _VerbCount() ? S_OK : E_INVALIDARG;
  920. }
  921. // VALIDATE returns S_FALSE for bad verbs
  922. if (FAILED(hr) && (uType == GCS_VALIDATEA || uType == GCS_VALIDATEW))
  923. hr = S_FALSE;
  924. return hr;
  925. }
  926. STDMETHODIMP CShellExecMenu::GetCommandString(UINT_PTR idCmd, UINT uType, UINT *pwRes, LPSTR pszName, UINT cchMax)
  927. {
  928. UINT idVerb;
  929. HRESULT hr = _MapVerbForGCS(idCmd, uType, &idVerb);
  930. if (SUCCEEDED(hr))
  931. {
  932. // the verb is good!
  933. switch (uType)
  934. {
  935. case GCS_HELPTEXTA:
  936. case GCS_HELPTEXTW:
  937. hr = _GetHelpText(idVerb, uType, pszName, cchMax);
  938. break;
  939. case GCS_VERBA:
  940. case GCS_VERBW:
  941. {
  942. if (uType == GCS_VERBA)
  943. SHTCharToAnsi(_dka.ExposeName(idVerb), pszName, cchMax);
  944. else
  945. SHTCharToUnicode(_dka.ExposeName(idVerb), (LPWSTR)pszName, cchMax);
  946. hr = S_OK;
  947. }
  948. break;
  949. case GCS_VALIDATEA:
  950. case GCS_VALIDATEW:
  951. // the hr from MapVerb is good enough
  952. break;
  953. default:
  954. hr = E_NOTIMPL;
  955. break;
  956. }
  957. }
  958. return hr;
  959. }
  960. STDMETHODIMP CShellExecMenu::AddPages(LPFNADDPROPSHEETPAGE pfnAddPage, LPARAM lParam)
  961. {
  962. return _pfnAddPages(_pdtobj, pfnAddPage, lParam);
  963. }
  964. STDMETHODIMP CShellExecMenu::ReplacePage(UINT uPageID, LPFNADDPROPSHEETPAGE pfnReplaceWith, LPARAM lParam)
  965. {
  966. return E_NOTIMPL;
  967. }
  968. STDAPI CShellExecMenu_CreateInstance(LPFNADDPAGES pfnAddPages, REFIID riid, void **ppv)
  969. {
  970. HRESULT hr;
  971. CShellExecMenu *pdext = new CShellExecMenu(pfnAddPages);
  972. if (pdext)
  973. {
  974. hr = pdext->QueryInterface(riid, ppv);
  975. pdext->Release();
  976. }
  977. else
  978. {
  979. *ppv = NULL;
  980. hr = E_OUTOFMEMORY;
  981. }
  982. return hr;
  983. }
  984. // these handlers slime off of CShellExecMenu's IShellPropSheetExt implementation
  985. STDAPI FileSystem_AddPages(IDataObject *pdtobj, LPFNADDPROPSHEETPAGE pfnAddPage, LPARAM lParam);
  986. STDAPI CShellFileDefExt_CreateInstance(IUnknown *punkOuter, REFIID riid, void **ppv)
  987. {
  988. return CShellExecMenu_CreateInstance(FileSystem_AddPages, riid, ppv);
  989. }
  990. STDAPI CDrives_AddPages(IDataObject *pdtobj, LPFNADDPROPSHEETPAGE pfnAddPage, LPARAM lParam);
  991. STDAPI CShellDrvDefExt_CreateInstance(IUnknown *punkOuter, REFIID riid, void **ppv)
  992. {
  993. return CShellExecMenu_CreateInstance(CDrives_AddPages, riid, ppv);
  994. }
  995. #ifdef _X86_
  996. STDAPI PIF_AddPages(IDataObject *pdtobj, LPFNADDPROPSHEETPAGE pfnAddPage, LPARAM lParam);
  997. STDAPI CProxyPage_CreateInstance(IUnknown *punkOuter, REFIID riid, void **ppv)
  998. {
  999. return CShellExecMenu_CreateInstance(PIF_AddPages, riid, ppv);
  1000. }
  1001. #endif