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.

676 lines
14 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: P I D L . C P P
  7. //
  8. // Contents: PIDL utility routines. This stuff is mainly copied from the
  9. // existing Namespace extension samples and real code, since
  10. // everyone and their gramma uses this stuff.
  11. //
  12. // Notes:
  13. //
  14. // Author: jeffspr 1 Oct 1997
  15. //
  16. //----------------------------------------------------------------------------
  17. #include "pch.h"
  18. #pragma hdrstop
  19. #include "shlobj.h"
  20. #include "shlobjp.h"
  21. #include "pidlutil.h"
  22. #if DBG
  23. //+---------------------------------------------------------------------------
  24. //
  25. // Function: ILNext
  26. //
  27. // Purpose: Return the next PIDL in the list
  28. //
  29. // Arguments:
  30. // pidl []
  31. //
  32. // Returns:
  33. //
  34. // Author: jeffspr 1 Oct 1997 (from brianwen)
  35. //
  36. // Notes:
  37. //
  38. LPITEMIDLIST ILNext(LPCITEMIDLIST pidl)
  39. {
  40. if (pidl)
  41. {
  42. pidl = (LPITEMIDLIST) ((BYTE *)pidl + pidl->mkid.cb);
  43. }
  44. return (LPITEMIDLIST)pidl;
  45. }
  46. //+---------------------------------------------------------------------------
  47. //
  48. // Function: ILIsEmpty
  49. //
  50. // Purpose: Is this PIDL empty
  51. //
  52. // Arguments:
  53. // pidl []
  54. //
  55. // Returns:
  56. //
  57. // Author: jeffspr 1 Oct 1997 (from brianwen)
  58. //
  59. // Notes:
  60. //
  61. BOOL ILIsEmpty(LPCITEMIDLIST pidl)
  62. {
  63. return (!pidl || !pidl->mkid.cb);
  64. }
  65. #endif // #if DBG
  66. //+---------------------------------------------------------------------------
  67. //
  68. // Function: ILGetSizePriv
  69. //
  70. // Purpose: Return the size of a pidl.
  71. //
  72. // Arguments:
  73. // pidl []
  74. //
  75. // Returns:
  76. //
  77. // Author: jeffspr 1 Oct 1997 (from brianwen)
  78. //
  79. // Notes:
  80. //
  81. UINT ILGetSizePriv(LPCITEMIDLIST pidl)
  82. {
  83. UINT cbTotal = 0;
  84. if (pidl)
  85. {
  86. cbTotal += sizeof(pidl->mkid.cb); // Null terminator
  87. while (pidl->mkid.cb)
  88. {
  89. cbTotal += pidl->mkid.cb;
  90. pidl = ILNext(pidl);
  91. }
  92. }
  93. return cbTotal;
  94. }
  95. //+---------------------------------------------------------------------------
  96. //
  97. // Function: ILCreate
  98. //
  99. // Purpose: Create a PIDL
  100. //
  101. // Arguments:
  102. // cbSize []
  103. //
  104. // Returns:
  105. //
  106. // Author: jeffspr 1 Oct 1997 (from brianwen)
  107. //
  108. // Notes:
  109. //
  110. LPITEMIDLIST ILCreate(DWORD dwSize)
  111. {
  112. LPITEMIDLIST pidl = (LPITEMIDLIST) SHAlloc(dwSize);
  113. return pidl;
  114. }
  115. VOID FreeIDL(LPITEMIDLIST pidl)
  116. {
  117. Assert(pidl);
  118. SHFree(pidl);
  119. }
  120. //+---------------------------------------------------------------------------
  121. //
  122. // Function: ILIsSingleID
  123. //
  124. // Purpose: Returns TRUE if the idlist has just one ID in it.
  125. //
  126. // Arguments:
  127. // pidl []
  128. //
  129. // Returns:
  130. //
  131. // Author: jeffspr 1 Oct 1997 (from brianwen)
  132. //
  133. // Notes:
  134. //
  135. BOOL ILIsSingleID(LPCITEMIDLIST pidl)
  136. {
  137. if (pidl == NULL)
  138. return FALSE;
  139. return (pidl->mkid.cb == 0 || ILNext(pidl)->mkid.cb == 0);
  140. }
  141. //+---------------------------------------------------------------------------
  142. //
  143. // Function: ILGetCID
  144. //
  145. // Purpose: Returns the number of ID's in the list.
  146. //
  147. // Arguments:
  148. // pidl []
  149. //
  150. // Returns:
  151. //
  152. // Author: jeffspr 1 Oct 1997 (from brianwen)
  153. //
  154. // Notes:
  155. //
  156. UINT ILGetCID(LPCITEMIDLIST pidl)
  157. {
  158. UINT cid = 0;
  159. while (!ILIsEmpty(pidl))
  160. {
  161. ++ cid;
  162. pidl = ILNext(pidl);
  163. }
  164. return cid;
  165. }
  166. //+---------------------------------------------------------------------------
  167. //
  168. // Function: ILGetSizeCID
  169. //
  170. // Purpose: Get the length of the first cid items in a pidl.
  171. //
  172. // Arguments:
  173. // pidl []
  174. // cid []
  175. //
  176. // Returns:
  177. //
  178. // Author: jeffspr 1 Oct 1997 (from brianwen)
  179. //
  180. // Notes:
  181. //
  182. UINT ILGetSizeCID(LPCITEMIDLIST pidl, UINT cid)
  183. {
  184. UINT cbTotal = 0;
  185. if (pidl)
  186. {
  187. cbTotal += sizeof(pidl->mkid.cb); // Null terminator
  188. while (cid && !ILIsEmpty(pidl))
  189. {
  190. cbTotal += pidl->mkid.cb;
  191. pidl = ILNext(pidl);
  192. -- cid;
  193. }
  194. }
  195. return cbTotal;
  196. }
  197. //+---------------------------------------------------------------------------
  198. //
  199. // Function: CloneIDLFirstCID
  200. //
  201. // Purpose: Make a new list consisting of only the first cid items on
  202. // an existing list.
  203. //
  204. // Arguments:
  205. // pidl []
  206. // cid []
  207. //
  208. // Returns:
  209. //
  210. // Author: jeffspr 1 Oct 1997 (from brianwen)
  211. //
  212. // Notes:
  213. //
  214. LPITEMIDLIST CloneIDLFirstCID(LPCITEMIDLIST pidl, UINT cid)
  215. {
  216. Assert((INT)cid >= 0);
  217. UINT cb = ILGetSizeCID(pidl, cid);
  218. LPITEMIDLIST pidlRet = (LPITEMIDLIST) SHAlloc(cb);
  219. if (pidlRet)
  220. {
  221. // Notes: no need to zero-init.
  222. // Also, do not copy the NULL terminator.
  223. memcpy (pidlRet, pidl, cb - sizeof(pidl->mkid.cb));
  224. LPITEMIDLIST pidlTerm = pidlRet;
  225. // Cannot test for NULL terminator, we have not terminated
  226. // the list yet.
  227. //
  228. while (cid)
  229. {
  230. pidlTerm = ILNext(pidlTerm);
  231. -- cid;
  232. }
  233. pidlTerm->mkid.cb = 0;
  234. }
  235. return pidlRet;
  236. }
  237. //+---------------------------------------------------------------------------
  238. //
  239. // Function: ILSkipCID
  240. //
  241. // Purpose: Skips the first cid items in a pidl.
  242. //
  243. // Arguments:
  244. // pidl []
  245. // cid []
  246. //
  247. // Returns:
  248. //
  249. // Author: jeffspr 1 Oct 1997 (from brianwen)
  250. //
  251. // Notes:
  252. //
  253. LPITEMIDLIST ILSkipCID(LPCITEMIDLIST pidl, UINT cid)
  254. {
  255. Assert((INT)cid >= 0);
  256. while (cid && !ILIsEmpty(pidl))
  257. {
  258. pidl = ILNext(pidl);
  259. -- cid;
  260. }
  261. return (LPITEMIDLIST)pidl;
  262. }
  263. //+---------------------------------------------------------------------------
  264. //
  265. // Function: ILCombinePriv
  266. //
  267. // Purpose: Combine two PIDLs
  268. //
  269. // Arguments:
  270. // pidl1 []
  271. // pidl2 []
  272. //
  273. // Returns:
  274. //
  275. // Author: jeffspr 1 Oct 1997 (from brianwen)
  276. //
  277. // Notes:
  278. //
  279. LPITEMIDLIST ILCombinePriv(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
  280. {
  281. LPITEMIDLIST pidlNew = NULL;
  282. // Let me pass in NULL pointers
  283. if (!pidl1)
  284. {
  285. if (!pidl2)
  286. {
  287. pidlNew = NULL;
  288. }
  289. else
  290. {
  291. pidlNew = CloneIDL(pidl2);
  292. }
  293. }
  294. else
  295. {
  296. if (!pidl2)
  297. {
  298. pidlNew = CloneIDL(pidl1);
  299. }
  300. else
  301. {
  302. UINT cb1 = ILGetSizePriv(pidl1) - sizeof(pidl1->mkid.cb);
  303. UINT cb2 = ILGetSizePriv(pidl2);
  304. pidlNew = ILCreate(cb1 + cb2);
  305. if (pidlNew)
  306. {
  307. memcpy(pidlNew, pidl1, cb1);
  308. memcpy((PWSTR)(((LPBYTE)pidlNew) + cb1), pidl2, cb2);
  309. Assert (ILGetSizePriv(pidlNew) == cb1+cb2);
  310. }
  311. }
  312. }
  313. return pidlNew;
  314. }
  315. #if 0
  316. BOOL ILIsEqual(LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
  317. {
  318. if (FSetupGlobalShellFolders())
  319. {
  320. LPSHELLFOLDER psfDesktop = (LPSHELLFOLDER) PvGlobGet (ipsfDesktop);
  321. if (psfDesktop)
  322. {
  323. VERIFYPTR(pidl1, FALSE);
  324. VERIFYPTR(pidl2, FALSE);
  325. return psfDesktop->CompareIDs(0, pidl1, pidl2) == ResultFromShort(0);
  326. }
  327. }
  328. return FALSE;
  329. }
  330. #endif
  331. //+---------------------------------------------------------------------------
  332. //
  333. // Function: CloneIDL
  334. //
  335. // Purpose: Clone an IDL (return a duplicate)
  336. //
  337. // Arguments:
  338. // pidl []
  339. //
  340. // Returns:
  341. //
  342. // Author: jeffspr 1 Oct 1997 (from brianwen)
  343. //
  344. // Notes:
  345. //
  346. LPITEMIDLIST CloneIDL(LPCITEMIDLIST pidl)
  347. {
  348. UINT cb = 0;
  349. LPITEMIDLIST pidlRet = NULL;
  350. if (pidl)
  351. {
  352. cb = ILGetSizePriv(pidl);
  353. pidlRet = (LPITEMIDLIST) SHAlloc(cb);
  354. if (pidlRet)
  355. {
  356. memcpy(pidlRet, pidl, cb);
  357. }
  358. }
  359. return pidlRet;
  360. }
  361. //+---------------------------------------------------------------------------
  362. //
  363. // Function: ILFindLastIDPriv
  364. //
  365. // Purpose: Find the last ID in an IDL
  366. //
  367. // Arguments:
  368. // pidl []
  369. //
  370. // Returns:
  371. //
  372. // Author: jeffspr 1 Oct 1997 (from brianwen)
  373. //
  374. // Notes:
  375. //
  376. LPITEMIDLIST ILFindLastIDPriv(LPCITEMIDLIST pidl)
  377. {
  378. LPCITEMIDLIST pidlLast = pidl;
  379. LPCITEMIDLIST pidlNext = pidl;
  380. Assert(pidl);
  381. // Find the last one in the list
  382. //
  383. while (pidlNext->mkid.cb)
  384. {
  385. pidlLast = pidlNext;
  386. pidlNext = ILNext(pidlLast);
  387. }
  388. return (LPITEMIDLIST)pidlLast;
  389. }
  390. //+---------------------------------------------------------------------------
  391. //
  392. // Function: ILRemoveLastIDPriv
  393. //
  394. // Purpose: Remove the last ID from an IDL
  395. //
  396. // Arguments:
  397. // pidl []
  398. //
  399. // Returns:
  400. //
  401. // Author: jeffspr 1 Oct 1997
  402. //
  403. // Notes:
  404. //
  405. BOOL ILRemoveLastIDPriv(LPITEMIDLIST pidl)
  406. {
  407. BOOL fRemoved = FALSE;
  408. Assert(pidl);
  409. if (pidl->mkid.cb)
  410. {
  411. LPITEMIDLIST pidlLast = (LPITEMIDLIST)ILFindLastIDPriv(pidl);
  412. Assert(pidlLast->mkid.cb);
  413. Assert(ILNext(pidlLast)->mkid.cb==0);
  414. // Remove the last one
  415. pidlLast->mkid.cb = 0; // null-terminator
  416. fRemoved = TRUE;
  417. }
  418. return fRemoved;
  419. }
  420. //+---------------------------------------------------------------------------
  421. //
  422. // Function: CloneRgIDL
  423. //
  424. // Purpose: Clone a pidl array
  425. //
  426. // Arguments:
  427. // rgpidl [in] PIDL array to clone
  428. // cidl [in] Count of the pidl array
  429. // fUseCache [in] If TRUE, generate the returned IDL from the cache
  430. // fAllowNonCacheItems [in] Use old version of pidl if cached version non available
  431. // pppidl [out] Return pointer for pidl array
  432. //
  433. // Returns:
  434. //
  435. // Author: jeffspr 22 Oct 1997
  436. //
  437. // Notes:
  438. //
  439. /*
  440. HRESULT HrCloneRgIDL(
  441. LPCITEMIDLIST * rgpidl,
  442. ULONG cidl,
  443. BOOL fUseCache,
  444. BOOL fAllowNonCacheItems,
  445. LPITEMIDLIST ** pppidl,
  446. ULONG * pcidl)
  447. {
  448. HRESULT hr = NOERROR;
  449. LPITEMIDLIST * rgpidlReturn = NULL;
  450. ULONG irg = 0;
  451. ULONG cidlCopied = 0;
  452. Assert(pppidl);
  453. Assert(pcidl);
  454. Assert(rgpidl);
  455. if (!rgpidl || !cidl)
  456. {
  457. hr = E_INVALIDARG;
  458. goto Exit;
  459. }
  460. else
  461. {
  462. // Alloc the return buffer
  463. //
  464. rgpidlReturn = (LPITEMIDLIST *) SHAlloc(cidl * sizeof(LPITEMIDLIST));
  465. if (!rgpidlReturn)
  466. {
  467. hr = E_OUTOFMEMORY;
  468. goto Exit;
  469. }
  470. else
  471. {
  472. // Clone all elements within the passed in PIDL array
  473. //
  474. for (irg = 0; irg < cidl; irg++)
  475. {
  476. if (rgpidl[irg])
  477. {
  478. if (fUseCache)
  479. {
  480. PCONNLISTENTRY pcle = NULL;
  481. PCONFOLDPIDL pcfp = (PCONFOLDPIDL) rgpidl[irg];
  482. hr = g_ccl.HrFindConnectionByGuid(&(pcfp->guidId), &pcle);
  483. if (hr == S_OK)
  484. {
  485. Assert(pcle);
  486. Assert(pcle->pccfe);
  487. // Copy to the return pidl array.
  488. hr = HrConFoldEntryToPidl(pcle->pccfe, &(rgpidlReturn[cidlCopied++]));
  489. if (FAILED(hr))
  490. goto Exit;
  491. }
  492. else
  493. {
  494. TraceTag(ttidShellFolder, "HrCloneRgIDL: Connection find returned: 0x%08x", hr);
  495. if (hr == S_FALSE)
  496. {
  497. if (fAllowNonCacheItems)
  498. {
  499. TraceTag(ttidShellFolder, "HrCloneRgIDL: Connection not found in cache, "
  500. "using non-cache item");
  501. rgpidlReturn[cidlCopied++] = CloneIDL((LPITEMIDLIST)rgpidl[irg]);
  502. if (!rgpidlReturn[irg])
  503. {
  504. hr = E_OUTOFMEMORY;
  505. goto Exit;
  506. }
  507. }
  508. else
  509. {
  510. TraceTag(ttidShellFolder, "HrCloneRgIDL: Connection not found in cache. "
  511. "Dropping item from array");
  512. }
  513. }
  514. else
  515. {
  516. AssertSz(FALSE, "HrCloneRgIDL: Connection find HR_FAILED");
  517. }
  518. }
  519. }
  520. else
  521. {
  522. // Clone this element in the PIDL array
  523. //
  524. rgpidlReturn[cidlCopied++] = CloneIDL ((LPITEMIDLIST) rgpidl[irg]);
  525. if (!rgpidlReturn[irg])
  526. {
  527. hr = E_OUTOFMEMORY;
  528. goto Exit;
  529. }
  530. }
  531. }
  532. else
  533. {
  534. // Make sure that we don't try to delete bogus data later.
  535. //
  536. rgpidlReturn[cidlCopied++] = NULL;
  537. AssertSz(FALSE, "Bogus element in the rgpidl in HrCloneRgIDL");
  538. hr = E_INVALIDARG;
  539. goto Exit;
  540. }
  541. }
  542. }
  543. }
  544. Exit:
  545. if (FAILED(hr))
  546. {
  547. // Free the already-allocated IDLISTs
  548. //
  549. ULONG irgT = 0;
  550. for (irgT = 0; irgT < irg; irgT++)
  551. {
  552. if (rgpidlReturn[irgT])
  553. {
  554. FreeIDL(rgpidlReturn[irgT]);
  555. }
  556. }
  557. SHFree(rgpidlReturn);
  558. *pppidl = NULL;
  559. }
  560. else
  561. {
  562. // Fill in the return var.
  563. //
  564. *pppidl = rgpidlReturn;
  565. *pcidl = cidlCopied;
  566. }
  567. TraceHr(ttidError, FAL, hr, FALSE, "HrCloneRgIDL");
  568. return hr;
  569. } // CloneRgIDL
  570. */
  571. //+---------------------------------------------------------------------------
  572. //
  573. // Function: FreeRgIDL
  574. //
  575. // Purpose: Free a PIDL array
  576. //
  577. // Arguments:
  578. // cidl [in] Size of PIDL array
  579. // apidl [in] Pointer to the array itself.
  580. //
  581. // Returns:
  582. //
  583. // Author: jeffspr 27 Oct 1997
  584. //
  585. // Notes:
  586. //
  587. VOID FreeRgIDL(
  588. UINT cidl,
  589. LPITEMIDLIST * apidl)
  590. {
  591. if (apidl)
  592. {
  593. for (UINT i = 0; i < cidl; i++)
  594. {
  595. FreeIDL(apidl[i]);
  596. }
  597. SHFree(apidl);
  598. }
  599. }