ReactOS  0.4.13-dev-259-g5ca9c9c
memlockbytes.c File Reference
#include "config.h"
#include <assert.h>
#include <stdarg.h>
#include <string.h>
#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "objbase.h"
#include "ole2.h"
#include "winerror.h"
#include "wine/debug.h"
Include dependency graph for memlockbytes.c:

Go to the source code of this file.

Classes

struct  HGLOBALLockBytesImpl
 

Macros

#define COBJMACROS
 
#define NONAMELESSUNION
 

Typedefs

typedef struct HGLOBALLockBytesImpl HGLOBALLockBytesImpl
 

Functions

 WINE_DEFAULT_DEBUG_CHANNEL (ole)
 
static HGLOBALLockBytesImplimpl_from_ILockBytes (ILockBytes *iface)
 
HRESULT WINAPI CreateILockBytesOnHGlobal (HGLOBAL global, BOOL delete_on_release, ILockBytes **ret)
 
HRESULT WINAPI GetHGlobalFromILockBytes (ILockBytes *iface, HGLOBAL *phglobal)
 
static HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface (ILockBytes *iface, REFIID riid, void **ppvObject)
 
static ULONG WINAPI HGLOBALLockBytesImpl_AddRef (ILockBytes *iface)
 
static ULONG WINAPI HGLOBALLockBytesImpl_Release (ILockBytes *iface)
 
static HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt (ILockBytes *iface, ULARGE_INTEGER ulOffset, void *pv, ULONG cb, ULONG *pcbRead)
 
static HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt (ILockBytes *iface, ULARGE_INTEGER ulOffset, const void *pv, ULONG cb, ULONG *pcbWritten)
 
static HRESULT WINAPI HGLOBALLockBytesImpl_Flush (ILockBytes *iface)
 
static HRESULT WINAPI HGLOBALLockBytesImpl_SetSize (ILockBytes *iface, ULARGE_INTEGER libNewSize)
 
static HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion (ILockBytes *iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
 
static HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion (ILockBytes *iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
 
static HRESULT WINAPI HGLOBALLockBytesImpl_Stat (ILockBytes *iface, STATSTG *pstatstg, DWORD grfStatFlag)
 

Variables

static const ILockBytesVtbl HGLOBALLockBytesImpl_Vtbl
 

Macro Definition Documentation

◆ COBJMACROS

#define COBJMACROS

Definition at line 28 of file memlockbytes.c.

◆ NONAMELESSUNION

#define NONAMELESSUNION

Definition at line 29 of file memlockbytes.c.

Typedef Documentation

◆ HGLOBALLockBytesImpl

Definition at line 70 of file memlockbytes.c.

Function Documentation

◆ CreateILockBytesOnHGlobal()

HRESULT WINAPI CreateILockBytesOnHGlobal ( HGLOBAL  global,
BOOL  delete_on_release,
ILockBytes **  ret 
)

Definition at line 100 of file memlockbytes.c.

101 {
102  HGLOBALLockBytesImpl* lockbytes;
103 
104  lockbytes = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALLockBytesImpl));
105  if (!lockbytes) return E_OUTOFMEMORY;
106 
107  lockbytes->ILockBytes_iface.lpVtbl = &HGLOBALLockBytesImpl_Vtbl;
108  lockbytes->ref = 1;
109 
110  /*
111  * Initialize the support.
112  */
113  lockbytes->supportHandle = global;
114  lockbytes->deleteOnRelease = delete_on_release;
115 
116  /*
117  * This method will allocate a handle if one is not supplied.
118  */
119  if (lockbytes->supportHandle == 0)
121 
122  /*
123  * Initialize the size of the array to the size of the handle.
124  */
125  lockbytes->byteArraySize.u.HighPart = 0;
126  lockbytes->byteArraySize.u.LowPart = GlobalSize(lockbytes->supportHandle);
127 
128  *ret = &lockbytes->ILockBytes_iface;
129 
130  return S_OK;
131 }
HGLOBAL NTAPI GlobalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:368
#define GMEM_NODISCARD
Definition: winbase.h:299
SIZE_T NTAPI GlobalSize(HGLOBAL hMem)
Definition: heapmem.c:1090
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
struct _ULARGE_INTEGER::@3737 u
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
int ret
static const ILockBytesVtbl HGLOBALLockBytesImpl_Vtbl
Definition: memlockbytes.c:77
ULARGE_INTEGER byteArraySize
Definition: memlockbytes.c:67
#define S_OK
Definition: intsafe.h:59
ILockBytes ILockBytes_iface
Definition: memlockbytes.c:50
#define GMEM_MOVEABLE
Definition: winbase.h:291

Referenced by create_storage(), DataObjectImpl_CreateComplex(), get_data_from_storage(), get_stgmed_for_storage(), render_embed_source_hack(), stream_to_storage(), test_data_cache_save(), test_data_cache_save_data(), test_hglobal_storage_creation(), test_hglobal_storage_stat(), and test_OleCreateStaticFromData().

◆ GetHGlobalFromILockBytes()

HRESULT WINAPI GetHGlobalFromILockBytes ( ILockBytes iface,
HGLOBAL phglobal 
)

Definition at line 147 of file memlockbytes.c.

148 {
150  STATSTG stbuf;
151  HRESULT hres;
153  ULONG xread;
154 
155  *phglobal = 0;
156  if (This->ILockBytes_iface.lpVtbl == &HGLOBALLockBytesImpl_Vtbl) {
157  *phglobal = This->supportHandle;
158  if (*phglobal == 0)
159  return E_INVALIDARG;
160  return S_OK;
161  }
162  /* It is not our lockbytes implementation, so use a more generic way */
163  hres = ILockBytes_Stat(iface,&stbuf,STATFLAG_NONAME);
164  if (hres != S_OK) {
165  ERR("Cannot ILockBytes_Stat, %x\n",hres);
166  return hres;
167  }
168  TRACE("cbSize is %s\n", wine_dbgstr_longlong(stbuf.cbSize.QuadPart));
169  *phglobal = GlobalAlloc( GMEM_MOVEABLE|GMEM_SHARE, stbuf.cbSize.u.LowPart);
170  if (!*phglobal)
171  return E_INVALIDARG;
172  memset(&start,0,sizeof(start));
173  hres = ILockBytes_ReadAt(iface, start, GlobalLock(*phglobal), stbuf.cbSize.u.LowPart, &xread);
174  GlobalUnlock(*phglobal);
175  if (hres != S_OK) {
176  FIXME("%p->ReadAt failed with %x\n",iface,hres);
177  return hres;
178  }
179  if (stbuf.cbSize.u.LowPart != xread) {
180  FIXME("Read size is not requested size %d vs %d?\n",stbuf.cbSize.u.LowPart, xread);
181  }
182  return S_OK;
183 }
HGLOBAL NTAPI GlobalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:368
static HGLOBALLockBytesImpl * impl_from_ILockBytes(ILockBytes *iface)
Definition: memlockbytes.c:72
#define GMEM_SHARE
Definition: winbase.h:302
#define FIXME(fmt,...)
Definition: debug.h:110
#define E_INVALIDARG
Definition: ddrawi.h:101
#define TRACE(s)
Definition: solgame.cpp:4
HRESULT hres
Definition: protocol.c:465
LONG HRESULT
Definition: typedefs.h:77
LPVOID NTAPI GlobalLock(HGLOBAL hMem)
Definition: heapmem.c:755
static const ILockBytesVtbl HGLOBALLockBytesImpl_Vtbl
Definition: memlockbytes.c:77
#define ERR(fmt,...)
Definition: debug.h:109
#define S_OK
Definition: intsafe.h:59
GLuint start
Definition: gl.h:1545
BOOL NTAPI GlobalUnlock(HGLOBAL hMem)
Definition: heapmem.c:1190
unsigned int ULONG
Definition: retypes.h:1
static __inline const char * wine_dbgstr_longlong(ULONGLONG ll)
Definition: compat.h:41
#define memset(x, y, z)
Definition: compat.h:39
#define GMEM_MOVEABLE
Definition: winbase.h:291

Referenced by create_storage().

◆ HGLOBALLockBytesImpl_AddRef()

static ULONG WINAPI HGLOBALLockBytesImpl_AddRef ( ILockBytes iface)
static

Definition at line 224 of file memlockbytes.c.

225 {
227  return InterlockedIncrement(&This->ref);
228 }
static HGLOBALLockBytesImpl * impl_from_ILockBytes(ILockBytes *iface)
Definition: memlockbytes.c:72
#define InterlockedIncrement
Definition: armddk.h:53

◆ HGLOBALLockBytesImpl_Flush()

static HRESULT WINAPI HGLOBALLockBytesImpl_Flush ( ILockBytes iface)
static

Definition at line 398 of file memlockbytes.c.

399 {
400  return S_OK;
401 }
#define S_OK
Definition: intsafe.h:59

◆ HGLOBALLockBytesImpl_LockRegion()

static HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion ( ILockBytes iface,
ULARGE_INTEGER  libOffset,
ULARGE_INTEGER  cb,
DWORD  dwLockType 
)
static

Definition at line 447 of file memlockbytes.c.

452 {
453  return STG_E_INVALIDFUNCTION;
454 }
#define STG_E_INVALIDFUNCTION
Definition: winerror.h:2564

◆ HGLOBALLockBytesImpl_QueryInterface()

static HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface ( ILockBytes iface,
REFIID  riid,
void **  ppvObject 
)
static

Definition at line 195 of file memlockbytes.c.

199 {
201 
202  if (ppvObject==0)
203  return E_INVALIDARG;
204 
205  *ppvObject = 0;
206 
207  if (IsEqualIID(riid, &IID_IUnknown) ||
208  IsEqualIID(riid, &IID_ILockBytes))
209  {
210  *ppvObject = &This->ILockBytes_iface;
211  }
212  else
213  return E_NOINTERFACE;
214 
215  ILockBytes_AddRef(iface);
216 
217  return S_OK;
218 }
#define E_NOINTERFACE
Definition: winerror.h:2364
REFIID riid
Definition: precomp.h:44
static HGLOBALLockBytesImpl * impl_from_ILockBytes(ILockBytes *iface)
Definition: memlockbytes.c:72
#define E_INVALIDARG
Definition: ddrawi.h:101
const GUID IID_IUnknown
REFIID LPVOID * ppvObject
Definition: precomp.h:44
#define S_OK
Definition: intsafe.h:59
#define IsEqualIID(riid1, riid2)
Definition: guiddef.h:90

◆ HGLOBALLockBytesImpl_ReadAt()

static HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt ( ILockBytes iface,
ULARGE_INTEGER  ulOffset,
void pv,
ULONG  cb,
ULONG pcbRead 
)
static

Definition at line 261 of file memlockbytes.c.

267 {
269 
270  void* supportBuffer;
271  ULONG bytesReadBuffer = 0;
272  ULONG bytesToReadFromBuffer;
273 
274  /*
275  * If the caller is not interested in the number of bytes read,
276  * we use another buffer to avoid "if" statements in the code.
277  */
278  if (pcbRead == 0)
279  pcbRead = &bytesReadBuffer;
280 
281  /*
282  * Make sure the offset is valid.
283  */
284  if (ulOffset.u.LowPart > This->byteArraySize.u.LowPart)
285  return E_FAIL;
286 
287  /*
288  * Using the known size of the array, calculate the number of bytes
289  * to read.
290  */
291  bytesToReadFromBuffer = min(This->byteArraySize.u.LowPart -
292  ulOffset.u.LowPart, cb);
293 
294  /*
295  * Lock the buffer in position and copy the data.
296  */
297  supportBuffer = GlobalLock(This->supportHandle);
298 
299  memcpy(pv,
300  (char *) supportBuffer + ulOffset.u.LowPart,
301  bytesToReadFromBuffer);
302 
303  /*
304  * Return the number of bytes read.
305  */
306  *pcbRead = bytesToReadFromBuffer;
307 
308  /*
309  * Cleanup
310  */
311  GlobalUnlock(This->supportHandle);
312 
313  /*
314  * The function returns S_OK if the specified number of bytes were read
315  * or the end of the array was reached.
316  * It returns STG_E_READFAULT if the number of bytes to read does not equal
317  * the number of bytes actually read.
318  */
319  if(*pcbRead == cb)
320  return S_OK;
321 
322  return STG_E_READFAULT;
323 }
static HGLOBALLockBytesImpl * impl_from_ILockBytes(ILockBytes *iface)
Definition: memlockbytes.c:72
#define E_FAIL
Definition: ddrawi.h:102
struct _ULARGE_INTEGER::@3737 u
static DWORD cb
Definition: integrity.c:41
LPVOID NTAPI GlobalLock(HGLOBAL hMem)
Definition: heapmem.c:755
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define S_OK
Definition: intsafe.h:59
#define STG_E_READFAULT
Definition: winerror.h:2576
#define min(a, b)
Definition: monoChain.cc:55
BOOL NTAPI GlobalUnlock(HGLOBAL hMem)
Definition: heapmem.c:1190
unsigned int ULONG
Definition: retypes.h:1

◆ HGLOBALLockBytesImpl_Release()

static ULONG WINAPI HGLOBALLockBytesImpl_Release ( ILockBytes iface)
static

Definition at line 234 of file memlockbytes.c.

235 {
237  ULONG ref;
238 
239  ref = InterlockedDecrement(&This->ref);
240  if (!ref)
241  {
242  if (This->deleteOnRelease)
243  {
244  GlobalFree(This->supportHandle);
245  This->supportHandle = 0;
246  }
248  }
249 
250  return ref;
251 }
static HGLOBALLockBytesImpl * impl_from_ILockBytes(ILockBytes *iface)
Definition: memlockbytes.c:72
Definition: send.c:47
GLenum GLint ref
Definition: glext.h:6028
#define GetProcessHeap()
Definition: compat.h:395
HGLOBAL NTAPI GlobalFree(HGLOBAL hMem)
Definition: heapmem.c:611
#define InterlockedDecrement
Definition: armddk.h:52
unsigned int ULONG
Definition: retypes.h:1
#define HeapFree(x, y, z)
Definition: compat.h:394

◆ HGLOBALLockBytesImpl_SetSize()

static HRESULT WINAPI HGLOBALLockBytesImpl_SetSize ( ILockBytes iface,
ULARGE_INTEGER  libNewSize 
)
static

Definition at line 410 of file memlockbytes.c.

413 {
415  HGLOBAL supportHandle;
416 
417  /*
418  * As documented.
419  */
420  if (libNewSize.u.HighPart != 0)
421  return STG_E_INVALIDFUNCTION;
422 
423  if (This->byteArraySize.u.LowPart == libNewSize.u.LowPart)
424  return S_OK;
425 
426  /*
427  * Re allocate the HGlobal to fit the new size of the stream.
428  */
429  supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
430 
431  if (supportHandle == 0)
432  return STG_E_MEDIUMFULL;
433 
434  This->supportHandle = supportHandle;
435  This->byteArraySize.u.LowPart = libNewSize.u.LowPart;
436 
437  return S_OK;
438 }
HGLOBAL NTAPI GlobalReAlloc(HGLOBAL hMem, SIZE_T dwBytes, UINT uFlags)
Definition: heapmem.c:825
static HGLOBALLockBytesImpl * impl_from_ILockBytes(ILockBytes *iface)
Definition: memlockbytes.c:72
#define STG_E_MEDIUMFULL
Definition: winerror.h:2581
#define STG_E_INVALIDFUNCTION
Definition: winerror.h:2564
struct _ULARGE_INTEGER::@3737 u
#define S_OK
Definition: intsafe.h:59

◆ HGLOBALLockBytesImpl_Stat()

static HRESULT WINAPI HGLOBALLockBytesImpl_Stat ( ILockBytes iface,
STATSTG *  pstatstg,
DWORD  grfStatFlag 
)
static

Definition at line 480 of file memlockbytes.c.

484 {
486 
487  memset(pstatstg, 0, sizeof(STATSTG));
488 
489  pstatstg->pwcsName = NULL;
490  pstatstg->type = STGTY_LOCKBYTES;
491  pstatstg->cbSize = This->byteArraySize;
492 
493  return S_OK;
494 }
static HGLOBALLockBytesImpl * impl_from_ILockBytes(ILockBytes *iface)
Definition: memlockbytes.c:72
smooth NULL
Definition: ftsmooth.c:416
#define S_OK
Definition: intsafe.h:59
#define memset(x, y, z)
Definition: compat.h:39

◆ HGLOBALLockBytesImpl_UnlockRegion()

static HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion ( ILockBytes iface,
ULARGE_INTEGER  libOffset,
ULARGE_INTEGER  cb,
DWORD  dwLockType 
)
static

Definition at line 463 of file memlockbytes.c.

468 {
469  return STG_E_INVALIDFUNCTION;
470 }
#define STG_E_INVALIDFUNCTION
Definition: winerror.h:2564

◆ HGLOBALLockBytesImpl_WriteAt()

static HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt ( ILockBytes iface,
ULARGE_INTEGER  ulOffset,
const void pv,
ULONG  cb,
ULONG pcbWritten 
)
static

Definition at line 333 of file memlockbytes.c.

339 {
341 
342  void* supportBuffer;
343  ULARGE_INTEGER newSize;
344  ULONG bytesWritten = 0;
345 
346  /*
347  * If the caller is not interested in the number of bytes written,
348  * we use another buffer to avoid "if" statements in the code.
349  */
350  if (pcbWritten == 0)
351  pcbWritten = &bytesWritten;
352 
353  if (cb == 0)
354  {
355  return S_OK;
356  }
357  else
358  {
359  newSize.u.HighPart = 0;
360  newSize.u.LowPart = ulOffset.u.LowPart + cb;
361  }
362 
363  /*
364  * Verify if we need to grow the stream
365  */
366  if (newSize.u.LowPart > This->byteArraySize.u.LowPart)
367  {
368  /* grow stream */
369  if (ILockBytes_SetSize(iface, newSize) == STG_E_MEDIUMFULL)
370  return STG_E_MEDIUMFULL;
371  }
372 
373  /*
374  * Lock the buffer in position and copy the data.
375  */
376  supportBuffer = GlobalLock(This->supportHandle);
377 
378  memcpy((char *) supportBuffer + ulOffset.u.LowPart, pv, cb);
379 
380  /*
381  * Return the number of bytes written.
382  */
383  *pcbWritten = cb;
384 
385  /*
386  * Cleanup
387  */
388  GlobalUnlock(This->supportHandle);
389 
390  return S_OK;
391 }
static HGLOBALLockBytesImpl * impl_from_ILockBytes(ILockBytes *iface)
Definition: memlockbytes.c:72
#define STG_E_MEDIUMFULL
Definition: winerror.h:2581
struct _ULARGE_INTEGER::@3737 u
static DWORD cb
Definition: integrity.c:41
LPVOID NTAPI GlobalLock(HGLOBAL hMem)
Definition: heapmem.c:755
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
GLenum GLsizei GLuint GLint * bytesWritten
Definition: glext.h:11123
#define S_OK
Definition: intsafe.h:59
BOOL NTAPI GlobalUnlock(HGLOBAL hMem)
Definition: heapmem.c:1190
unsigned int ULONG
Definition: retypes.h:1

◆ impl_from_ILockBytes()

static HGLOBALLockBytesImpl* impl_from_ILockBytes ( ILockBytes iface)
inlinestatic

Definition at line 72 of file memlockbytes.c.

73 {
74  return CONTAINING_RECORD(iface, HGLOBALLockBytesImpl, ILockBytes_iface);
75 }
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560

Referenced by GetHGlobalFromILockBytes(), HGLOBALLockBytesImpl_AddRef(), HGLOBALLockBytesImpl_QueryInterface(), HGLOBALLockBytesImpl_ReadAt(), HGLOBALLockBytesImpl_Release(), HGLOBALLockBytesImpl_SetSize(), HGLOBALLockBytesImpl_Stat(), and HGLOBALLockBytesImpl_WriteAt().

◆ WINE_DEFAULT_DEBUG_CHANNEL()

WINE_DEFAULT_DEBUG_CHANNEL ( ole  )

Variable Documentation

◆ HGLOBALLockBytesImpl_Vtbl

static const ILockBytesVtbl HGLOBALLockBytesImpl_Vtbl
static
Initial value:
=
{
}
static HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt(ILockBytes *iface, ULARGE_INTEGER ulOffset, const void *pv, ULONG cb, ULONG *pcbWritten)
Definition: memlockbytes.c:333
static HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(ILockBytes *iface, ULARGE_INTEGER libNewSize)
Definition: memlockbytes.c:410
static ULONG WINAPI HGLOBALLockBytesImpl_Release(ILockBytes *iface)
Definition: memlockbytes.c:234
static HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(ILockBytes *iface, REFIID riid, void **ppvObject)
Definition: memlockbytes.c:195
static HRESULT WINAPI HGLOBALLockBytesImpl_Stat(ILockBytes *iface, STATSTG *pstatstg, DWORD grfStatFlag)
Definition: memlockbytes.c:480
static HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(ILockBytes *iface, ULARGE_INTEGER ulOffset, void *pv, ULONG cb, ULONG *pcbRead)
Definition: memlockbytes.c:261
static HRESULT WINAPI HGLOBALLockBytesImpl_Flush(ILockBytes *iface)
Definition: memlockbytes.c:398
static ULONG WINAPI HGLOBALLockBytesImpl_AddRef(ILockBytes *iface)
Definition: memlockbytes.c:224
static HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion(ILockBytes *iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
Definition: memlockbytes.c:463
static HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion(ILockBytes *iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
Definition: memlockbytes.c:447

Definition at line 77 of file memlockbytes.c.

Referenced by CreateILockBytesOnHGlobal(), and GetHGlobalFromILockBytes().