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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
33 #include "wine/winbase16.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(ole);
44 /******************************************************************************
45 * HGLOBALLockBytesImpl definition.
47 * This class imlements the ILockBytes inteface and represents a byte array
48 * object supported by an HGLOBAL pointer.
50 struct HGLOBALLockBytesImpl
53 * Needs to be the first item in the stuct
54 * since we want to cast this in an ILockBytes pointer
56 ILockBytesVtbl *lpVtbl;
64 * Support for the LockBytes object
66 HGLOBAL supportHandle;
69 * This flag is TRUE if the HGLOBAL is destroyed when the object
70 * is finally released.
75 * Helper variable that contains the size of the byte array
77 ULARGE_INTEGER byteArraySize;
80 typedef struct HGLOBALLockBytesImpl HGLOBALLockBytesImpl;
83 * Method definition for the HGLOBALLockBytesImpl class.
85 HGLOBALLockBytesImpl* HGLOBALLockBytesImpl_Construct(
87 BOOL fDeleteOnRelease);
89 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This);
91 HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(
93 REFIID riid, /* [in] */
94 void** ppvObject); /* [iid_is][out] */
96 ULONG WINAPI HGLOBALLockBytesImpl_AddRef(
99 ULONG WINAPI HGLOBALLockBytesImpl_Release(
102 HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(
104 ULARGE_INTEGER ulOffset, /* [in] */
105 void* pv, /* [length_is][size_is][out] */
107 ULONG* pcbRead); /* [out] */
109 HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt(
111 ULARGE_INTEGER ulOffset, /* [in] */
112 const void* pv, /* [size_is][in] */
114 ULONG* pcbWritten); /* [out] */
116 HRESULT WINAPI HGLOBALLockBytesImpl_Flush(
119 HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(
121 ULARGE_INTEGER libNewSize); /* [in] */
123 HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion(
125 ULARGE_INTEGER libOffset, /* [in] */
126 ULARGE_INTEGER cb, /* [in] */
127 DWORD dwLockType); /* [in] */
129 HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion(
131 ULARGE_INTEGER libOffset, /* [in] */
132 ULARGE_INTEGER cb, /* [in] */
133 DWORD dwLockType); /* [in] */
135 HRESULT WINAPI HGLOBALLockBytesImpl_Stat(
137 STATSTG* pstatstg, /* [out] */
138 DWORD grfStatFlag); /* [in] */
141 * Virtual function table for the HGLOBALLockBytesImpl class.
143 static ILockBytesVtbl HGLOBALLockBytesImpl_Vtbl =
145 HGLOBALLockBytesImpl_QueryInterface,
146 HGLOBALLockBytesImpl_AddRef,
147 HGLOBALLockBytesImpl_Release,
148 HGLOBALLockBytesImpl_ReadAt,
149 HGLOBALLockBytesImpl_WriteAt,
150 HGLOBALLockBytesImpl_Flush,
151 HGLOBALLockBytesImpl_SetSize,
152 HGLOBALLockBytesImpl_LockRegion,
153 HGLOBALLockBytesImpl_UnlockRegion,
154 HGLOBALLockBytesImpl_Stat,
157 /******************************************************************************
158 * CreateILockBytesOnHGlobal [OLE32.@]
160 HRESULT WINAPI CreateILockBytesOnHGlobal(HGLOBAL hGlobal,
161 BOOL fDeleteOnRelease,
162 LPLOCKBYTES* ppLkbyt)
164 HGLOBALLockBytesImpl* newLockBytes;
166 newLockBytes = HGLOBALLockBytesImpl_Construct(hGlobal, fDeleteOnRelease);
168 if (newLockBytes != NULL)
170 return IUnknown_QueryInterface((IUnknown*)newLockBytes,
175 return E_OUTOFMEMORY;
178 /******************************************************************************
179 * GetHGlobalFromILockBytes [OLE32.@]
181 HRESULT WINAPI GetHGlobalFromILockBytes(ILockBytes* plkbyt, HGLOBAL* phglobal)
183 HGLOBALLockBytesImpl* const pMemLockBytes = (HGLOBALLockBytesImpl*)plkbyt;
186 ULARGE_INTEGER start;
190 if (pMemLockBytes->lpVtbl == &HGLOBALLockBytesImpl_Vtbl) {
191 *phglobal = pMemLockBytes->supportHandle;
196 /* It is not our lockbytes implementation, so use a more generic way */
197 hres = ILockBytes_Stat(plkbyt,&stbuf,0);
199 ERR("Cannot ILockBytes_Stat, %lx\n",hres);
202 FIXME("cbSize is %ld\n",stbuf.cbSize.u.LowPart);
203 *phglobal = GlobalAlloc( GMEM_MOVEABLE|GMEM_SHARE, stbuf.cbSize.u.LowPart);
206 memset(&start,0,sizeof(start));
207 hres = ILockBytes_ReadAt(plkbyt, start, GlobalLock(*phglobal), stbuf.cbSize.u.LowPart, &xread);
208 GlobalUnlock(*phglobal);
210 FIXME("%p->ReadAt failed with %lx\n",plkbyt,hres);
213 if (stbuf.cbSize.u.LowPart != xread) {
214 FIXME("Read size is not requested size %ld vs %ld?\n",stbuf.cbSize.u.LowPart, xread);
219 /******************************************************************************
221 * HGLOBALLockBytesImpl implementation
225 /******************************************************************************
226 * This is the constructor for the HGLOBALLockBytesImpl class.
229 * hGlobal - Handle that will support the stream. can be NULL.
230 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
231 * when the IStream object is destroyed.
233 HGLOBALLockBytesImpl* HGLOBALLockBytesImpl_Construct(HGLOBAL hGlobal,
234 BOOL fDeleteOnRelease)
236 HGLOBALLockBytesImpl* newLockBytes;
237 newLockBytes = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALLockBytesImpl));
242 * Set up the virtual function table and reference count.
244 newLockBytes->lpVtbl = &HGLOBALLockBytesImpl_Vtbl;
245 newLockBytes->ref = 0;
248 * Initialize the support.
250 newLockBytes->supportHandle = hGlobal;
251 newLockBytes->deleteOnRelease = fDeleteOnRelease;
254 * This method will allocate a handle if one is not supplied.
256 if (newLockBytes->supportHandle == 0)
258 newLockBytes->supportHandle = GlobalAlloc(GMEM_MOVEABLE |
264 * Initialize the size of the array to the size of the handle.
266 newLockBytes->byteArraySize.u.HighPart = 0;
267 newLockBytes->byteArraySize.u.LowPart = GlobalSize(
268 newLockBytes->supportHandle);
274 /******************************************************************************
275 * This is the destructor of the HGLOBALStreamImpl class.
277 * This method will clean-up all the resources used-up by the given
278 * HGLOBALLockBytesImpl class. The pointer passed-in to this function will be
279 * freed and will not be valid anymore.
281 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This)
284 * Release the HGlobal if the constructor asked for that.
286 if (This->deleteOnRelease)
288 GlobalFree(This->supportHandle);
289 This->supportHandle = 0;
293 * Finally, free the memory used-up by the class.
295 HeapFree(GetProcessHeap(), 0, This);
298 /******************************************************************************
299 * This implements the IUnknown method QueryInterface for this
302 HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(
304 REFIID riid, /* [in] */
305 void** ppvObject) /* [iid_is][out] */
307 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
310 * Perform a sanity check on the parameters.
316 * Initialize the return parameter.
321 * Compare the riid with the interface IDs implemented by this object.
323 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
325 *ppvObject = (ILockBytes*)This;
327 else if (memcmp(&IID_ILockBytes, riid, sizeof(IID_ILockBytes)) == 0)
329 *ppvObject = (ILockBytes*)This;
333 * Check that we obtained an interface.
336 return E_NOINTERFACE;
339 * Query Interface always increases the reference count by one when it is
342 HGLOBALLockBytesImpl_AddRef(iface);
347 /******************************************************************************
348 * This implements the IUnknown method AddRef for this
351 ULONG WINAPI HGLOBALLockBytesImpl_AddRef(ILockBytes* iface)
353 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
360 /******************************************************************************
361 * This implements the IUnknown method Release for this
364 ULONG WINAPI HGLOBALLockBytesImpl_Release(ILockBytes* iface)
366 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
375 * If the reference count goes down to 0, perform suicide.
379 HGLOBALLockBytesImpl_Destroy(This);
385 /******************************************************************************
386 * This method is part of the ILockBytes interface.
388 * It reads a block of information from the byte array at the specified
391 * See the documentation of ILockBytes for more info.
393 HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(
395 ULARGE_INTEGER ulOffset, /* [in] */
396 void* pv, /* [length_is][size_is][out] */
398 ULONG* pcbRead) /* [out] */
400 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
403 ULONG bytesReadBuffer = 0;
404 ULONG bytesToReadFromBuffer;
407 * If the caller is not interested in the number of bytes read,
408 * we use another buffer to avoid "if" statements in the code.
411 pcbRead = &bytesReadBuffer;
414 * Make sure the offset is valid.
416 if (ulOffset.u.LowPart > This->byteArraySize.u.LowPart)
420 * Using the known size of the array, calculate the number of bytes
423 bytesToReadFromBuffer = min(This->byteArraySize.u.LowPart -
424 ulOffset.u.LowPart, cb);
427 * Lock the buffer in position and copy the data.
429 supportBuffer = GlobalLock(This->supportHandle);
432 (char *) supportBuffer + ulOffset.u.LowPart,
433 bytesToReadFromBuffer);
436 * Return the number of bytes read.
438 *pcbRead = bytesToReadFromBuffer;
443 GlobalUnlock(This->supportHandle);
446 * The function returns S_OK if the specified number of bytes were read
447 * or the end of the array was reached.
448 * It returns STG_E_READFAULT if the number of bytes to read does not equal
449 * the number of bytes actually read.
454 return STG_E_READFAULT;
457 /******************************************************************************
458 * This method is part of the ILockBytes interface.
460 * It writes the specified bytes at the specified offset.
461 * position. If the array is too small, it will be resized.
463 * See the documentation of ILockBytes for more info.
465 HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt(
467 ULARGE_INTEGER ulOffset, /* [in] */
468 const void* pv, /* [size_is][in] */
470 ULONG* pcbWritten) /* [out] */
472 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
475 ULARGE_INTEGER newSize;
476 ULONG bytesWritten = 0;
479 * If the caller is not interested in the number of bytes written,
480 * we use another buffer to avoid "if" statements in the code.
483 pcbWritten = &bytesWritten;
491 newSize.u.HighPart = 0;
492 newSize.u.LowPart = ulOffset.u.LowPart + cb;
496 * Verify if we need to grow the stream
498 if (newSize.u.LowPart > This->byteArraySize.u.LowPart)
501 if (HGLOBALLockBytesImpl_SetSize(iface, newSize) == STG_E_MEDIUMFULL)
502 return STG_E_MEDIUMFULL;
506 * Lock the buffer in position and copy the data.
508 supportBuffer = GlobalLock(This->supportHandle);
510 memcpy((char *) supportBuffer + ulOffset.u.LowPart, pv, cb);
513 * Return the number of bytes written.
520 GlobalUnlock(This->supportHandle);
525 /******************************************************************************
526 * This method is part of the ILockBytes interface.
528 * See the documentation of ILockBytes for more info.
530 HRESULT WINAPI HGLOBALLockBytesImpl_Flush(ILockBytes* iface)
535 /******************************************************************************
536 * This method is part of the ILockBytes interface.
538 * It will change the size of the byte array.
540 * See the documentation of ILockBytes for more info.
542 HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(
544 ULARGE_INTEGER libNewSize) /* [in] */
546 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
547 HGLOBAL supportHandle;
552 if (libNewSize.u.HighPart != 0)
553 return STG_E_INVALIDFUNCTION;
555 if (This->byteArraySize.u.LowPart == libNewSize.u.LowPart)
559 * Re allocate the HGlobal to fit the new size of the stream.
561 supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
563 if (supportHandle == 0)
564 return STG_E_MEDIUMFULL;
566 This->supportHandle = supportHandle;
567 This->byteArraySize.u.LowPart = libNewSize.u.LowPart;
572 /******************************************************************************
573 * This method is part of the ILockBytes interface.
575 * The global memory implementation of ILockBytes does not support locking.
577 * See the documentation of ILockBytes for more info.
579 HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion(
581 ULARGE_INTEGER libOffset, /* [in] */
582 ULARGE_INTEGER cb, /* [in] */
583 DWORD dwLockType) /* [in] */
585 return STG_E_INVALIDFUNCTION;
588 /******************************************************************************
589 * This method is part of the ILockBytes interface.
591 * The global memory implementation of ILockBytes does not support locking.
593 * See the documentation of ILockBytes for more info.
595 HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion(
597 ULARGE_INTEGER libOffset, /* [in] */
598 ULARGE_INTEGER cb, /* [in] */
599 DWORD dwLockType) /* [in] */
601 return STG_E_INVALIDFUNCTION;
604 /******************************************************************************
605 * This method is part of the ILockBytes interface.
607 * This method returns information about the current
610 * See the documentation of ILockBytes for more info.
612 HRESULT WINAPI HGLOBALLockBytesImpl_Stat(
614 STATSTG* pstatstg, /* [out] */
615 DWORD grfStatFlag) /* [in] */
617 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
619 memset(pstatstg, 0, sizeof(STATSTG));
621 pstatstg->pwcsName = NULL;
622 pstatstg->type = STGTY_LOCKBYTES;
623 pstatstg->cbSize = This->byteArraySize;