uLib  User mode C/C++ extended API library for Win32 programmers.
MemStrm.cpp
Go to the documentation of this file.
1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 // Project: uLib - User mode library.
3 // Module: Simplified global memory streams (when you need an IStream)
4 // Author: Copyright (c) Love Nystrom
5 // License: NNOSL (BSD descendant, see NNOSL.txt in the base directory).
6 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 
8 #include <uLib\MemStrm.h>
9 
10 int _omsCount = 0; // OleInitialize sentry
11 
12 // OpenMemStream -- Return an IStream in global memory
13 
14 LPSTREAM OpenMemStream( size_t Size )
15 {
16  LPSTREAM pStream;
17  HGLOBAL hMem;
18  HRESULT rc = (0 == _omsCount++) ? OleInitialize( NULL ) : S_OK;
19  if (rc == S_OK || rc == S_FALSE)
20  {
21  hMem = GlobalAlloc( GHND| GMEM_NODISCARD| GMEM_SHARE, Size );
22  if (hMem)
23  {
24  if (CreateStreamOnHGlobal( hMem, TRUE, &pStream ) == S_OK)
25  return pStream;
26  else GlobalFree( hMem );
27  }
28  }
29  return NULL;
30 }
31 
32 // GetMemStreamHandle -- Return the current memory handle for the MemStream
33 
34 HGLOBAL GetMemStreamHandle( LPSTREAM pStream )
35 {
36  HGLOBAL handle = NULL;
37  GetHGlobalFromStream( pStream, &handle );
38  return handle;
39 }
40 
41 // CloseMemStream -- Close the global memory IStream
42 
43 void CloseMemStream( LPSTREAM pStream )
44 {
45  pStream->Release();
46  if (0 == --_omsCount) OleUninitialize();
47 }
48 
49 // EOF
int _omsCount
Definition: MemStrm.cpp:10
HGLOBAL GetMemStreamHandle(LPSTREAM pStream)
Definition: MemStrm.cpp:34
void CloseMemStream(LPSTREAM pStream)
Definition: MemStrm.cpp:43
LPSTREAM OpenMemStream(size_t Size)
Definition: MemStrm.cpp:14