1 /******************************************************************************
3 * Global memory implementation of ILockBytes.
5 * Copyright 1999 Thuy Nguyen
14 #include "debugtools.h"
16 DEFAULT_DEBUG_CHANNEL(ole)
18 /******************************************************************************
19 * HGLOBALLockBytesImpl definition.
21 * This class imlements the ILockBytes inteface and represents a byte array
22 * object supported by an HGLOBAL pointer.
24 struct HGLOBALLockBytesImpl
27 * Needs to be the first item in the stuct
28 * since we want to cast this in an ILockBytes pointer
30 ICOM_VFIELD(ILockBytes);
38 * Support for the LockBytes object
40 HGLOBAL supportHandle;
43 * This flag is TRUE if the HGLOBAL is destroyed when the object
44 * is finally released.
49 * Helper variable that contains the size of the byte array
51 ULARGE_INTEGER byteArraySize;
54 typedef struct HGLOBALLockBytesImpl HGLOBALLockBytesImpl;
57 * Method definition for the HGLOBALLockBytesImpl class.
59 HGLOBALLockBytesImpl* HGLOBALLockBytesImpl_Construct(
61 BOOL fDeleteOnRelease);
63 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This);
65 HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(
67 REFIID riid, /* [in] */
68 void** ppvObject); /* [iid_is][out] */
70 ULONG WINAPI HGLOBALLockBytesImpl_AddRef(
73 ULONG WINAPI HGLOBALLockBytesImpl_Release(
76 HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(
78 ULARGE_INTEGER ulOffset, /* [in] */
79 void* pv, /* [length_is][size_is][out] */
81 ULONG* pcbRead); /* [out] */
83 HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt(
85 ULARGE_INTEGER ulOffset, /* [in] */
86 const void* pv, /* [size_is][in] */
88 ULONG* pcbWritten); /* [out] */
90 HRESULT WINAPI HGLOBALLockBytesImpl_Flush(
93 HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(
95 ULARGE_INTEGER libNewSize); /* [in] */
97 HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion(
99 ULARGE_INTEGER libOffset, /* [in] */
100 ULARGE_INTEGER cb, /* [in] */
101 DWORD dwLockType); /* [in] */
103 HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion(
105 ULARGE_INTEGER libOffset, /* [in] */
106 ULARGE_INTEGER cb, /* [in] */
107 DWORD dwLockType); /* [in] */
109 HRESULT WINAPI HGLOBALLockBytesImpl_Stat(
111 STATSTG* pstatstg, /* [out] */
112 DWORD grfStatFlag); /* [in] */
115 * Virtual function table for the HGLOBALLockBytesImpl class.
117 static ICOM_VTABLE(ILockBytes) HGLOBALLockBytesImpl_Vtbl =
119 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
120 HGLOBALLockBytesImpl_QueryInterface,
121 HGLOBALLockBytesImpl_AddRef,
122 HGLOBALLockBytesImpl_Release,
123 HGLOBALLockBytesImpl_ReadAt,
124 HGLOBALLockBytesImpl_WriteAt,
125 HGLOBALLockBytesImpl_Flush,
126 HGLOBALLockBytesImpl_SetSize,
127 HGLOBALLockBytesImpl_LockRegion,
128 HGLOBALLockBytesImpl_UnlockRegion,
129 HGLOBALLockBytesImpl_Stat,
132 /******************************************************************************
133 * CreateILockBytesOnHGlobal [OLE32.57]
135 HRESULT WINAPI CreateILockBytesOnHGlobal(HGLOBAL hGlobal,
136 BOOL fDeleteOnRelease,
137 LPLOCKBYTES* ppLkbyt)
139 HGLOBALLockBytesImpl* newLockBytes;
141 newLockBytes = HGLOBALLockBytesImpl_Construct(hGlobal, fDeleteOnRelease);
143 if (newLockBytes != NULL)
145 return IUnknown_QueryInterface((IUnknown*)newLockBytes,
150 return E_OUTOFMEMORY;
153 /******************************************************************************
154 * GetHGlobalFromILockBytes [OLE32.70]
156 HRESULT WINAPI GetHGlobalFromILockBytes(ILockBytes* plkbyt, HGLOBAL* phglobal)
158 HGLOBALLockBytesImpl* const pMemLockBytes = (HGLOBALLockBytesImpl*)plkbyt;
160 if (ICOM_VTBL(pMemLockBytes) == &HGLOBALLockBytesImpl_Vtbl)
161 *phglobal = pMemLockBytes->supportHandle;
171 /******************************************************************************
173 * HGLOBALLockBytesImpl implementation
177 /******************************************************************************
178 * This is the constructor for the HGLOBALLockBytesImpl class.
181 * hGlobal - Handle that will support the stream. can be NULL.
182 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
183 * when the IStream object is destroyed.
185 HGLOBALLockBytesImpl* HGLOBALLockBytesImpl_Construct(HGLOBAL hGlobal,
186 BOOL fDeleteOnRelease)
188 HGLOBALLockBytesImpl* newLockBytes;
189 newLockBytes = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALLockBytesImpl));
194 * Set up the virtual function table and reference count.
196 ICOM_VTBL(newLockBytes) = &HGLOBALLockBytesImpl_Vtbl;
197 newLockBytes->ref = 0;
200 * Initialize the support.
202 newLockBytes->supportHandle = hGlobal;
203 newLockBytes->deleteOnRelease = fDeleteOnRelease;
206 * This method will allocate a handle if one is not supplied.
208 if (newLockBytes->supportHandle == 0)
210 newLockBytes->supportHandle = GlobalAlloc(GMEM_MOVEABLE |
216 * Initialize the size of the array to the size of the handle.
218 newLockBytes->byteArraySize.s.HighPart = 0;
219 newLockBytes->byteArraySize.s.LowPart = GlobalSize(
220 newLockBytes->supportHandle);
226 /******************************************************************************
227 * This is the destructor of the HGLOBALStreamImpl class.
229 * This method will clean-up all the resources used-up by the given
230 * HGLOBALLockBytesImpl class. The pointer passed-in to this function will be
231 * freed and will not be valid anymore.
233 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This)
236 * Release the HGlobal if the constructor asked for that.
238 if (This->deleteOnRelease)
240 GlobalFree(This->supportHandle);
241 This->supportHandle = 0;
245 * Finally, free the memory used-up by the class.
247 HeapFree(GetProcessHeap(), 0, This);
250 /******************************************************************************
251 * This implements the IUnknown method QueryInterface for this
254 HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(
256 REFIID riid, /* [in] */
257 void** ppvObject) /* [iid_is][out] */
259 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
262 * Perform a sanity check on the parameters.
268 * Initialize the return parameter.
273 * Compare the riid with the interface IDs implemented by this object.
275 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
277 *ppvObject = (ILockBytes*)This;
279 else if (memcmp(&IID_ILockBytes, riid, sizeof(IID_ILockBytes)) == 0)
281 *ppvObject = (ILockBytes*)This;
285 * Check that we obtained an interface.
288 return E_NOINTERFACE;
291 * Query Interface always increases the reference count by one when it is
294 HGLOBALLockBytesImpl_AddRef(iface);
299 /******************************************************************************
300 * This implements the IUnknown method AddRef for this
303 ULONG WINAPI HGLOBALLockBytesImpl_AddRef(ILockBytes* iface)
305 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
312 /******************************************************************************
313 * This implements the IUnknown method Release for this
316 ULONG WINAPI HGLOBALLockBytesImpl_Release(ILockBytes* iface)
318 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
327 * If the reference count goes down to 0, perform suicide.
331 HGLOBALLockBytesImpl_Destroy(This);
337 /******************************************************************************
338 * This method is part of the ILockBytes interface.
340 * It reads a block of information from the byte array at the specified
343 * See the documentation of ILockBytes for more info.
345 HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(
347 ULARGE_INTEGER ulOffset, /* [in] */
348 void* pv, /* [length_is][size_is][out] */
350 ULONG* pcbRead) /* [out] */
352 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
355 ULONG bytesReadBuffer = 0;
356 ULONG bytesToReadFromBuffer;
359 * If the caller is not interested in the number of bytes read,
360 * we use another buffer to avoid "if" statements in the code.
363 pcbRead = &bytesReadBuffer;
366 * Make sure the offset is valid.
368 if (ulOffset.s.LowPart > This->byteArraySize.s.LowPart)
372 * Using the known size of the array, calculate the number of bytes
375 bytesToReadFromBuffer = MIN(This->byteArraySize.s.LowPart -
376 ulOffset.s.LowPart, cb);
379 * Lock the buffer in position and copy the data.
381 supportBuffer = GlobalLock(This->supportHandle);
384 (char *) supportBuffer + ulOffset.s.LowPart,
385 bytesToReadFromBuffer);
388 * Return the number of bytes read.
390 *pcbRead = bytesToReadFromBuffer;
395 GlobalUnlock(This->supportHandle);
398 * The function returns S_OK if the specified number of bytes were read
399 * or the end of the array was reached.
400 * It returns STG_E_READFAULT if the number of bytes to read does not equal
401 * the number of bytes actually read.
406 return STG_E_READFAULT;
409 /******************************************************************************
410 * This method is part of the ILockBytes interface.
412 * It writes the specified bytes at the specified offset.
413 * position. If the array is too small, it will be resized.
415 * See the documentation of ILockBytes for more info.
417 HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt(
419 ULARGE_INTEGER ulOffset, /* [in] */
420 const void* pv, /* [size_is][in] */
422 ULONG* pcbWritten) /* [out] */
424 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
427 ULARGE_INTEGER newSize;
428 ULONG bytesWritten = 0;
431 * If the caller is not interested in the number of bytes written,
432 * we use another buffer to avoid "if" statements in the code.
435 pcbWritten = &bytesWritten;
443 newSize.s.HighPart = 0;
444 newSize.s.LowPart = ulOffset.s.LowPart + cb;
448 * Verify if we need to grow the stream
450 if (newSize.s.LowPart > This->byteArraySize.s.LowPart)
453 if (HGLOBALLockBytesImpl_SetSize(iface, newSize) == STG_E_MEDIUMFULL)
454 return STG_E_MEDIUMFULL;
458 * Lock the buffer in position and copy the data.
460 supportBuffer = GlobalLock(This->supportHandle);
462 memcpy((char *) supportBuffer + ulOffset.s.LowPart, pv, cb);
465 * Return the number of bytes written.
472 GlobalUnlock(This->supportHandle);
477 /******************************************************************************
478 * This method is part of the ILockBytes interface.
480 * See the documentation of ILockBytes for more info.
482 HRESULT WINAPI HGLOBALLockBytesImpl_Flush(ILockBytes* iface)
487 /******************************************************************************
488 * This method is part of the ILockBytes interface.
490 * It will change the size of the byte array.
492 * See the documentation of ILockBytes for more info.
494 HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(
496 ULARGE_INTEGER libNewSize) /* [in] */
498 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
503 if (libNewSize.s.HighPart != 0)
504 return STG_E_INVALIDFUNCTION;
506 if (This->byteArraySize.s.LowPart == libNewSize.s.LowPart)
510 * Re allocate the HGlobal to fit the new size of the stream.
512 This->supportHandle = GlobalReAlloc(This->supportHandle,
513 libNewSize.s.LowPart,
516 if (This->supportHandle == 0)
517 return STG_E_MEDIUMFULL;
519 This->byteArraySize.s.LowPart = libNewSize.s.LowPart;
524 /******************************************************************************
525 * This method is part of the ILockBytes interface.
527 * The global memory implementation of ILockBytes does not support locking.
529 * See the documentation of ILockBytes for more info.
531 HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion(
533 ULARGE_INTEGER libOffset, /* [in] */
534 ULARGE_INTEGER cb, /* [in] */
535 DWORD dwLockType) /* [in] */
537 return STG_E_INVALIDFUNCTION;
540 /******************************************************************************
541 * This method is part of the ILockBytes interface.
543 * The global memory implementation of ILockBytes does not support locking.
545 * See the documentation of ILockBytes for more info.
547 HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion(
549 ULARGE_INTEGER libOffset, /* [in] */
550 ULARGE_INTEGER cb, /* [in] */
551 DWORD dwLockType) /* [in] */
553 return STG_E_INVALIDFUNCTION;
556 /******************************************************************************
557 * This method is part of the ILockBytes interface.
559 * This method returns information about the current
562 * See the documentation of ILockBytes for more info.
564 HRESULT WINAPI HGLOBALLockBytesImpl_Stat(
566 STATSTG* pstatstg, /* [out] */
567 DWORD grfStatFlag) /* [in] */
569 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
571 memset(pstatstg, 0, sizeof(STATSTG));
573 pstatstg->pwcsName = NULL;
574 pstatstg->type = STGTY_LOCKBYTES;
575 pstatstg->cbSize = This->byteArraySize;