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 ICOM_VFIELD(ILockBytes);
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 ICOM_VTABLE(ILockBytes) HGLOBALLockBytesImpl_Vtbl =
145 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
146 HGLOBALLockBytesImpl_QueryInterface,
147 HGLOBALLockBytesImpl_AddRef,
148 HGLOBALLockBytesImpl_Release,
149 HGLOBALLockBytesImpl_ReadAt,
150 HGLOBALLockBytesImpl_WriteAt,
151 HGLOBALLockBytesImpl_Flush,
152 HGLOBALLockBytesImpl_SetSize,
153 HGLOBALLockBytesImpl_LockRegion,
154 HGLOBALLockBytesImpl_UnlockRegion,
155 HGLOBALLockBytesImpl_Stat,
158 /******************************************************************************
159 * CreateILockBytesOnHGlobal [OLE32.@]
161 HRESULT WINAPI CreateILockBytesOnHGlobal(HGLOBAL hGlobal,
162 BOOL fDeleteOnRelease,
163 LPLOCKBYTES* ppLkbyt)
165 HGLOBALLockBytesImpl* newLockBytes;
167 newLockBytes = HGLOBALLockBytesImpl_Construct(hGlobal, fDeleteOnRelease);
169 if (newLockBytes != NULL)
171 return IUnknown_QueryInterface((IUnknown*)newLockBytes,
176 return E_OUTOFMEMORY;
179 /******************************************************************************
180 * GetHGlobalFromILockBytes [OLE32.@]
182 HRESULT WINAPI GetHGlobalFromILockBytes(ILockBytes* plkbyt, HGLOBAL* phglobal)
184 HGLOBALLockBytesImpl* const pMemLockBytes = (HGLOBALLockBytesImpl*)plkbyt;
187 ULARGE_INTEGER start;
191 if (pMemLockBytes->lpVtbl == &HGLOBALLockBytesImpl_Vtbl) {
192 *phglobal = pMemLockBytes->supportHandle;
197 /* It is not our lockbytes implementation, so use a more generic way */
198 hres = ILockBytes_Stat(plkbyt,&stbuf,0);
200 ERR("Cannot ILockBytes_Stat, %lx\n",hres);
203 FIXME("cbSize is %ld\n",stbuf.cbSize.u.LowPart);
204 *phglobal = GlobalAlloc( GMEM_MOVEABLE|GMEM_SHARE, stbuf.cbSize.u.LowPart);
207 memset(&start,0,sizeof(start));
208 hres = ILockBytes_ReadAt(plkbyt, start, GlobalLock(*phglobal), stbuf.cbSize.u.LowPart, &xread);
209 GlobalUnlock(*phglobal);
211 FIXME("%p->ReadAt failed with %lx\n",plkbyt,hres);
214 if (stbuf.cbSize.u.LowPart != xread) {
215 FIXME("Read size is not requested size %ld vs %ld?\n",stbuf.cbSize.u.LowPart, xread);
220 /******************************************************************************
222 * HGLOBALLockBytesImpl implementation
226 /******************************************************************************
227 * This is the constructor for the HGLOBALLockBytesImpl class.
230 * hGlobal - Handle that will support the stream. can be NULL.
231 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
232 * when the IStream object is destroyed.
234 HGLOBALLockBytesImpl* HGLOBALLockBytesImpl_Construct(HGLOBAL hGlobal,
235 BOOL fDeleteOnRelease)
237 HGLOBALLockBytesImpl* newLockBytes;
238 newLockBytes = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALLockBytesImpl));
243 * Set up the virtual function table and reference count.
245 newLockBytes->lpVtbl = &HGLOBALLockBytesImpl_Vtbl;
246 newLockBytes->ref = 0;
249 * Initialize the support.
251 newLockBytes->supportHandle = hGlobal;
252 newLockBytes->deleteOnRelease = fDeleteOnRelease;
255 * This method will allocate a handle if one is not supplied.
257 if (newLockBytes->supportHandle == 0)
259 newLockBytes->supportHandle = GlobalAlloc(GMEM_MOVEABLE |
265 * Initialize the size of the array to the size of the handle.
267 newLockBytes->byteArraySize.u.HighPart = 0;
268 newLockBytes->byteArraySize.u.LowPart = GlobalSize(
269 newLockBytes->supportHandle);
275 /******************************************************************************
276 * This is the destructor of the HGLOBALStreamImpl class.
278 * This method will clean-up all the resources used-up by the given
279 * HGLOBALLockBytesImpl class. The pointer passed-in to this function will be
280 * freed and will not be valid anymore.
282 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This)
285 * Release the HGlobal if the constructor asked for that.
287 if (This->deleteOnRelease)
289 GlobalFree(This->supportHandle);
290 This->supportHandle = 0;
294 * Finally, free the memory used-up by the class.
296 HeapFree(GetProcessHeap(), 0, This);
299 /******************************************************************************
300 * This implements the IUnknown method QueryInterface for this
303 HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(
305 REFIID riid, /* [in] */
306 void** ppvObject) /* [iid_is][out] */
308 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
311 * Perform a sanity check on the parameters.
317 * Initialize the return parameter.
322 * Compare the riid with the interface IDs implemented by this object.
324 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
326 *ppvObject = (ILockBytes*)This;
328 else if (memcmp(&IID_ILockBytes, riid, sizeof(IID_ILockBytes)) == 0)
330 *ppvObject = (ILockBytes*)This;
334 * Check that we obtained an interface.
337 return E_NOINTERFACE;
340 * Query Interface always increases the reference count by one when it is
343 HGLOBALLockBytesImpl_AddRef(iface);
348 /******************************************************************************
349 * This implements the IUnknown method AddRef for this
352 ULONG WINAPI HGLOBALLockBytesImpl_AddRef(ILockBytes* iface)
354 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
361 /******************************************************************************
362 * This implements the IUnknown method Release for this
365 ULONG WINAPI HGLOBALLockBytesImpl_Release(ILockBytes* iface)
367 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
376 * If the reference count goes down to 0, perform suicide.
380 HGLOBALLockBytesImpl_Destroy(This);
386 /******************************************************************************
387 * This method is part of the ILockBytes interface.
389 * It reads a block of information from the byte array at the specified
392 * See the documentation of ILockBytes for more info.
394 HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(
396 ULARGE_INTEGER ulOffset, /* [in] */
397 void* pv, /* [length_is][size_is][out] */
399 ULONG* pcbRead) /* [out] */
401 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
404 ULONG bytesReadBuffer = 0;
405 ULONG bytesToReadFromBuffer;
408 * If the caller is not interested in the number of bytes read,
409 * we use another buffer to avoid "if" statements in the code.
412 pcbRead = &bytesReadBuffer;
415 * Make sure the offset is valid.
417 if (ulOffset.u.LowPart > This->byteArraySize.u.LowPart)
421 * Using the known size of the array, calculate the number of bytes
424 bytesToReadFromBuffer = min(This->byteArraySize.u.LowPart -
425 ulOffset.u.LowPart, cb);
428 * Lock the buffer in position and copy the data.
430 supportBuffer = GlobalLock(This->supportHandle);
433 (char *) supportBuffer + ulOffset.u.LowPart,
434 bytesToReadFromBuffer);
437 * Return the number of bytes read.
439 *pcbRead = bytesToReadFromBuffer;
444 GlobalUnlock(This->supportHandle);
447 * The function returns S_OK if the specified number of bytes were read
448 * or the end of the array was reached.
449 * It returns STG_E_READFAULT if the number of bytes to read does not equal
450 * the number of bytes actually read.
455 return STG_E_READFAULT;
458 /******************************************************************************
459 * This method is part of the ILockBytes interface.
461 * It writes the specified bytes at the specified offset.
462 * position. If the array is too small, it will be resized.
464 * See the documentation of ILockBytes for more info.
466 HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt(
468 ULARGE_INTEGER ulOffset, /* [in] */
469 const void* pv, /* [size_is][in] */
471 ULONG* pcbWritten) /* [out] */
473 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
476 ULARGE_INTEGER newSize;
477 ULONG bytesWritten = 0;
480 * If the caller is not interested in the number of bytes written,
481 * we use another buffer to avoid "if" statements in the code.
484 pcbWritten = &bytesWritten;
492 newSize.u.HighPart = 0;
493 newSize.u.LowPart = ulOffset.u.LowPart + cb;
497 * Verify if we need to grow the stream
499 if (newSize.u.LowPart > This->byteArraySize.u.LowPart)
502 if (HGLOBALLockBytesImpl_SetSize(iface, newSize) == STG_E_MEDIUMFULL)
503 return STG_E_MEDIUMFULL;
507 * Lock the buffer in position and copy the data.
509 supportBuffer = GlobalLock(This->supportHandle);
511 memcpy((char *) supportBuffer + ulOffset.u.LowPart, pv, cb);
514 * Return the number of bytes written.
521 GlobalUnlock(This->supportHandle);
526 /******************************************************************************
527 * This method is part of the ILockBytes interface.
529 * See the documentation of ILockBytes for more info.
531 HRESULT WINAPI HGLOBALLockBytesImpl_Flush(ILockBytes* iface)
536 /******************************************************************************
537 * This method is part of the ILockBytes interface.
539 * It will change the size of the byte array.
541 * See the documentation of ILockBytes for more info.
543 HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(
545 ULARGE_INTEGER libNewSize) /* [in] */
547 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
548 HGLOBAL supportHandle;
553 if (libNewSize.u.HighPart != 0)
554 return STG_E_INVALIDFUNCTION;
556 if (This->byteArraySize.u.LowPart == libNewSize.u.LowPart)
560 * Re allocate the HGlobal to fit the new size of the stream.
562 supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
564 if (supportHandle == 0)
565 return STG_E_MEDIUMFULL;
567 This->supportHandle = supportHandle;
568 This->byteArraySize.u.LowPart = libNewSize.u.LowPart;
573 /******************************************************************************
574 * This method is part of the ILockBytes interface.
576 * The global memory implementation of ILockBytes does not support locking.
578 * See the documentation of ILockBytes for more info.
580 HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion(
582 ULARGE_INTEGER libOffset, /* [in] */
583 ULARGE_INTEGER cb, /* [in] */
584 DWORD dwLockType) /* [in] */
586 return STG_E_INVALIDFUNCTION;
589 /******************************************************************************
590 * This method is part of the ILockBytes interface.
592 * The global memory implementation of ILockBytes does not support locking.
594 * See the documentation of ILockBytes for more info.
596 HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion(
598 ULARGE_INTEGER libOffset, /* [in] */
599 ULARGE_INTEGER cb, /* [in] */
600 DWORD dwLockType) /* [in] */
602 return STG_E_INVALIDFUNCTION;
605 /******************************************************************************
606 * This method is part of the ILockBytes interface.
608 * This method returns information about the current
611 * See the documentation of ILockBytes for more info.
613 HRESULT WINAPI HGLOBALLockBytesImpl_Stat(
615 STATSTG* pstatstg, /* [out] */
616 DWORD grfStatFlag) /* [in] */
618 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
620 memset(pstatstg, 0, sizeof(STATSTG));
622 pstatstg->pwcsName = NULL;
623 pstatstg->type = STGTY_LOCKBYTES;
624 pstatstg->cbSize = This->byteArraySize;