1 /******************************************************************************
3 * Global memory implementation of ILockBytes.
5 * Copyright 1999 Thuy Nguyen
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
43 /******************************************************************************
44 * HGLOBALLockBytesImpl definition.
46 * This class imlements the ILockBytes inteface and represents a byte array
47 * object supported by an HGLOBAL pointer.
49 struct HGLOBALLockBytesImpl
52 * Needs to be the first item in the struct
53 * since we want to cast this in an ILockBytes pointer
55 const ILockBytesVtbl *lpVtbl;
63 * Support for the LockBytes object
65 HGLOBAL supportHandle;
68 * This flag is TRUE if the HGLOBAL is destroyed when the object
69 * is finally released.
74 * Helper variable that contains the size of the byte array
76 ULARGE_INTEGER byteArraySize;
79 typedef struct HGLOBALLockBytesImpl HGLOBALLockBytesImpl;
82 * Method definition for the HGLOBALLockBytesImpl class.
84 HGLOBALLockBytesImpl* HGLOBALLockBytesImpl_Construct(
86 BOOL fDeleteOnRelease);
88 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This);
90 static HRESULT WINAPI HGLOBALLockBytesImpl_SetSize( ILockBytes* iface, ULARGE_INTEGER libNewSize );
92 static const ILockBytesVtbl HGLOBALLockBytesImpl_Vtbl;
94 /******************************************************************************
95 * CreateILockBytesOnHGlobal [OLE32.@]
97 * Create a byte array object which is intended to be the compound file foundation.
98 * This object supports a COM implementation of the ILockBytes interface.
101 * hGlobal [ I] Global memory handle
102 * fDeleteOnRelease [ I] Whether the handle should be freed when the object is released.
103 * ppLkbyt [ O] Address of ILockBytes pointer that receives
104 * the interface pointer to the new byte array object.
110 * The supplied ILockBytes pointer can be used by the StgCreateDocfileOnILockBytes
111 * function to build a compound file on top of this byte array object.
112 * The ILockBytes interface instance calls the GlobalReAlloc function to grow
113 * the memory block as required.
115 HRESULT WINAPI CreateILockBytesOnHGlobal(HGLOBAL hGlobal,
116 BOOL fDeleteOnRelease,
117 LPLOCKBYTES* ppLkbyt)
119 HGLOBALLockBytesImpl* newLockBytes;
121 newLockBytes = HGLOBALLockBytesImpl_Construct(hGlobal, fDeleteOnRelease);
123 if (newLockBytes != NULL)
125 return IUnknown_QueryInterface((IUnknown*)newLockBytes,
130 return E_OUTOFMEMORY;
133 /******************************************************************************
134 * GetHGlobalFromILockBytes [OLE32.@]
136 * Retrieve a global memory handle to a byte array object created
137 * using the CreateILockBytesOnHGlobal function.
140 * plkbyt [ I] Pointer to the ILockBytes interface on byte array object
141 * phglobal [ O] Address to store a global memory handle
143 * S_OK if *phglobal has a correct value
144 * E_INVALIDARG if any parameters are invalid
147 HRESULT WINAPI GetHGlobalFromILockBytes(ILockBytes* plkbyt, HGLOBAL* phglobal)
149 HGLOBALLockBytesImpl* const pMemLockBytes = (HGLOBALLockBytesImpl*)plkbyt;
152 ULARGE_INTEGER start;
156 if (pMemLockBytes->lpVtbl == &HGLOBALLockBytesImpl_Vtbl) {
157 *phglobal = pMemLockBytes->supportHandle;
162 /* It is not our lockbytes implementation, so use a more generic way */
163 hres = ILockBytes_Stat(plkbyt,&stbuf,0);
165 ERR("Cannot ILockBytes_Stat, %lx\n",hres);
168 FIXME("cbSize is %ld\n",stbuf.cbSize.u.LowPart);
169 *phglobal = GlobalAlloc( GMEM_MOVEABLE|GMEM_SHARE, stbuf.cbSize.u.LowPart);
172 memset(&start,0,sizeof(start));
173 hres = ILockBytes_ReadAt(plkbyt, start, GlobalLock(*phglobal), stbuf.cbSize.u.LowPart, &xread);
174 GlobalUnlock(*phglobal);
176 FIXME("%p->ReadAt failed with %lx\n",plkbyt,hres);
179 if (stbuf.cbSize.u.LowPart != xread) {
180 FIXME("Read size is not requested size %ld vs %ld?\n",stbuf.cbSize.u.LowPart, xread);
185 /******************************************************************************
187 * HGLOBALLockBytesImpl implementation
191 /******************************************************************************
192 * This is the constructor for the HGLOBALLockBytesImpl class.
195 * hGlobal [ I] Handle that will support the stream. can be NULL.
196 * fDeleteOnRelease [ I] Flag set to TRUE if the HGLOBAL will be released
197 * when the IStream object is destroyed.
199 HGLOBALLockBytesImpl* HGLOBALLockBytesImpl_Construct(HGLOBAL hGlobal,
200 BOOL fDeleteOnRelease)
202 HGLOBALLockBytesImpl* newLockBytes;
203 newLockBytes = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALLockBytesImpl));
208 * Set up the virtual function table and reference count.
210 newLockBytes->lpVtbl = &HGLOBALLockBytesImpl_Vtbl;
211 newLockBytes->ref = 0;
214 * Initialize the support.
216 newLockBytes->supportHandle = hGlobal;
217 newLockBytes->deleteOnRelease = fDeleteOnRelease;
220 * This method will allocate a handle if one is not supplied.
222 if (newLockBytes->supportHandle == 0)
224 newLockBytes->supportHandle = GlobalAlloc(GMEM_MOVEABLE |
230 * Initialize the size of the array to the size of the handle.
232 newLockBytes->byteArraySize.u.HighPart = 0;
233 newLockBytes->byteArraySize.u.LowPart = GlobalSize(
234 newLockBytes->supportHandle);
240 /******************************************************************************
241 * This is the destructor of the HGLOBALStreamImpl class.
243 * This method will clean-up all the resources used-up by the given
244 * HGLOBALLockBytesImpl class. The pointer passed-in to this function will be
245 * freed and will not be valid anymore.
247 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This)
250 * Release the HGlobal if the constructor asked for that.
252 if (This->deleteOnRelease)
254 GlobalFree(This->supportHandle);
255 This->supportHandle = 0;
259 * Finally, free the memory used-up by the class.
261 HeapFree(GetProcessHeap(), 0, This);
264 /******************************************************************************
265 * This implements the IUnknown method QueryInterface for this
268 static HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(
270 REFIID riid, /* [in] */
271 void** ppvObject) /* [iid_is][out] */
273 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
276 * Perform a sanity check on the parameters.
282 * Initialize the return parameter.
287 * Compare the riid with the interface IDs implemented by this object.
289 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
291 *ppvObject = (ILockBytes*)This;
293 else if (memcmp(&IID_ILockBytes, riid, sizeof(IID_ILockBytes)) == 0)
295 *ppvObject = (ILockBytes*)This;
299 * Check that we obtained an interface.
302 return E_NOINTERFACE;
305 * Query Interface always increases the reference count by one when it is
308 IUnknown_AddRef(iface);
313 /******************************************************************************
314 * This implements the IUnknown method AddRef for this
317 static ULONG WINAPI HGLOBALLockBytesImpl_AddRef(ILockBytes* iface)
319 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
320 return InterlockedIncrement(&This->ref);
323 /******************************************************************************
324 * This implements the IUnknown method Release for this
327 static ULONG WINAPI HGLOBALLockBytesImpl_Release(ILockBytes* iface)
329 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
332 ref = InterlockedDecrement(&This->ref);
335 * If the reference count goes down to 0, perform suicide.
339 HGLOBALLockBytesImpl_Destroy(This);
345 /******************************************************************************
346 * This method is part of the ILockBytes interface.
348 * It reads a block of information from the byte array at the specified
351 * See the documentation of ILockBytes for more info.
353 static HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(
355 ULARGE_INTEGER ulOffset, /* [in] */
356 void* pv, /* [length_is][size_is][out] */
358 ULONG* pcbRead) /* [out] */
360 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
363 ULONG bytesReadBuffer = 0;
364 ULONG bytesToReadFromBuffer;
367 * If the caller is not interested in the number of bytes read,
368 * we use another buffer to avoid "if" statements in the code.
371 pcbRead = &bytesReadBuffer;
374 * Make sure the offset is valid.
376 if (ulOffset.u.LowPart > This->byteArraySize.u.LowPart)
380 * Using the known size of the array, calculate the number of bytes
383 bytesToReadFromBuffer = min(This->byteArraySize.u.LowPart -
384 ulOffset.u.LowPart, cb);
387 * Lock the buffer in position and copy the data.
389 supportBuffer = GlobalLock(This->supportHandle);
392 (char *) supportBuffer + ulOffset.u.LowPart,
393 bytesToReadFromBuffer);
396 * Return the number of bytes read.
398 *pcbRead = bytesToReadFromBuffer;
403 GlobalUnlock(This->supportHandle);
406 * The function returns S_OK if the specified number of bytes were read
407 * or the end of the array was reached.
408 * It returns STG_E_READFAULT if the number of bytes to read does not equal
409 * the number of bytes actually read.
414 return STG_E_READFAULT;
417 /******************************************************************************
418 * This method is part of the ILockBytes interface.
420 * It writes the specified bytes at the specified offset.
421 * position. If the array is too small, it will be resized.
423 * See the documentation of ILockBytes for more info.
425 static HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt(
427 ULARGE_INTEGER ulOffset, /* [in] */
428 const void* pv, /* [size_is][in] */
430 ULONG* pcbWritten) /* [out] */
432 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
435 ULARGE_INTEGER newSize;
436 ULONG bytesWritten = 0;
439 * If the caller is not interested in the number of bytes written,
440 * we use another buffer to avoid "if" statements in the code.
443 pcbWritten = &bytesWritten;
451 newSize.u.HighPart = 0;
452 newSize.u.LowPart = ulOffset.u.LowPart + cb;
456 * Verify if we need to grow the stream
458 if (newSize.u.LowPart > This->byteArraySize.u.LowPart)
461 if (HGLOBALLockBytesImpl_SetSize(iface, newSize) == STG_E_MEDIUMFULL)
462 return STG_E_MEDIUMFULL;
466 * Lock the buffer in position and copy the data.
468 supportBuffer = GlobalLock(This->supportHandle);
470 memcpy((char *) supportBuffer + ulOffset.u.LowPart, pv, cb);
473 * Return the number of bytes written.
480 GlobalUnlock(This->supportHandle);
485 /******************************************************************************
486 * This method is part of the ILockBytes interface.
488 * See the documentation of ILockBytes for more info.
490 static HRESULT WINAPI HGLOBALLockBytesImpl_Flush(ILockBytes* iface)
495 /******************************************************************************
496 * This method is part of the ILockBytes interface.
498 * It will change the size of the byte array.
500 * See the documentation of ILockBytes for more info.
502 static HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(
504 ULARGE_INTEGER libNewSize) /* [in] */
506 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
507 HGLOBAL supportHandle;
512 if (libNewSize.u.HighPart != 0)
513 return STG_E_INVALIDFUNCTION;
515 if (This->byteArraySize.u.LowPart == libNewSize.u.LowPart)
519 * Re allocate the HGlobal to fit the new size of the stream.
521 supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
523 if (supportHandle == 0)
524 return STG_E_MEDIUMFULL;
526 This->supportHandle = supportHandle;
527 This->byteArraySize.u.LowPart = libNewSize.u.LowPart;
532 /******************************************************************************
533 * This method is part of the ILockBytes interface.
535 * The global memory implementation of ILockBytes does not support locking.
537 * See the documentation of ILockBytes for more info.
539 static HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion(
541 ULARGE_INTEGER libOffset, /* [in] */
542 ULARGE_INTEGER cb, /* [in] */
543 DWORD dwLockType) /* [in] */
545 return STG_E_INVALIDFUNCTION;
548 /******************************************************************************
549 * This method is part of the ILockBytes interface.
551 * The global memory implementation of ILockBytes does not support locking.
553 * See the documentation of ILockBytes for more info.
555 static HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion(
557 ULARGE_INTEGER libOffset, /* [in] */
558 ULARGE_INTEGER cb, /* [in] */
559 DWORD dwLockType) /* [in] */
561 return STG_E_INVALIDFUNCTION;
564 /******************************************************************************
565 * This method is part of the ILockBytes interface.
567 * This method returns information about the current
570 * See the documentation of ILockBytes for more info.
572 static HRESULT WINAPI HGLOBALLockBytesImpl_Stat(
574 STATSTG* pstatstg, /* [out] */
575 DWORD grfStatFlag) /* [in] */
577 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
579 memset(pstatstg, 0, sizeof(STATSTG));
581 pstatstg->pwcsName = NULL;
582 pstatstg->type = STGTY_LOCKBYTES;
583 pstatstg->cbSize = This->byteArraySize;
589 * Virtual function table for the HGLOBALLockBytesImpl class.
591 static const ILockBytesVtbl HGLOBALLockBytesImpl_Vtbl =
593 HGLOBALLockBytesImpl_QueryInterface,
594 HGLOBALLockBytesImpl_AddRef,
595 HGLOBALLockBytesImpl_Release,
596 HGLOBALLockBytesImpl_ReadAt,
597 HGLOBALLockBytesImpl_WriteAt,
598 HGLOBALLockBytesImpl_Flush,
599 HGLOBALLockBytesImpl_SetSize,
600 HGLOBALLockBytesImpl_LockRegion,
601 HGLOBALLockBytesImpl_UnlockRegion,
602 HGLOBALLockBytesImpl_Stat,