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.

158 lines
4.8 KiB

  1. //+---------------------------------------------------------------------
  2. //
  3. // File: stgutils.hxx
  4. //
  5. // Contents: IStorage and IStream Helper functions
  6. //
  7. //----------------------------------------------------------------------
  8. #include "headers.hxx"
  9. #pragma hdrstop
  10. //+---------------------------------------------------------------
  11. //
  12. // Function: GetMonikerDisplayName
  13. //
  14. // Synopsis: Retrieves the display name of a moniker
  15. //
  16. // Arguments: [pmk] -- the moniker for which the display name is requested
  17. // [ppstr] -- the place where the display name is returned
  18. //
  19. // Returns: Success iff the display name could be retrieved
  20. //
  21. // Notes: The display name string is allocated using the task allocator
  22. // and should be freed by the same.
  23. //
  24. //----------------------------------------------------------------
  25. HRESULT
  26. GetMonikerDisplayName(LPMONIKER pmk, LPOLESTR FAR* ppstr)
  27. {
  28. HRESULT r;
  29. LPBC pbc;
  30. if (OK(r = CreateBindCtx(0, &pbc)))
  31. {
  32. r = pmk->GetDisplayName(pbc, NULL, ppstr);
  33. pbc->Release();
  34. }
  35. return r;
  36. }
  37. //+---------------------------------------------------------------
  38. //
  39. // Function: CreateStorageOnHGlobal
  40. //
  41. // Synopsis: Creates an IStorage on a global memory handle
  42. //
  43. // Arguments: [hgbl] -- memory handle to create storage on
  44. // [ppStg] -- where the storage is returned
  45. //
  46. // Returns: Success iff the storage could be successfully created
  47. // on the memory handle.
  48. //
  49. // Notes: This helper function combines CreateILockBytesOnHGlobal
  50. // and StgCreateDocfileOnILockBytes. hgbl may be NULL in
  51. // which case a global memory handle will be automatically
  52. // allocated.
  53. //
  54. //----------------------------------------------------------------
  55. HRESULT
  56. CreateStorageOnHGlobal(HGLOBAL hgbl, LPSTORAGE FAR* ppStg)
  57. {
  58. HRESULT r;
  59. LPLOCKBYTES pLockBytes;
  60. if (OK(r = CreateILockBytesOnHGlobal(hgbl, TRUE, &pLockBytes)))
  61. {
  62. //REVIEW: should be use STGM_DELETEONRELEASE when hgbl == NULL?
  63. r = StgCreateDocfileOnILockBytes(pLockBytes,
  64. STGM_CREATE|STGM_READWRITE|STGM_SHARE_EXCLUSIVE, 0, ppStg);
  65. pLockBytes->Release();
  66. }
  67. return r;
  68. }
  69. //+---------------------------------------------------------------
  70. //
  71. // Function: ConvertToMemoryStream
  72. //
  73. // Synopsis: Takes a stream and produces an equivalent stream
  74. // stored in memory for faster individual reads
  75. //
  76. // Arguments: [pStrmFrom] -- the stream to convert
  77. //
  78. // Returns: An equivalent stream
  79. //
  80. // Notes: Any failure in creating the memory stream will result in
  81. // the original stream, pStrmFrom, being returned.
  82. // Hence the caller should assume the stream passed in has
  83. // been released and should release the stream returned
  84. // after it has finished using it.
  85. //
  86. //----------------------------------------------------------------
  87. LPSTREAM
  88. ConvertToMemoryStream(LPSTREAM pStrmFrom)
  89. {
  90. //REVIEW: perhaps we should use the IStream::CopyTo function
  91. // instead of doing the read manually!
  92. HRESULT r;
  93. LPSTREAM pStrm = NULL;
  94. STATSTG statstg;
  95. if (OK(r = pStrmFrom->Stat(&statstg, STATFLAG_NONAME)))
  96. {
  97. if (statstg.cbSize.HighPart != 0)
  98. {
  99. DOUT(TEXT("o2base/stdils/ConvertToMemoryStream E_FAIL\r\n"));
  100. r = E_FAIL;
  101. }
  102. else
  103. {
  104. HGLOBAL hgbl = GlobalAlloc(GMEM_SHARE, statstg.cbSize.LowPart);
  105. if (hgbl == NULL)
  106. {
  107. DOUT(TEXT("o2base/stgutils/ConvertToMemoryStream failed\r\n"));
  108. r = E_OUTOFMEMORY;
  109. }
  110. else
  111. {
  112. LPVOID pv = GlobalLock(hgbl);
  113. if (pv == NULL)
  114. {
  115. DOUT(TEXT("o2base/stdils/ConvertToMemoryStream E_FAIL(2)\r\n"));
  116. r = E_FAIL;
  117. }
  118. else
  119. {
  120. r = pStrmFrom->Read(pv, statstg.cbSize.LowPart, NULL);
  121. GlobalUnlock(hgbl);
  122. if (OK(r))
  123. {
  124. if (OK(r = CreateStreamOnHGlobal(hgbl, TRUE, &pStrm)))
  125. {
  126. pStrm->SetSize(statstg.cbSize);
  127. pStrmFrom->Release();
  128. }
  129. else
  130. {
  131. DOUT(TEXT("o2base/stdils/ConvertToMemoryStream CreateStreamOnHGlobal Failed!\r\n"));
  132. }
  133. }
  134. }
  135. if (!OK(r))
  136. {
  137. GlobalFree(hgbl);
  138. }
  139. }
  140. }
  141. }
  142. if (!OK(r))
  143. {
  144. pStrm = pStrmFrom;
  145. }
  146. return pStrm;
  147. }
  148.