1 /******************************************************************************
3 * Global memory implementation of ILockBytes.
5 * Copyright 1999 Thuy Nguyen
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(ole);
23 /******************************************************************************
24 * HGLOBALLockBytesImpl definition.
26 * This class imlements the ILockBytes inteface and represents a byte array
27 * object supported by an HGLOBAL pointer.
29 struct HGLOBALLockBytesImpl
32 * Needs to be the first item in the stuct
33 * since we want to cast this in an ILockBytes pointer
35 ICOM_VFIELD(ILockBytes);
43 * Support for the LockBytes object
45 HGLOBAL supportHandle;
48 * This flag is TRUE if the HGLOBAL is destroyed when the object
49 * is finally released.
54 * Helper variable that contains the size of the byte array
56 ULARGE_INTEGER byteArraySize;
59 typedef struct HGLOBALLockBytesImpl HGLOBALLockBytesImpl;
62 * Method definition for the HGLOBALLockBytesImpl class.
64 HGLOBALLockBytesImpl* HGLOBALLockBytesImpl_Construct(
66 BOOL fDeleteOnRelease);
68 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This);
70 HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(
72 REFIID riid, /* [in] */
73 void** ppvObject); /* [iid_is][out] */
75 ULONG WINAPI HGLOBALLockBytesImpl_AddRef(
78 ULONG WINAPI HGLOBALLockBytesImpl_Release(
81 HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(
83 ULARGE_INTEGER ulOffset, /* [in] */
84 void* pv, /* [length_is][size_is][out] */
86 ULONG* pcbRead); /* [out] */
88 HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt(
90 ULARGE_INTEGER ulOffset, /* [in] */
91 const void* pv, /* [size_is][in] */
93 ULONG* pcbWritten); /* [out] */
95 HRESULT WINAPI HGLOBALLockBytesImpl_Flush(
98 HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(
100 ULARGE_INTEGER libNewSize); /* [in] */
102 HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion(
104 ULARGE_INTEGER libOffset, /* [in] */
105 ULARGE_INTEGER cb, /* [in] */
106 DWORD dwLockType); /* [in] */
108 HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion(
110 ULARGE_INTEGER libOffset, /* [in] */
111 ULARGE_INTEGER cb, /* [in] */
112 DWORD dwLockType); /* [in] */
114 HRESULT WINAPI HGLOBALLockBytesImpl_Stat(
116 STATSTG* pstatstg, /* [out] */
117 DWORD grfStatFlag); /* [in] */
120 * Virtual function table for the HGLOBALLockBytesImpl class.
122 static ICOM_VTABLE(ILockBytes) HGLOBALLockBytesImpl_Vtbl =
124 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
125 HGLOBALLockBytesImpl_QueryInterface,
126 HGLOBALLockBytesImpl_AddRef,
127 HGLOBALLockBytesImpl_Release,
128 HGLOBALLockBytesImpl_ReadAt,
129 HGLOBALLockBytesImpl_WriteAt,
130 HGLOBALLockBytesImpl_Flush,
131 HGLOBALLockBytesImpl_SetSize,
132 HGLOBALLockBytesImpl_LockRegion,
133 HGLOBALLockBytesImpl_UnlockRegion,
134 HGLOBALLockBytesImpl_Stat,
137 /******************************************************************************
138 * CreateILockBytesOnHGlobal [OLE32.57]
140 HRESULT WINAPI CreateILockBytesOnHGlobal(HGLOBAL hGlobal,
141 BOOL fDeleteOnRelease,
142 LPLOCKBYTES* ppLkbyt)
144 HGLOBALLockBytesImpl* newLockBytes;
146 newLockBytes = HGLOBALLockBytesImpl_Construct(hGlobal, fDeleteOnRelease);
148 if (newLockBytes != NULL)
150 return IUnknown_QueryInterface((IUnknown*)newLockBytes,
155 return E_OUTOFMEMORY;
158 /******************************************************************************
159 * GetHGlobalFromILockBytes [OLE32.70]
161 HRESULT WINAPI GetHGlobalFromILockBytes(ILockBytes* plkbyt, HGLOBAL* phglobal)
163 HGLOBALLockBytesImpl* const pMemLockBytes = (HGLOBALLockBytesImpl*)plkbyt;
165 if (ICOM_VTBL(pMemLockBytes) == &HGLOBALLockBytesImpl_Vtbl)
166 *phglobal = pMemLockBytes->supportHandle;
176 /******************************************************************************
178 * HGLOBALLockBytesImpl implementation
182 /******************************************************************************
183 * This is the constructor for the HGLOBALLockBytesImpl class.
186 * hGlobal - Handle that will support the stream. can be NULL.
187 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
188 * when the IStream object is destroyed.
190 HGLOBALLockBytesImpl* HGLOBALLockBytesImpl_Construct(HGLOBAL hGlobal,
191 BOOL fDeleteOnRelease)
193 HGLOBALLockBytesImpl* newLockBytes;
194 newLockBytes = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALLockBytesImpl));
199 * Set up the virtual function table and reference count.
201 ICOM_VTBL(newLockBytes) = &HGLOBALLockBytesImpl_Vtbl;
202 newLockBytes->ref = 0;
205 * Initialize the support.
207 newLockBytes->supportHandle = hGlobal;
208 newLockBytes->deleteOnRelease = fDeleteOnRelease;
211 * This method will allocate a handle if one is not supplied.
213 if (newLockBytes->supportHandle == 0)
215 newLockBytes->supportHandle = GlobalAlloc(GMEM_MOVEABLE |
221 * Initialize the size of the array to the size of the handle.
223 newLockBytes->byteArraySize.s.HighPart = 0;
224 newLockBytes->byteArraySize.s.LowPart = GlobalSize(
225 newLockBytes->supportHandle);
231 /******************************************************************************
232 * This is the destructor of the HGLOBALStreamImpl class.
234 * This method will clean-up all the resources used-up by the given
235 * HGLOBALLockBytesImpl class. The pointer passed-in to this function will be
236 * freed and will not be valid anymore.
238 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This)
241 * Release the HGlobal if the constructor asked for that.
243 if (This->deleteOnRelease)
245 GlobalFree(This->supportHandle);
246 This->supportHandle = 0;
250 * Finally, free the memory used-up by the class.
252 HeapFree(GetProcessHeap(), 0, This);
255 /******************************************************************************
256 * This implements the IUnknown method QueryInterface for this
259 HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(
261 REFIID riid, /* [in] */
262 void** ppvObject) /* [iid_is][out] */
264 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
267 * Perform a sanity check on the parameters.
273 * Initialize the return parameter.
278 * Compare the riid with the interface IDs implemented by this object.
280 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
282 *ppvObject = (ILockBytes*)This;
284 else if (memcmp(&IID_ILockBytes, riid, sizeof(IID_ILockBytes)) == 0)
286 *ppvObject = (ILockBytes*)This;
290 * Check that we obtained an interface.
293 return E_NOINTERFACE;
296 * Query Interface always increases the reference count by one when it is
299 HGLOBALLockBytesImpl_AddRef(iface);
304 /******************************************************************************
305 * This implements the IUnknown method AddRef for this
308 ULONG WINAPI HGLOBALLockBytesImpl_AddRef(ILockBytes* iface)
310 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
317 /******************************************************************************
318 * This implements the IUnknown method Release for this
321 ULONG WINAPI HGLOBALLockBytesImpl_Release(ILockBytes* iface)
323 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
332 * If the reference count goes down to 0, perform suicide.
336 HGLOBALLockBytesImpl_Destroy(This);
342 /******************************************************************************
343 * This method is part of the ILockBytes interface.
345 * It reads a block of information from the byte array at the specified
348 * See the documentation of ILockBytes for more info.
350 HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(
352 ULARGE_INTEGER ulOffset, /* [in] */
353 void* pv, /* [length_is][size_is][out] */
355 ULONG* pcbRead) /* [out] */
357 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
360 ULONG bytesReadBuffer = 0;
361 ULONG bytesToReadFromBuffer;
364 * If the caller is not interested in the number of bytes read,
365 * we use another buffer to avoid "if" statements in the code.
368 pcbRead = &bytesReadBuffer;
371 * Make sure the offset is valid.
373 if (ulOffset.s.LowPart > This->byteArraySize.s.LowPart)
377 * Using the known size of the array, calculate the number of bytes
380 bytesToReadFromBuffer = min(This->byteArraySize.s.LowPart -
381 ulOffset.s.LowPart, cb);
384 * Lock the buffer in position and copy the data.
386 supportBuffer = GlobalLock(This->supportHandle);
389 (char *) supportBuffer + ulOffset.s.LowPart,
390 bytesToReadFromBuffer);
393 * Return the number of bytes read.
395 *pcbRead = bytesToReadFromBuffer;
400 GlobalUnlock(This->supportHandle);
403 * The function returns S_OK if the specified number of bytes were read
404 * or the end of the array was reached.
405 * It returns STG_E_READFAULT if the number of bytes to read does not equal
406 * the number of bytes actually read.
411 return STG_E_READFAULT;
414 /******************************************************************************
415 * This method is part of the ILockBytes interface.
417 * It writes the specified bytes at the specified offset.
418 * position. If the array is too small, it will be resized.
420 * See the documentation of ILockBytes for more info.
422 HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt(
424 ULARGE_INTEGER ulOffset, /* [in] */
425 const void* pv, /* [size_is][in] */
427 ULONG* pcbWritten) /* [out] */
429 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
432 ULARGE_INTEGER newSize;
433 ULONG bytesWritten = 0;
436 * If the caller is not interested in the number of bytes written,
437 * we use another buffer to avoid "if" statements in the code.
440 pcbWritten = &bytesWritten;
448 newSize.s.HighPart = 0;
449 newSize.s.LowPart = ulOffset.s.LowPart + cb;
453 * Verify if we need to grow the stream
455 if (newSize.s.LowPart > This->byteArraySize.s.LowPart)
458 if (HGLOBALLockBytesImpl_SetSize(iface, newSize) == STG_E_MEDIUMFULL)
459 return STG_E_MEDIUMFULL;
463 * Lock the buffer in position and copy the data.
465 supportBuffer = GlobalLock(This->supportHandle);
467 memcpy((char *) supportBuffer + ulOffset.s.LowPart, pv, cb);
470 * Return the number of bytes written.
477 GlobalUnlock(This->supportHandle);
482 /******************************************************************************
483 * This method is part of the ILockBytes interface.
485 * See the documentation of ILockBytes for more info.
487 HRESULT WINAPI HGLOBALLockBytesImpl_Flush(ILockBytes* iface)
492 /******************************************************************************
493 * This method is part of the ILockBytes interface.
495 * It will change the size of the byte array.
497 * See the documentation of ILockBytes for more info.
499 HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(
501 ULARGE_INTEGER libNewSize) /* [in] */
503 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
508 if (libNewSize.s.HighPart != 0)
509 return STG_E_INVALIDFUNCTION;
511 if (This->byteArraySize.s.LowPart == libNewSize.s.LowPart)
515 * Re allocate the HGlobal to fit the new size of the stream.
517 This->supportHandle = GlobalReAlloc(This->supportHandle,
518 libNewSize.s.LowPart,
521 if (This->supportHandle == 0)
522 return STG_E_MEDIUMFULL;
524 This->byteArraySize.s.LowPart = libNewSize.s.LowPart;
529 /******************************************************************************
530 * This method is part of the ILockBytes interface.
532 * The global memory implementation of ILockBytes does not support locking.
534 * See the documentation of ILockBytes for more info.
536 HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion(
538 ULARGE_INTEGER libOffset, /* [in] */
539 ULARGE_INTEGER cb, /* [in] */
540 DWORD dwLockType) /* [in] */
542 return STG_E_INVALIDFUNCTION;
545 /******************************************************************************
546 * This method is part of the ILockBytes interface.
548 * The global memory implementation of ILockBytes does not support locking.
550 * See the documentation of ILockBytes for more info.
552 HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion(
554 ULARGE_INTEGER libOffset, /* [in] */
555 ULARGE_INTEGER cb, /* [in] */
556 DWORD dwLockType) /* [in] */
558 return STG_E_INVALIDFUNCTION;
561 /******************************************************************************
562 * This method is part of the ILockBytes interface.
564 * This method returns information about the current
567 * See the documentation of ILockBytes for more info.
569 HRESULT WINAPI HGLOBALLockBytesImpl_Stat(
571 STATSTG* pstatstg, /* [out] */
572 DWORD grfStatFlag) /* [in] */
574 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
576 memset(pstatstg, 0, sizeof(STATSTG));
578 pstatstg->pwcsName = NULL;
579 pstatstg->type = STGTY_LOCKBYTES;
580 pstatstg->cbSize = This->byteArraySize;