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.

445 lines
12 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: memstm.h
  7. //
  8. // Contents: class declarations and API's for memory streams
  9. //
  10. // Classes: MEMSTM (struct)
  11. // CMemStm
  12. // CMemBytes
  13. // CMarshalMemStm
  14. // CMarshalMemBytes
  15. //
  16. // Functions:
  17. //
  18. // History: dd-mmm-yy Author Comment
  19. // 31-Jan-95 t-ScottH added Dump methods to CMemStm and CMemBytes
  20. // (_DEBUG only)
  21. // 24-Jan-94 alexgo first pass converting to Cairo style
  22. // memory allocation
  23. // 09-Nov-93 alexgo 32bit port, added API declarations
  24. // 02-Dec-93 alexgo finished commenting and converting
  25. // to cairo standards
  26. //--------------------------------------------------------------------------
  27. #if !defined( _MEMSTM_H_ )
  28. #define _MEMSTM_H_
  29. #include <sem.hxx> // CMutexSem
  30. #include <olesem.hxx>
  31. #ifdef _DEBUG
  32. #include "dbgexts.h"
  33. #endif // _DEBUG
  34. /*
  35. * MemStm APIs
  36. */
  37. STDAPI_(LPSTREAM) CreateMemStm(DWORD cb, LPHANDLE phMem);
  38. STDAPI_(LPSTREAM) CloneMemStm(HANDLE hMem);
  39. STDAPI_(void) ReleaseMemStm(LPHANDLE phMem, BOOL fInternalOnly);
  40. STDAPI CreateStreamOnHGlobal(HANDLE hGlobal,
  41. BOOL fDeleteOnRelease, LPSTREAM FAR * ppstm);
  42. STDAPI GetHGlobalFromStream(LPSTREAM pstm,
  43. HGLOBAL FAR *phglobal);
  44. STDAPI_(IUnknown FAR*) CMemStmUnMarshal(void);
  45. STDAPI_(IUnknown FAR*) CMemBytesUnMarshal(void);
  46. class FAR CMarshalMemStm;
  47. class FAR CMarshalMemBytes;
  48. //+-------------------------------------------------------------------------
  49. //
  50. // Class: MEMSTM
  51. //
  52. // Purpose: A structure to describe the global memroy
  53. //
  54. // Interface:
  55. //
  56. // History: dd-mmm-yy Author Comment
  57. // 09-Nov-93 alexgo 32bit port
  58. //
  59. // Notes:
  60. //
  61. // cRef counts all CMemStm pointers to this MEMSTM plus the number of times
  62. // a hMem handle to MEMSTM had been returned
  63. //
  64. //--------------------------------------------------------------------------
  65. struct MEMSTM
  66. { // Data in shared memory
  67. DWORD cb; // Size of hGlobal
  68. DWORD cRef; // See below
  69. #ifdef NOTSHARED
  70. HANDLE hGlobal; // The data
  71. #else
  72. BYTE * m_pBuf;
  73. HANDLE hGlobal;
  74. #endif
  75. BOOL fDeleteOnRelease;
  76. };
  77. #define STREAM_SIG (0x4d525453L)
  78. //+-------------------------------------------------------------------------
  79. //
  80. // Class: CRefMutexSem
  81. //
  82. // Purpose: A class that provides a refcounted CMutexSem object
  83. //
  84. // Interface:
  85. //
  86. // History: dd-mmm-yy Author Comment
  87. // 20-Sep-2000 mfeingol Created
  88. //
  89. // Notes:
  90. //
  91. class CRefMutexSem
  92. {
  93. protected:
  94. LONG m_lRefs;
  95. CMutexSem2 m_mxs;
  96. CRefMutexSem();
  97. BOOL FInit();
  98. public:
  99. static CRefMutexSem* CreateInstance();
  100. ULONG STDMETHODCALLTYPE AddRef();
  101. ULONG STDMETHODCALLTYPE Release();
  102. void RequestCS();
  103. void ReleaseCS();
  104. const CMutexSem2* GetMutexSem();
  105. };
  106. // Autolock class for CRefMutexSem
  107. class CRefMutexAutoLock INHERIT_UNWIND_IF_CAIRO
  108. {
  109. EXPORTDEF DECLARE_UNWIND
  110. protected:
  111. CRefMutexSem* m_pmxs;
  112. public:
  113. CRefMutexAutoLock (CRefMutexSem* pmxs);
  114. ~CRefMutexAutoLock();
  115. };
  116. //+-------------------------------------------------------------------------
  117. //
  118. // Class: CMemStm
  119. //
  120. // Purpose: IStream on memory (shared mem for win16)
  121. //
  122. // Interface: IStream
  123. //
  124. // History: dd-mmm-yy Author Comment
  125. // 02-Dec-93 alexgo 32bit port
  126. //
  127. // Notes:
  128. //
  129. // CMemStm is a stream implementation on top of global shared memory MEMSTM
  130. //
  131. // CMemStm
  132. // +---------+
  133. // + pvtf + Shared memory
  134. // +---------+ +--------------+
  135. // + m_pMem +-->|cb |
  136. // +---------+ |cRef |
  137. // |hGlobal |--->+--------------+
  138. // +--------------+ | Actual Data |
  139. // CMemStm MEMSTM +--------------+
  140. //--------------------------------------------------------------------------
  141. class FAR CMemStm : public IStream, public CPrivAlloc
  142. {
  143. public:
  144. STDMETHOD(QueryInterface) (REFIID iidInterface, void **ppvObj);
  145. STDMETHOD_(ULONG,AddRef) (void);
  146. STDMETHOD_(ULONG,Release) (void);
  147. STDMETHOD(Read) (VOID HUGEP* pv, ULONG cb, ULONG FAR* pcbRead);
  148. STDMETHOD(Write) (VOID const HUGEP* pv, ULONG cb, ULONG *pcbWritten);
  149. STDMETHOD(Seek) (LARGE_INTEGER dlibMove, DWORD dwOrigin,
  150. ULARGE_INTEGER *plibNewPosition);
  151. STDMETHOD(SetSize) (ULARGE_INTEGER cb);
  152. STDMETHOD(CopyTo) (IStream *pstm, ULARGE_INTEGER cb,
  153. ULARGE_INTEGER *pcbRead, ULARGE_INTEGER *pcbWritten);
  154. STDMETHOD(Commit) (DWORD grfCommitFlags);
  155. STDMETHOD(Revert) (void);
  156. STDMETHOD(LockRegion) (ULARGE_INTEGER libOffset, ULARGE_INTEGER cb,
  157. DWORD dwLockType);
  158. STDMETHOD(UnlockRegion) (ULARGE_INTEGER libOffset, ULARGE_INTEGER cb,
  159. DWORD dwLockType);
  160. STDMETHOD(Stat) (STATSTG *pstatstg, DWORD statflag);
  161. STDMETHOD(Clone)(IStream **ppstm);
  162. STDSTATIC_(CMemStm FAR*) Create(HANDLE hMem, CRefMutexSem* pmxs);
  163. #ifdef _DEBUG
  164. HRESULT Dump(char **ppszDump, ULONG ulFlag, int nIndentLevel);
  165. friend DEBUG_EXTENSION_API(dump_cmemstm);
  166. #endif // _DEBUG
  167. ~CMemStm();
  168. private:
  169. CMemStm();
  170. DWORD m_dwSig; // Signature indicating this is our
  171. // implementation of
  172. // IStream: STREAM_SIG
  173. ULONG m_refs; // Number of references to this CmemStm
  174. ULONG m_pos; // Seek pointer for Read/Write
  175. HANDLE m_hMem; // Memory Handle passed on creation
  176. MEMSTM FAR* m_pData; // Pointer to that memroy
  177. CRefMutexSem* m_pmxs; // mutex for MultiThread protection
  178. friend HRESULT STDAPICALLTYPE GetHGlobalFromStream(LPSTREAM, HGLOBAL *);
  179. friend LPSTREAM STDAPICALLTYPE CreateMemStm(DWORD, LPHANDLE);
  180. friend class CMarshalMemStm;
  181. };
  182. #define LOCKBYTE_SIG (0x0046574A)
  183. //+-------------------------------------------------------------------------
  184. //
  185. // Class: CMemBytes
  186. //
  187. // Purpose: an ILockBytes implementation atop (global shared in win16)
  188. // memory MEMSTM
  189. //
  190. // Interface: ILockBytes
  191. //
  192. // History: dd-mmm-yy Author Comment
  193. // 02-Dec-93 alexgo 32bit port
  194. //
  195. // Notes:
  196. //
  197. // CMemBytes
  198. // +---------+
  199. // + pvtf + Shared memory
  200. // +---------+ +--------------+
  201. // + m_pData +-->| cb |
  202. // +---------+ | cRef |
  203. // | hGlobal |--->+-------------+
  204. // +--------------+ | Actual data |
  205. // CMemBytes MEMSTM +-------------+
  206. //
  207. // cRef counts all CMemBytes pointers to this MEMSTM.
  208. // It and fDeleteOnRelease control the GlobalFree'ing of the hGlobal.
  209. //
  210. //--------------------------------------------------------------------------
  211. class FAR CMemBytes : public ILockBytes, public CPrivAlloc
  212. {
  213. public:
  214. STDMETHOD(QueryInterface) (REFIID iidInterface,
  215. void FAR* FAR* ppvObj);
  216. STDMETHOD_(ULONG,AddRef) (void);
  217. STDMETHOD_(ULONG,Release) (void);
  218. STDMETHOD(ReadAt) (ULARGE_INTEGER ulOffset, VOID HUGEP *pv, ULONG cb,
  219. ULONG FAR *pcbRead);
  220. STDMETHOD(WriteAt) (ULARGE_INTEGER ulOffset, VOID const HUGEP *pv,
  221. ULONG cb, ULONG FAR *pcbWritten);
  222. STDMETHOD(Flush) (void);
  223. STDMETHOD(SetSize) (ULARGE_INTEGER cb);
  224. STDMETHOD(LockRegion) (ULARGE_INTEGER libOffset, ULARGE_INTEGER cb,
  225. DWORD dwLockType);
  226. STDMETHOD(UnlockRegion) (ULARGE_INTEGER libOffset, ULARGE_INTEGER cb,
  227. DWORD dwLockType);
  228. STDMETHOD(Stat) (THIS_ STATSTG FAR *pstatstg, DWORD statflag);
  229. STDSTATIC_(CMemBytes FAR*) Create(HANDLE hMem);
  230. #ifdef _DEBUG
  231. HRESULT Dump(char **ppszDump, ULONG ulFlag, int nIndentLevel);
  232. friend DEBUG_EXTENSION_API(dump_membytes);
  233. #endif // _DEBUG
  234. ctor_dtor:
  235. CMemBytes()
  236. {
  237. GET_A5();
  238. m_hMem = NULL;
  239. m_pData = NULL;
  240. m_refs = 0;
  241. }
  242. ~CMemBytes()
  243. {
  244. // empty body
  245. }
  246. private:
  247. DWORD m_dwSig; // Signature indicating this
  248. // is our implementation of
  249. // ILockBytes: LOCKBYTE_SIG
  250. ULONG m_refs; // Normal reference count
  251. HANDLE m_hMem; // Handle for bookeeping info
  252. // (MEMSTM)
  253. MEMSTM FAR* m_pData; // Pointer to that memory
  254. friend HRESULT STDAPICALLTYPE GetHGlobalFromILockBytes(LPLOCKBYTES,
  255. HGLOBAL FAR*);
  256. friend class CMarshalMemBytes;
  257. SET_A5;
  258. };
  259. #ifndef WIN32
  260. //
  261. // THE MARSHALLING CLASSES BELOW ARE ONLY IN 16BIT OLE!!!!
  262. //
  263. //+-------------------------------------------------------------------------
  264. //
  265. // Class: CMarshalMemStm
  266. //
  267. // Purpose: provides marshalling for CMemStm's
  268. //
  269. // Interface: IMarshal
  270. //
  271. // History: dd-mmm-yy Author Comment
  272. // 02-Dec-93 alexgo 32bit port
  273. // 05-Dec-93 alexgo removed m_clsid
  274. //
  275. // Notes:
  276. //
  277. // CMarshalMemStm can Marshal, Unmarshal CMemStm. It is impletented as
  278. // a seperate object accessible from CMemStm, CMemBytes: QueryIntreface of
  279. // IMarshal on CMemStm's IStream will return an IMarshal pointer to
  280. // CMarshalMemStm, but QueryInterface of IStream on that IMarshal will
  281. // fail.
  282. // Also QueryInterface of IUnknown on IMarshal will not return the same value
  283. // As QueryInterface of IUnkown on the original IStream.
  284. //
  285. //--------------------------------------------------------------------------
  286. class FAR CMarshalMemStm : public IMarshal
  287. {
  288. public:
  289. STDMETHOD(QueryInterface) (REFIID riid, LPVOID FAR* ppvObj);
  290. STDMETHOD_(ULONG,AddRef) (void);
  291. STDMETHOD_(ULONG,Release) (void);
  292. STDMETHOD(GetUnmarshalClass)(THIS_ REFIID riid, LPVOID pv,
  293. DWORD dwDestContext, LPVOID pvDestContext,
  294. DWORD mshlflags, LPCLSID pCid);
  295. STDMETHOD(GetMarshalSizeMax)(THIS_ REFIID riid, LPVOID pv,
  296. DWORD dwDestContext, LPVOID pvDestContext,
  297. DWORD mshlflags, LPDWORD pSize);
  298. STDMETHOD(MarshalInterface)(THIS_ IStream FAR* pStm, REFIID riid,
  299. LPVOID pv, DWORD dwDestContext, LPVOID pvDestContext,
  300. DWORD mshlflags);
  301. STDMETHOD(UnmarshalInterface)(THIS_ IStream FAR* pStm, REFIID riid,
  302. LPVOID FAR* ppv);
  303. STDMETHOD(ReleaseMarshalData)(THIS_ IStream FAR* pStm);
  304. STDMETHOD(DisconnectObject)(THIS_ DWORD dwReserved);
  305. STDSTATIC_(CMarshalMemStm FAR*) Create(CMemStm FAR* pMemStm);
  306. ctor_dtor:
  307. CMarshalMemStm()
  308. {
  309. GET_A5();
  310. m_pMemStm = NULL;
  311. m_refs = 0;
  312. }
  313. ~CMarshalMemStm()
  314. {
  315. // empty body
  316. }
  317. private:
  318. ULONG m_refs; // Number of references to this CmemStm
  319. CMemStm FAR* m_pMemStm; // Pointer to object [Un]Marshalled
  320. SET_A5;
  321. };
  322. //+-------------------------------------------------------------------------
  323. //
  324. // Class: CMarshalMemBytes
  325. //
  326. // Purpose: provides marshalling for CMemBytes
  327. //
  328. // Interface: IMarshal
  329. //
  330. // History: dd-mmm-yy Author Comment
  331. // 02-Dec-93 alexgo 32bit port
  332. // 05-Dec-93 alexgo removed m_clsid
  333. //
  334. // Notes:
  335. //
  336. // CMarshalMemBytes can Marshal, Unmarshal CMemBytes. It is impletented as
  337. // a seperate object accessible from CMemBytes, CMemBytes: QueryIntreface of
  338. // IMarshal on CMemBytes's ILocBytes will return an IMarshal pointer to
  339. // CMarshalMemBytes, but QueryInterface of ILockBytes on that IMarshal will
  340. // fail.
  341. // Also QueryInterface of IUnknown on IMarshal will not return the same value
  342. // As QueryInterface of IUnkown on the original ILockBytes.
  343. //
  344. //--------------------------------------------------------------------------
  345. class FAR CMarshalMemBytes : public IMarshal
  346. {
  347. public:
  348. STDMETHOD(QueryInterface) (REFIID riid, LPVOID FAR* ppvObj);
  349. STDMETHOD_(ULONG,AddRef) (void);
  350. STDMETHOD_(ULONG,Release) (void);
  351. STDMETHOD(GetUnmarshalClass)(THIS_ REFIID riid, LPVOID pv,
  352. DWORD dwDestContext, LPVOID pvDestContext,
  353. DWORD mshlflags, LPCLSID pCid);
  354. STDMETHOD(GetMarshalSizeMax)(THIS_ REFIID riid, LPVOID pv,
  355. DWORD dwDestContext, LPVOID pvDestContext,
  356. DWORD mshlflags, LPDWORD pSize);
  357. STDMETHOD(MarshalInterface)(THIS_ IStream FAR* pStm, REFIID riid,
  358. LPVOID pv, DWORD dwDestContext, LPVOID pvDestContext,
  359. DWORD mshlflags);
  360. STDMETHOD(UnmarshalInterface)(THIS_ IStream FAR* pStm, REFIID riid,
  361. LPVOID FAR* ppv);
  362. STDMETHOD(ReleaseMarshalData)(THIS_ IStream FAR* pStm);
  363. STDMETHOD(DisconnectObject)(THIS_ DWORD dwReserved);
  364. STDSTATIC_(CMarshalMemBytes FAR*) Create(CMemBytes FAR* pMemBytes);
  365. ctor_dtor:
  366. CMarshalMemBytes()
  367. {
  368. GET_A5();
  369. m_pMemBytes = NULL;
  370. m_refs = 0;
  371. }
  372. ~CMarshalMemBytes()
  373. {
  374. // empty body
  375. }
  376. private:
  377. ULONG m_refs; // Number of references to
  378. // this CMemBytes
  379. CMemBytes FAR* m_pMemBytes; // Pointer to object
  380. // [Un]Marshalled
  381. SET_A5;
  382. };
  383. #endif // !WIN32
  384. #endif // _MemBytes_H
  385.