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.

1188 lines
29 KiB

  1. //*******************************************************************************************
  2. //
  3. // Filename : DataObj.cpp
  4. //
  5. // Implementation file for CObjFormats and CCabObj
  6. //
  7. // Copyright (c) 1994 - 1996 Microsoft Corporation. All rights reserved
  8. //
  9. //*******************************************************************************************
  10. #include "pch.h"
  11. #include "ccstock.h"
  12. #include "thisdll.h"
  13. #include "folder.h"
  14. #include "dataobj.h"
  15. #include "cabitms.h"
  16. UINT CCabObj::s_uFileGroupDesc = 0;
  17. UINT CCabObj::s_uFileContents = 0;
  18. UINT CCabObj::s_uPersistedDataObject = 0;
  19. UINT CCabObj::s_uHIDA = 0;
  20. // {dfe49cfe-cd09-11d2-9643-00c04f79adf0}
  21. const GUID CLSID_CabViewDataObject = {0xdfe49cfe, 0xcd09, 0x11d2, 0x96, 0x43, 0x00, 0xc0, 0x4f, 0x79, 0xad, 0xf0};
  22. #define MAX_CHUNK (60*1024) /* max mouthful to CopyTo from CCabStream */
  23. class CCabStream : public IStream
  24. {
  25. public:
  26. CCabStream(HGLOBAL hStream,DWORD dwStreamLength);
  27. ~CCabStream(void);
  28. // *** IUnknown methods ***
  29. STDMETHODIMP QueryInterface(REFIID riid, LPVOID * ppvObj);
  30. STDMETHODIMP_(ULONG) AddRef();
  31. STDMETHODIMP_(ULONG) Release();
  32. // *** IStream methods ***
  33. STDMETHODIMP Read(void *pv,ULONG cb,ULONG *pcbRead);
  34. STDMETHODIMP Write(const void *pv,ULONG cb,ULONG *pcbWritten);
  35. STDMETHODIMP Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER * plibNewPosition);
  36. STDMETHODIMP SetSize(ULARGE_INTEGER libNewSize);
  37. STDMETHODIMP CopyTo(IStream * pstm, ULARGE_INTEGER cb, ULARGE_INTEGER * pcbRead, ULARGE_INTEGER * pcbWritten);
  38. STDMETHODIMP Commit(DWORD grfCommitFlags);
  39. STDMETHODIMP Revert(void);
  40. STDMETHODIMP LockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
  41. STDMETHODIMP UnlockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
  42. STDMETHODIMP Stat(STATSTG * pstatstg, DWORD grfStatFlag);
  43. STDMETHODIMP Clone(IStream ** ppstm);
  44. private:
  45. CRefCount m_cRef;
  46. CRefDll m_cRefDll;
  47. HGLOBAL m_hStream;
  48. DWORD m_dwStreamLength;
  49. DWORD m_dwStreamPosition;
  50. };
  51. CCabStream::CCabStream(HGLOBAL hStream,DWORD dwStreamLength)
  52. {
  53. m_hStream = hStream;
  54. m_dwStreamLength = dwStreamLength;
  55. m_dwStreamPosition = 0;
  56. AddRef();
  57. }
  58. CCabStream::~CCabStream(void)
  59. {
  60. GlobalFree(m_hStream);
  61. }
  62. STDMETHODIMP CCabStream::QueryInterface(
  63. REFIID riid,
  64. LPVOID FAR* ppvObj)
  65. {
  66. *ppvObj = NULL;
  67. LPUNKNOWN pObj;
  68. if (riid == IID_IUnknown)
  69. {
  70. pObj = (IUnknown*)((IStream *)this);
  71. }
  72. else if (riid == IID_IStream)
  73. {
  74. pObj = (IUnknown*)((IStream *)this);
  75. }
  76. else
  77. {
  78. return(E_NOINTERFACE);
  79. }
  80. pObj->AddRef();
  81. *ppvObj = pObj;
  82. return(NOERROR);
  83. }
  84. STDMETHODIMP_(ULONG) CCabStream::AddRef(void)
  85. {
  86. return(m_cRef.AddRef());
  87. }
  88. STDMETHODIMP_(ULONG) CCabStream::Release(void)
  89. {
  90. if (!m_cRef.Release())
  91. {
  92. delete this;
  93. return(0);
  94. }
  95. return(m_cRef.GetRef());
  96. }
  97. STDMETHODIMP CCabStream::Read(void * pv, ULONG cb, ULONG * pcbRead)
  98. {
  99. *pcbRead = 0;
  100. if (m_dwStreamPosition < m_dwStreamLength)
  101. {
  102. if (cb > (m_dwStreamLength - m_dwStreamPosition))
  103. {
  104. *pcbRead = (m_dwStreamLength - m_dwStreamPosition);
  105. }
  106. else
  107. {
  108. *pcbRead = cb;
  109. }
  110. CopyMemory(pv,(char *) m_hStream + m_dwStreamPosition,*pcbRead);
  111. m_dwStreamPosition += *pcbRead;
  112. }
  113. return(S_OK);
  114. }
  115. STDMETHODIMP CCabStream::Write(const void * pv, ULONG cb, ULONG * pcbWritten)
  116. {
  117. return(E_NOTIMPL);
  118. }
  119. STDMETHODIMP CCabStream::Seek(LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER * plibNewPosition)
  120. {
  121. switch (dwOrigin)
  122. {
  123. case STREAM_SEEK_SET:
  124. m_dwStreamPosition = dlibMove.LowPart;
  125. break;
  126. case STREAM_SEEK_CUR:
  127. m_dwStreamPosition += dlibMove.LowPart;
  128. break;
  129. case STREAM_SEEK_END:
  130. m_dwStreamPosition = m_dwStreamLength + dlibMove.LowPart;
  131. break;
  132. default:
  133. return(STG_E_INVALIDFUNCTION);
  134. }
  135. if (plibNewPosition)
  136. {
  137. (*plibNewPosition).LowPart = m_dwStreamPosition;
  138. (*plibNewPosition).HighPart = 0;
  139. }
  140. return(S_OK);
  141. }
  142. STDMETHODIMP CCabStream::SetSize(ULARGE_INTEGER libNewSize)
  143. {
  144. return(E_NOTIMPL);
  145. }
  146. STDMETHODIMP CCabStream::CopyTo(IStream * pstm, ULARGE_INTEGER cb, ULARGE_INTEGER * pcbRead, ULARGE_INTEGER * pcbWritten)
  147. {
  148. HRESULT hRes;
  149. unsigned long cbActual = cb.LowPart;
  150. unsigned long cbWritten;
  151. unsigned long cbChunk;
  152. if (pcbRead)
  153. {
  154. (*pcbRead).LowPart = 0;
  155. (*pcbRead).HighPart = 0;
  156. }
  157. if (pcbWritten)
  158. {
  159. (*pcbWritten).LowPart = 0;
  160. (*pcbWritten).HighPart = 0;
  161. }
  162. hRes = S_OK;
  163. if (m_dwStreamPosition < m_dwStreamLength)
  164. {
  165. if (cbActual > (m_dwStreamLength - m_dwStreamPosition))
  166. {
  167. cbActual = (m_dwStreamLength - m_dwStreamPosition);
  168. }
  169. while (cbActual)
  170. {
  171. if (cbActual > MAX_CHUNK)
  172. {
  173. cbChunk = MAX_CHUNK;
  174. }
  175. else
  176. {
  177. cbChunk = cbActual;
  178. }
  179. hRes = pstm->Write((char *) m_hStream + m_dwStreamPosition,cbChunk,&cbWritten);
  180. if (FAILED(hRes))
  181. {
  182. break;
  183. }
  184. m_dwStreamPosition += cbChunk;
  185. if (pcbRead)
  186. {
  187. (*pcbRead).LowPart += cbChunk;
  188. (*pcbRead).HighPart = 0;
  189. }
  190. if (pcbWritten)
  191. {
  192. (*pcbWritten).LowPart += cbWritten;
  193. (*pcbWritten).HighPart = 0;
  194. }
  195. cbActual -= cbChunk;
  196. }
  197. }
  198. return(hRes);
  199. }
  200. STDMETHODIMP CCabStream::Commit(DWORD grfCommitFlags)
  201. {
  202. return(E_NOTIMPL);
  203. }
  204. STDMETHODIMP CCabStream::Revert(void)
  205. {
  206. return(E_NOTIMPL);
  207. }
  208. STDMETHODIMP CCabStream::LockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
  209. {
  210. return(E_NOTIMPL);
  211. }
  212. STDMETHODIMP CCabStream::UnlockRegion(ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
  213. {
  214. return(E_NOTIMPL);
  215. }
  216. STDMETHODIMP CCabStream::Stat(STATSTG * pstatstg, DWORD grfStatFlag)
  217. {
  218. return(E_NOTIMPL);
  219. }
  220. STDMETHODIMP CCabStream::Clone(IStream ** ppstm)
  221. {
  222. return(E_NOTIMPL);
  223. }
  224. class CObjFormats : public IEnumFORMATETC
  225. {
  226. public:
  227. CObjFormats(UINT cfmt, const FORMATETC afmt[]);
  228. ~CObjFormats();
  229. // *** IUnknown methods ***
  230. STDMETHODIMP QueryInterface(REFIID riid, LPVOID * ppvObj);
  231. STDMETHODIMP_(ULONG) AddRef();
  232. STDMETHODIMP_(ULONG) Release();
  233. // *** IEnumFORMATETC methods ***
  234. STDMETHODIMP Next(ULONG celt, FORMATETC *rgelt, ULONG *pceltFethed);
  235. STDMETHODIMP Skip(ULONG celt);
  236. STDMETHODIMP Reset();
  237. STDMETHODIMP Clone(IEnumFORMATETC ** ppenum);
  238. private:
  239. CRefCount m_cRef;
  240. CRefDll m_cRefDll;
  241. UINT m_iFmt;
  242. UINT m_cFmt;
  243. FORMATETC *m_aFmt;
  244. } ;
  245. CObjFormats::CObjFormats(UINT cfmt, const FORMATETC afmt[])
  246. {
  247. m_iFmt = 0;
  248. m_cFmt = cfmt;
  249. m_aFmt = new FORMATETC[cfmt];
  250. if (m_aFmt)
  251. {
  252. CopyMemory(m_aFmt, afmt, cfmt*sizeof(afmt[0]));
  253. }
  254. }
  255. CObjFormats::~CObjFormats()
  256. {
  257. if (m_aFmt)
  258. {
  259. delete m_aFmt;
  260. }
  261. }
  262. // *** IUnknown methods ***
  263. STDMETHODIMP CObjFormats::QueryInterface(
  264. REFIID riid,
  265. LPVOID FAR* ppvObj)
  266. {
  267. *ppvObj = NULL;
  268. if (!m_aFmt)
  269. {
  270. return(E_OUTOFMEMORY);
  271. }
  272. LPUNKNOWN pObj;
  273. if (riid == IID_IUnknown)
  274. {
  275. pObj = (IUnknown*)((IEnumFORMATETC*)this);
  276. }
  277. else if (riid == IID_IEnumFORMATETC)
  278. {
  279. pObj = (IUnknown*)((IEnumFORMATETC*)this);
  280. }
  281. else
  282. {
  283. return(E_NOINTERFACE);
  284. }
  285. pObj->AddRef();
  286. *ppvObj = pObj;
  287. return(NOERROR);
  288. }
  289. STDMETHODIMP_(ULONG) CObjFormats::AddRef(void)
  290. {
  291. return(m_cRef.AddRef());
  292. }
  293. STDMETHODIMP_(ULONG) CObjFormats::Release(void)
  294. {
  295. if (!m_cRef.Release())
  296. {
  297. delete this;
  298. return(0);
  299. }
  300. return(m_cRef.GetRef());
  301. }
  302. STDMETHODIMP CObjFormats::Next(ULONG celt, FORMATETC *rgelt, ULONG *pceltFethed)
  303. {
  304. UINT cfetch;
  305. HRESULT hres = S_FALSE; // assume less numbers
  306. if (m_iFmt < m_cFmt)
  307. {
  308. cfetch = m_cFmt - m_iFmt;
  309. if (cfetch >= celt)
  310. {
  311. cfetch = celt;
  312. hres = S_OK;
  313. }
  314. CopyMemory(rgelt, &m_aFmt[m_iFmt], cfetch * sizeof(FORMATETC));
  315. m_iFmt += cfetch;
  316. }
  317. else
  318. {
  319. cfetch = 0;
  320. }
  321. if (pceltFethed)
  322. {
  323. *pceltFethed = cfetch;
  324. }
  325. return hres;
  326. }
  327. STDMETHODIMP CObjFormats::Skip(ULONG celt)
  328. {
  329. m_iFmt += celt;
  330. if (m_iFmt > m_cFmt)
  331. {
  332. m_iFmt = m_cFmt;
  333. return S_FALSE;
  334. }
  335. return S_OK;
  336. }
  337. STDMETHODIMP CObjFormats::Reset()
  338. {
  339. m_iFmt = 0;
  340. return S_OK;
  341. }
  342. STDMETHODIMP CObjFormats::Clone(IEnumFORMATETC ** ppenum)
  343. {
  344. return(E_NOTIMPL);
  345. }
  346. HRESULT CabViewDataObject_CreateInstance(REFIID riid, LPVOID *ppvObj)
  347. {
  348. HRESULT hr;
  349. CCabObj* pco = new CCabObj();
  350. if (NULL != pco)
  351. {
  352. pco->AddRef();
  353. hr = pco->QueryInterface(riid, ppvObj);
  354. pco->Release();
  355. }
  356. else
  357. {
  358. hr = E_OUTOFMEMORY;
  359. }
  360. return hr;
  361. }
  362. CCabObj::CCabObj(HWND hwndOwner, CCabFolder *pcf, LPCABITEM *apit, UINT cpit)
  363. : m_lSel(8), m_lContents(NULL)
  364. {
  365. m_pcfHere = pcf;
  366. pcf->AddRef();
  367. m_hwndOwner = hwndOwner;
  368. m_lSel.AddItems(apit, cpit);
  369. }
  370. // constructor used when we're co-created:
  371. CCabObj::CCabObj() : m_pcfHere(NULL), m_hwndOwner(NULL), m_lSel(8), m_lContents(NULL)
  372. {
  373. };
  374. CCabObj::~CCabObj()
  375. {
  376. if (m_lContents != NULL)
  377. {
  378. int cItems = m_lSel.GetCount();
  379. while (cItems--)
  380. {
  381. if (m_lContents[cItems] != NULL)
  382. {
  383. GlobalFree(m_lContents[cItems]);
  384. m_lContents[cItems] = NULL;
  385. }
  386. }
  387. GlobalFree(m_lContents);
  388. m_lContents = NULL;
  389. }
  390. if (m_pcfHere)
  391. {
  392. m_pcfHere->Release();
  393. }
  394. }
  395. // *** IUnknown methods ***
  396. STDMETHODIMP CCabObj::QueryInterface(
  397. REFIID riid,
  398. LPVOID FAR* ppvObj)
  399. {
  400. *ppvObj = NULL;
  401. if (m_lSel.GetState() == CCabItemList::State_OutOfMem)
  402. {
  403. return(E_OUTOFMEMORY);
  404. }
  405. LPUNKNOWN pObj;
  406. if (riid == IID_IUnknown)
  407. {
  408. pObj = (IUnknown*)((IDataObject*)this);
  409. }
  410. else if (riid == IID_IDataObject)
  411. {
  412. pObj = (IUnknown*)((IDataObject*)this);
  413. }
  414. else if (riid == IID_IPersist)
  415. {
  416. pObj = (IUnknown*)((IPersist*)this);
  417. }
  418. else if (riid == IID_IPersistStream)
  419. {
  420. pObj = (IUnknown*)((IPersistStream*)this);
  421. }
  422. else
  423. {
  424. return(E_NOINTERFACE);
  425. }
  426. pObj->AddRef();
  427. *ppvObj = pObj;
  428. return(NOERROR);
  429. }
  430. STDMETHODIMP_(ULONG) CCabObj::AddRef(void)
  431. {
  432. return(m_cRef.AddRef());
  433. }
  434. STDMETHODIMP_(ULONG) CCabObj::Release(void)
  435. {
  436. if (!m_cRef.Release())
  437. {
  438. delete this;
  439. return(0);
  440. }
  441. return(m_cRef.GetRef());
  442. }
  443. /////////////////////////////////
  444. ////// IPersistStream Impl
  445. /////////////////////////////////
  446. typedef struct
  447. {
  448. DWORD dwVersion;
  449. DWORD dwExtraSize; // After pidl list
  450. DWORD dwReserved1;
  451. DWORD dwReserved2;
  452. } CABVIEWDATAOBJ_PERSISTSTRUCT;
  453. /*****************************************************************************\
  454. FUNCTION: IPersistStream::Load
  455. DESCRIPTION:
  456. See IPersistStream::Save() for the layout of the stream.
  457. \*****************************************************************************/
  458. HRESULT CCabObj::Load(IStream *pStm)
  459. {
  460. HRESULT hr = E_INVALIDARG;
  461. if (pStm)
  462. {
  463. CABVIEWDATAOBJ_PERSISTSTRUCT cvdops = { 0 };
  464. hr = pStm->Read(&cvdops, sizeof(cvdops), NULL); // #1
  465. // If we rev the version, read it now (cvdops.dwVersion)
  466. if (S_OK == hr)
  467. {
  468. LPITEMIDLIST pidl = NULL; // ILLoadFromStream frees the param
  469. // ASSERT(!m_pff);
  470. hr = ILLoadFromStream(pStm, &pidl); // #2
  471. if (SUCCEEDED(hr))
  472. {
  473. ATOMICRELEASE(m_pcfHere);
  474. hr = SHBindToObject(NULL, CLSID_CabFolder, pidl, (void **)&m_pcfHere);
  475. ILFree(pidl);
  476. }
  477. }
  478. DWORD dwNumPidls;
  479. if (SUCCEEDED(hr))
  480. {
  481. hr = pStm->Read(&dwNumPidls, SIZEOF(dwNumPidls), NULL); // #3
  482. }
  483. if (S_OK == hr)
  484. {
  485. for (int nIndex = 0; (nIndex < (int)dwNumPidls) && SUCCEEDED(hr); nIndex++)
  486. {
  487. LPITEMIDLIST pidl = NULL; // ILLoadFromStream frees the param
  488. hr = ILLoadFromStream(pStm, &pidl); // #4
  489. if (SUCCEEDED(hr))
  490. {
  491. hr = m_lSel.AddItems((LPCABITEM*) &pidl, 1);
  492. ILFree(pidl);
  493. }
  494. }
  495. }
  496. if (SUCCEEDED(hr))
  497. {
  498. // We may be reading a version newer than us, so skip their data.
  499. if (0 != cvdops.dwExtraSize)
  500. {
  501. LARGE_INTEGER li = {0};
  502. li.LowPart = cvdops.dwExtraSize;
  503. hr = pStm->Seek(li, STREAM_SEEK_CUR, NULL);
  504. }
  505. }
  506. }
  507. // don't return success codes other than S_OK:
  508. return SUCCEEDED(hr) ? S_OK : hr;
  509. }
  510. /*****************************************************************************\
  511. FUNCTION: IPersistStream::Save
  512. DESCRIPTION:
  513. The stream will be layed out in the following way:
  514. Version 1:
  515. 1. CABVIEWDATAOBJ_PERSISTSTRUCT - Constant sized data.
  516. <PidlList BEGIN>
  517. 2. PIDL pidl - Pidl for m_pcfHere. It will be a public pidl (fully qualified
  518. from the shell root)
  519. 3. DWORD dwNumPidls - Number of pidls coming.
  520. 4. PIDL pidl(n) - Pidl in slot (n) of m_lSel
  521. <PidlList END>
  522. \*****************************************************************************/
  523. HRESULT CCabObj::Save(IStream *pStm, BOOL fClearDirty)
  524. {
  525. HRESULT hr = E_INVALIDARG;
  526. if (pStm)
  527. {
  528. CABVIEWDATAOBJ_PERSISTSTRUCT cvdops = {0};
  529. DWORD dwNumPidls = m_lSel.GetCount();
  530. cvdops.dwVersion = 1;
  531. hr = pStm->Write(&cvdops, sizeof(cvdops), NULL); // #1
  532. if (SUCCEEDED(hr))
  533. {
  534. if (m_pcfHere)
  535. {
  536. LPITEMIDLIST pidl;
  537. hr = m_pcfHere->GetCurFolder(&pidl);
  538. if (SUCCEEDED(hr))
  539. {
  540. hr = ILSaveToStream(pStm, pidl); // #2
  541. ILFree(pidl);
  542. }
  543. }
  544. else
  545. {
  546. hr = E_FAIL;
  547. }
  548. }
  549. if (SUCCEEDED(hr))
  550. hr = pStm->Write(&dwNumPidls, SIZEOF(dwNumPidls), NULL); // #3
  551. if (SUCCEEDED(hr))
  552. {
  553. for (int nIndex = 0; (nIndex < (int)dwNumPidls) && SUCCEEDED(hr); nIndex++)
  554. {
  555. hr = ILSaveToStream(pStm, (LPCITEMIDLIST) m_lSel[nIndex]); // #4
  556. }
  557. }
  558. }
  559. return hr;
  560. }
  561. #define MAX_STREAM_SIZE (500 * 1024) // 500k
  562. /*****************************************************************************\
  563. FUNCTION: IPersistStream::GetSizeMax
  564. DESCRIPTION:
  565. Now this is tough. I can't calculate the real value because I don't know
  566. how big the hglobals are going to be for the user provided data. I will
  567. assume everything fits in
  568. \*****************************************************************************/
  569. HRESULT CCabObj::GetSizeMax(ULARGE_INTEGER * pcbSize)
  570. {
  571. if (pcbSize)
  572. {
  573. pcbSize->HighPart = 0;
  574. pcbSize->LowPart = MAX_STREAM_SIZE;
  575. }
  576. return E_NOTIMPL;
  577. }
  578. /*****************************************************************************\
  579. DESCRIPTION:
  580. Allocate an hglobal of the indicated size, initialized from the
  581. specified buffer.
  582. \*****************************************************************************/
  583. HRESULT StgMediumWriteHGlobal(HGLOBAL *phglob, LPVOID pv, SIZE_T cb)
  584. {
  585. HRESULT hres = E_OUTOFMEMORY;
  586. *phglob = 0; // Rules are rules
  587. if (cb)
  588. {
  589. *phglob = (HGLOBAL) LocalAlloc(LPTR, cb);
  590. if (*phglob)
  591. {
  592. hres = S_OK;
  593. CopyMemory(*phglob, pv, cb);
  594. }
  595. }
  596. else
  597. hres = E_INVALIDARG; // Can't clone a discardable block
  598. return hres;
  599. }
  600. /*****************************************************************************\
  601. DESCRIPTION:
  602. When the copy source goes away (the process shuts down), it calls
  603. OleFlushClipboard. OLE will then copy our data, release us, and then
  604. give out our data later. This works for most things except for:
  605. 1. When lindex needs to very. This doesn't work because ole doesn't know
  606. how to ask us how may lindexs they need to copy.
  607. 2. If this object has a private interface OLE doesn't know about. For us,
  608. it's IAsyncOperation.
  609. To get around this problem, we want OLE to recreate us when some possible
  610. paste target calls OleGetClipboard. We want OLE to call OleLoadFromStream()
  611. to have us CoCreated and reload our persisted data via IPersistStream.
  612. OLE doesn't want to do this by default or they may have backward compat
  613. problems so they want a sign from the heavens, or at least from us, that
  614. we will work. They ping our "OleClipboardPersistOnFlush" clipboard format
  615. to ask this.
  616. \*****************************************************************************/
  617. HRESULT _RenderOlePersist(STGMEDIUM * pStgMedium)
  618. {
  619. // The actual cookie value is opaque to the outside world. Since
  620. // we don't use it either, we just leave it at zero in case we use
  621. // it in the future. It's mere existence will cause OLE to do the
  622. // use our IPersistStream, which is what we want.
  623. DWORD dwCookie = 0;
  624. return StgMediumWriteHGlobal(&pStgMedium->hGlobal, &dwCookie, sizeof(dwCookie));
  625. }
  626. STDMETHODIMP CCabObj::GetData(FORMATETC *pfmt, STGMEDIUM *pmedium)
  627. {
  628. ZeroMemory(pmedium, sizeof(*pmedium));
  629. if (!InitFileGroupDesc())
  630. {
  631. return(E_UNEXPECTED);
  632. }
  633. if (pfmt->cfFormat == s_uFileGroupDesc)
  634. {
  635. if (pfmt->ptd==NULL && (pfmt->dwAspect&DVASPECT_CONTENT) && pfmt->lindex==-1
  636. && (pfmt->tymed&TYMED_HGLOBAL))
  637. {
  638. int cItems = m_lSel.GetCount();
  639. if (cItems < 1)
  640. {
  641. return(E_UNEXPECTED);
  642. }
  643. FILEGROUPDESCRIPTOR *pfgd = (FILEGROUPDESCRIPTOR *)GlobalAlloc(GMEM_FIXED,
  644. sizeof(FILEGROUPDESCRIPTOR) + (cItems-1)*sizeof(FILEDESCRIPTOR));
  645. if (!pfgd)
  646. {
  647. return(E_OUTOFMEMORY);
  648. }
  649. pfgd->cItems = cItems;
  650. for (--cItems; cItems>=0; --cItems)
  651. {
  652. LPCABITEM pItem = m_lSel[cItems];
  653. FILETIME ft;
  654. pfgd->fgd[cItems].dwFlags = FD_ATTRIBUTES|FD_WRITESTIME|FD_FILESIZE|FD_PROGRESSUI;
  655. pfgd->fgd[cItems].dwFileAttributes = pItem->uFileAttribs;
  656. DosDateTimeToFileTime(pItem->uFileDate, pItem->uFileTime,&ft);
  657. LocalFileTimeToFileTime(&ft, &pfgd->fgd[cItems].ftLastWriteTime);
  658. pfgd->fgd[cItems].nFileSizeHigh = 0;
  659. pfgd->fgd[cItems].nFileSizeLow = pItem->dwFileSize;
  660. LPCWSTR pszName;
  661. WSTR_ALIGNED_STACK_COPY(&pszName, pItem->szName);
  662. lstrcpyn(pfgd->fgd[cItems].cFileName, PathFindFileName(pszName),
  663. ARRAYSIZE(pfgd->fgd[cItems].cFileName));
  664. }
  665. pmedium->tymed = TYMED_HGLOBAL;
  666. pmedium->hGlobal = (HGLOBAL)pfgd;
  667. pmedium->pUnkForRelease = NULL;
  668. return(NOERROR);
  669. }
  670. return(E_INVALIDARG);
  671. }
  672. if (!InitFileContents())
  673. {
  674. return(E_UNEXPECTED);
  675. }
  676. if (pfmt->cfFormat == s_uFileContents)
  677. {
  678. if (pfmt->ptd==NULL && (pfmt->dwAspect&DVASPECT_CONTENT))
  679. {
  680. if (pfmt->tymed & TYMED_ISTREAM)
  681. {
  682. int cItems = m_lSel.GetCount();
  683. if ((pfmt->lindex < 0) || (pfmt->lindex >= cItems))
  684. {
  685. return(E_INVALIDARG);
  686. }
  687. HRESULT hRes = InitContents();
  688. if (FAILED(hRes))
  689. {
  690. return(hRes);
  691. }
  692. if (!m_lContents[pfmt->lindex])
  693. {
  694. return(E_OUTOFMEMORY);
  695. }
  696. CCabStream *stream = new CCabStream(m_lContents[pfmt->lindex],m_lSel[pfmt->lindex]->dwFileSize);
  697. if (!stream)
  698. {
  699. return(E_OUTOFMEMORY);
  700. }
  701. pmedium->tymed = TYMED_ISTREAM;
  702. pmedium->pstm = stream;
  703. pmedium->pUnkForRelease = NULL;
  704. m_lContents[pfmt->lindex] = NULL;
  705. return(NOERROR);
  706. }
  707. if (pfmt->tymed&TYMED_HGLOBAL)
  708. {
  709. int cItems = m_lSel.GetCount();
  710. if (pfmt->lindex >= cItems)
  711. {
  712. return(E_INVALIDARG);
  713. }
  714. HRESULT hRes = InitContents();
  715. if (FAILED(hRes))
  716. {
  717. return(hRes);
  718. }
  719. if (!m_lContents[pfmt->lindex])
  720. {
  721. return(E_OUTOFMEMORY);
  722. }
  723. pmedium->tymed = TYMED_HGLOBAL;
  724. pmedium->hGlobal = m_lContents[pfmt->lindex];
  725. pmedium->pUnkForRelease = NULL;
  726. m_lContents[pfmt->lindex] = NULL;
  727. return(NOERROR);
  728. }
  729. }
  730. return(E_INVALIDARG);
  731. }
  732. if (!InitPersistedDataObject())
  733. {
  734. return E_UNEXPECTED;
  735. }
  736. if (pfmt->cfFormat == s_uPersistedDataObject)
  737. {
  738. if ((pfmt->ptd == NULL) &&
  739. (pfmt->dwAspect & DVASPECT_CONTENT) &&
  740. (pfmt->lindex == -1) &&
  741. (pfmt->tymed & TYMED_HGLOBAL))
  742. {
  743. return _RenderOlePersist(pmedium);
  744. }
  745. else
  746. {
  747. return E_INVALIDARG;
  748. }
  749. }
  750. if (!InitHIDA())
  751. {
  752. return E_UNEXPECTED;
  753. }
  754. if (pfmt->cfFormat == s_uHIDA)
  755. {
  756. HRESULT hr = E_INVALIDARG;
  757. if ((pfmt->ptd == NULL) &&
  758. (pfmt->dwAspect & DVASPECT_CONTENT) &&
  759. (pfmt->lindex == -1) &&
  760. (pfmt->tymed & TYMED_HGLOBAL))
  761. {
  762. LPITEMIDLIST pidl;
  763. hr = m_pcfHere->GetCurFolder(&pidl);
  764. if (SUCCEEDED(hr))
  765. {
  766. HIDA hida = HIDA_Create(pidl, m_lSel.GetCount(), (LPCITEMIDLIST*)m_lSel.GetArray());
  767. if (hida)
  768. {
  769. pmedium->tymed = TYMED_HGLOBAL;
  770. pmedium->hGlobal = hida;
  771. hr = S_OK;
  772. }
  773. else
  774. {
  775. hr = E_OUTOFMEMORY;
  776. }
  777. ILFree(pidl);
  778. }
  779. }
  780. return hr;
  781. }
  782. return(E_NOTIMPL);
  783. }
  784. STDMETHODIMP CCabObj::GetDataHere(FORMATETC *pformatetc, STGMEDIUM *pmedium)
  785. {
  786. return(E_NOTIMPL);
  787. }
  788. STDMETHODIMP CCabObj::QueryGetData(FORMATETC *pfmt)
  789. {
  790. if (!InitFileGroupDesc())
  791. {
  792. return(E_UNEXPECTED);
  793. }
  794. if (pfmt->cfFormat == s_uFileGroupDesc)
  795. {
  796. if (pfmt->ptd==NULL && (pfmt->dwAspect&DVASPECT_CONTENT) && pfmt->lindex==-1
  797. && (pfmt->tymed&TYMED_HGLOBAL))
  798. {
  799. return(S_OK);
  800. }
  801. return(E_INVALIDARG);
  802. }
  803. if (!InitFileContents())
  804. {
  805. return(E_UNEXPECTED);
  806. }
  807. if (pfmt->cfFormat == s_uFileContents)
  808. {
  809. if (pfmt->ptd==NULL && (pfmt->dwAspect&DVASPECT_CONTENT)
  810. && (pfmt->tymed & (TYMED_ISTREAM | TYMED_HGLOBAL)))
  811. {
  812. return(S_OK);
  813. }
  814. return(E_INVALIDARG);
  815. }
  816. if (!InitPersistedDataObject())
  817. {
  818. return(E_UNEXPECTED);
  819. }
  820. if (pfmt->cfFormat == s_uPersistedDataObject)
  821. {
  822. if (pfmt->ptd == NULL && (pfmt->dwAspect & DVASPECT_CONTENT)
  823. && (pfmt->tymed & TYMED_HGLOBAL))
  824. {
  825. return(S_OK);
  826. }
  827. return(E_INVALIDARG);
  828. }
  829. if (!InitHIDA())
  830. {
  831. return(E_UNEXPECTED);
  832. }
  833. if (pfmt->cfFormat == s_uHIDA)
  834. {
  835. if (pfmt->ptd == NULL && (pfmt->dwAspect & DVASPECT_CONTENT)
  836. && (pfmt->tymed & TYMED_HGLOBAL))
  837. {
  838. return(S_OK);
  839. }
  840. return(E_INVALIDARG);
  841. }
  842. return(E_NOTIMPL);
  843. }
  844. STDMETHODIMP CCabObj::GetCanonicalFormatEtc(FORMATETC *pformatetcIn, FORMATETC *pformatetcOut)
  845. {
  846. return(E_NOTIMPL);
  847. }
  848. STDMETHODIMP CCabObj::SetData(FORMATETC *pformatetc, STGMEDIUM *pmedium, BOOL fRelease)
  849. {
  850. return(E_NOTIMPL);
  851. }
  852. STDMETHODIMP CCabObj::EnumFormatEtc(DWORD dwDirection, IEnumFORMATETC **ppenumFormatEtc)
  853. {
  854. if (!InitFileGroupDesc() || !InitFileContents() || !InitPersistedDataObject() || !InitHIDA())
  855. {
  856. return(E_UNEXPECTED);
  857. }
  858. FORMATETC fmte[] = {
  859. {(USHORT)s_uFileContents, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL|TYMED_ISTREAM },
  860. {(USHORT)s_uFileGroupDesc, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL },
  861. {(USHORT)s_uPersistedDataObject, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL },
  862. {(USHORT)s_uHIDA, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL },
  863. };
  864. CObjFormats *pFmts = new CObjFormats(ARRAYSIZE(fmte), fmte);
  865. if (!pFmts)
  866. {
  867. return(E_OUTOFMEMORY);
  868. }
  869. pFmts->AddRef();
  870. HRESULT hRes = pFmts->QueryInterface(IID_IEnumFORMATETC, (LPVOID *)ppenumFormatEtc);
  871. pFmts->Release();
  872. return(hRes);
  873. }
  874. STDMETHODIMP CCabObj::DAdvise(FORMATETC *pformatetc, DWORD advf, IAdviseSink *pAdvSink,
  875. DWORD *pdwConnection)
  876. {
  877. return(E_NOTIMPL);
  878. }
  879. STDMETHODIMP CCabObj::DUnadvise(DWORD dwConnection)
  880. {
  881. return(E_NOTIMPL);
  882. }
  883. STDMETHODIMP CCabObj::EnumDAdvise(IEnumSTATDATA **ppenumAdvise)
  884. {
  885. return(E_NOTIMPL);
  886. }
  887. BOOL CCabObj::InitFileGroupDesc()
  888. {
  889. if (s_uFileGroupDesc)
  890. {
  891. return(TRUE);
  892. }
  893. s_uFileGroupDesc = RegisterClipboardFormat(CFSTR_FILEDESCRIPTOR);
  894. return(s_uFileGroupDesc != 0);
  895. }
  896. BOOL CCabObj::InitFileContents()
  897. {
  898. if (s_uFileContents)
  899. {
  900. return(TRUE);
  901. }
  902. s_uFileContents = RegisterClipboardFormat(CFSTR_FILECONTENTS);
  903. return(s_uFileContents != 0);
  904. }
  905. #define CFSTR_OLECLIPBOARDPERSISTONFLUSH TEXT("OleClipboardPersistOnFlush")
  906. BOOL CCabObj::InitPersistedDataObject()
  907. {
  908. if (s_uPersistedDataObject)
  909. {
  910. return(TRUE);
  911. }
  912. s_uPersistedDataObject = RegisterClipboardFormat(CFSTR_OLECLIPBOARDPERSISTONFLUSH);
  913. return(s_uPersistedDataObject != 0);
  914. }
  915. BOOL CCabObj::InitHIDA()
  916. {
  917. if (s_uHIDA)
  918. {
  919. return(TRUE);
  920. }
  921. s_uHIDA = RegisterClipboardFormat(CFSTR_SHELLIDLIST);
  922. return(s_uHIDA != 0);
  923. }
  924. HGLOBAL * CALLBACK CCabObj::ShouldExtract(LPCTSTR pszFile, DWORD dwSize, UINT date,
  925. UINT time, UINT attribs, LPARAM lParam)
  926. {
  927. CCabObj *pThis = (CCabObj*)lParam;
  928. int iItem = pThis->m_lSel.FindInList(pszFile, dwSize, date, time, attribs);
  929. if (iItem < 0)
  930. {
  931. return(EXTRACT_FALSE);
  932. }
  933. // Copy nothing for now
  934. return(&(pThis->m_lContents[iItem]));
  935. }
  936. HRESULT CCabObj::InitContents()
  937. {
  938. if (m_lContents)
  939. {
  940. return S_OK;
  941. }
  942. int iCount = m_lSel.GetCount();
  943. if (iCount < 1)
  944. {
  945. return E_UNEXPECTED;
  946. }
  947. m_lContents = (HGLOBAL *)GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT,
  948. sizeof(HGLOBAL)*m_lSel.GetCount());
  949. if (!m_lContents)
  950. {
  951. return E_OUTOFMEMORY;
  952. }
  953. TCHAR szHere[MAX_PATH];
  954. if ((NULL == m_pcfHere) || (!m_pcfHere->GetPath(szHere)))
  955. {
  956. return E_UNEXPECTED;
  957. }
  958. CCabExtract ceHere(szHere);
  959. ceHere.ExtractItems(m_hwndOwner, DIR_MEM, ShouldExtract, (LPARAM)this);
  960. return S_OK;
  961. }