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.

263 lines
5.8 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: lockbyte.cpp
  7. //
  8. // Contents: Apis for working with the standard ILockByte implementation
  9. // on memory
  10. //
  11. // Classes:
  12. //
  13. // Functions: CreateILockBytesOnHGlobal
  14. // GetHGlobalFromILockBytes
  15. //
  16. // History: dd-mmm-yy Author Comment
  17. // 11-Jan-93 alexgo added VDATEHEAP macros to every function
  18. // fixed compile warnings
  19. // 16-Dec-93 alexgo fixed bad memory bugs
  20. // 02-Dec-93 alexgo 32bit port
  21. // 15-Sep-92 jasonful author
  22. //
  23. //--------------------------------------------------------------------------
  24. #include <le2int.h>
  25. #pragma SEG(lockbyte)
  26. #include "memstm.h"
  27. #include <reterr.h>
  28. NAME_SEG(LockBytes)
  29. ASSERTDATA
  30. //+-------------------------------------------------------------------------
  31. //
  32. // Function: CreateILockBytesOnHGlobal
  33. //
  34. // Synopsis: Creates a CMemBytes on the given HGlobal
  35. //
  36. // Effects:
  37. //
  38. // Arguments: [hGlobal] -- the memory to use (may be NULL)
  39. // [fDeleteOnRelease] -- if TRUE, then [hGlobal will
  40. // be freed when CMemBytes is
  41. // freed via a Release
  42. // [pplkbyt] -- where to put the pointer to the CMemByte
  43. // instance
  44. // Requires:
  45. //
  46. // Returns: HRESULT
  47. //
  48. // Signals:
  49. //
  50. // Modifies:
  51. //
  52. // Algorithm:
  53. //
  54. // History: dd-mmm-yy Author Comment
  55. // 11-Jan-94 alexgo removed initialization of cbSize to -1
  56. // to fix a compile warning
  57. // 16-Dec-93 alexgo fixed bogus usage of MAKELONG (turned
  58. // into a GlobalLock)
  59. // 02-Dec-93 alexgo 32bit port, fixed memory leak bug
  60. //
  61. // Notes: REVIEW32: It's fine to *ask* for shared memory on NT, you
  62. // just won't get it. We need to make sure that any callers
  63. // (looks like apps at the moment) don't have the wrong idea :)
  64. //
  65. //--------------------------------------------------------------------------
  66. #pragma SEG(CreateILockBytesOnHGlobal)
  67. STDAPI CreateILockBytesOnHGlobal
  68. (HGLOBAL hGlobal,
  69. BOOL fDeleteOnRelease,
  70. LPLOCKBYTES FAR* pplkbyt)
  71. {
  72. OLETRACEIN((API_CreateILockBytesOnHGlobal,
  73. PARAMFMT("hGlobal= %h, fDeleteOnRelease= %B, pplkbyt= %p"),
  74. hGlobal, fDeleteOnRelease, pplkbyt));
  75. VDATEHEAP();
  76. HANDLE hMem = NULL;
  77. struct MEMSTM FAR* pData = NULL;
  78. ILockBytes FAR* pBytes = NULL;
  79. DWORD cbSize;
  80. BOOL fAllochGlobal = FALSE;
  81. HRESULT hresult;
  82. VDATEPTROUT_LABEL (pplkbyt, LPLOCKBYTES, SafeExit, hresult);
  83. *pplkbyt = NULL;
  84. if (NULL==hGlobal)
  85. {
  86. hGlobal = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE, 0);
  87. if (hGlobal == NULL)
  88. {
  89. goto ErrorExit;
  90. }
  91. fAllochGlobal = TRUE;
  92. cbSize = 0;
  93. }
  94. else
  95. {
  96. cbSize = (ULONG) GlobalSize (hGlobal);
  97. // Is there a way to verify a zero-sized handle?
  98. if (cbSize!=0)
  99. {
  100. // verify validity of passed-in handle
  101. if (NULL==GlobalLock(hGlobal))
  102. {
  103. // bad handle
  104. hresult = ResultFromScode (E_INVALIDARG);
  105. goto SafeExit;
  106. }
  107. GlobalUnlock (hGlobal);
  108. }
  109. }
  110. hMem = GlobalAlloc (GMEM_DDESHARE | GMEM_MOVEABLE, sizeof (MEMSTM));
  111. if (hMem == NULL)
  112. {
  113. if (fAllochGlobal && hGlobal )
  114. {
  115. GlobalFree(hGlobal);
  116. }
  117. goto ErrorExit;
  118. }
  119. pData = (MEMSTM FAR *)GlobalLock(hMem);
  120. if (pData == NULL)
  121. {
  122. goto FreeMem;
  123. }
  124. pData->cRef = 0;
  125. pData->cb = cbSize;
  126. pData->fDeleteOnRelease = fDeleteOnRelease;
  127. pData->hGlobal = hGlobal;
  128. pBytes = CMemBytes::Create(hMem); // Create the ILockBytes
  129. if (pBytes == NULL)
  130. {
  131. goto FreeMem;
  132. }
  133. *pplkbyt = pBytes;
  134. GlobalUnlock(hMem);
  135. CALLHOOKOBJECTCREATE(S_OK,CLSID_NULL,IID_ILockBytes,
  136. (IUnknown **)pplkbyt);
  137. hresult = NOERROR;
  138. goto SafeExit;
  139. FreeMem:
  140. if (pData)
  141. {
  142. GlobalUnlock(hMem);
  143. }
  144. if (hMem)
  145. {
  146. GlobalFree(hMem);
  147. }
  148. if (fAllochGlobal && hGlobal )
  149. {
  150. GlobalFree(hGlobal);
  151. }
  152. ErrorExit:
  153. Assert (0);
  154. hresult = ResultFromScode(E_OUTOFMEMORY);
  155. SafeExit:
  156. OLETRACEOUT((API_CreateILockBytesOnHGlobal, hresult));
  157. return hresult;
  158. }
  159. //+-------------------------------------------------------------------------
  160. //
  161. // Function: GetHGlobalFromILockBytes
  162. //
  163. // Synopsis: Retrieves the hGlobal the ILockBytes was created with
  164. //
  165. // Effects:
  166. //
  167. // Arguments: [plkbyt] -- pointer to the ILockBytes implementation
  168. // [phglobal] -- where to put the hglobal
  169. //
  170. // Requires:
  171. //
  172. // Returns: HRESULT
  173. //
  174. // Signals:
  175. //
  176. // Modifies:
  177. //
  178. // Algorithm: hacked--does a pointer cast and checks the signature :( :(
  179. //
  180. // History: dd-mmm-yy Author Comment
  181. // 02-Dec-93 alexgo 32bit port
  182. //
  183. // Notes:
  184. //
  185. //--------------------------------------------------------------------------
  186. #pragma SEG(GetHGlobalFromILockBytes)
  187. STDAPI GetHGlobalFromILockBytes
  188. (LPLOCKBYTES plkbyt,
  189. HGLOBAL FAR* phglobal)
  190. {
  191. OLETRACEIN((API_GetHGlobalFromILockBytes,
  192. PARAMFMT("plkbyt= %p, phglobal= %p"),
  193. plkbyt, phglobal));
  194. VDATEHEAP();
  195. HRESULT hresult;
  196. CMemBytes FAR* pCMemByte;
  197. MEMSTM FAR* pMem;
  198. VDATEIFACE_LABEL(plkbyt, errRtn, hresult);
  199. VDATEPTROUT_LABEL (phglobal, HANDLE, errRtn, hresult);
  200. CALLHOOKOBJECT(S_OK,CLSID_NULL,IID_ILockBytes,(IUnknown **)&plkbyt);
  201. *phglobal = NULL;
  202. pCMemByte = (CMemBytes FAR*)plkbyt;
  203. if (!IsValidReadPtrIn (&(pCMemByte->m_dwSig), sizeof(ULONG))
  204. || pCMemByte->m_dwSig != LOCKBYTE_SIG)
  205. {
  206. // we were passed someone else's implementation of ILockBytes
  207. hresult = ResultFromScode (E_INVALIDARG);
  208. goto errRtn;
  209. }
  210. pMem= pCMemByte->m_pData;
  211. if (NULL==pMem)
  212. {
  213. Assert (0);
  214. hresult = ResultFromScode (E_OUTOFMEMORY);
  215. goto errRtn;
  216. }
  217. Assert (pMem->cb <= GlobalSize (pMem->hGlobal));
  218. Verify (*phglobal = pMem->hGlobal);
  219. hresult = NOERROR;
  220. errRtn:
  221. OLETRACEOUT((API_GetHGlobalFromILockBytes, hresult));
  222. return hresult;
  223. }