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
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 HRESULT WINAPI
HGLOBALLockBytesImpl_QueryInterface(
92 REFIID riid
, /* [in] */
93 void** ppvObject
); /* [iid_is][out] */
95 ULONG WINAPI
HGLOBALLockBytesImpl_AddRef(
98 ULONG WINAPI
HGLOBALLockBytesImpl_Release(
101 HRESULT WINAPI
HGLOBALLockBytesImpl_ReadAt(
103 ULARGE_INTEGER ulOffset
, /* [in] */
104 void* pv
, /* [length_is][size_is][out] */
106 ULONG
* pcbRead
); /* [out] */
108 HRESULT WINAPI
HGLOBALLockBytesImpl_WriteAt(
110 ULARGE_INTEGER ulOffset
, /* [in] */
111 const void* pv
, /* [size_is][in] */
113 ULONG
* pcbWritten
); /* [out] */
115 HRESULT WINAPI
HGLOBALLockBytesImpl_Flush(
118 HRESULT WINAPI
HGLOBALLockBytesImpl_SetSize(
120 ULARGE_INTEGER libNewSize
); /* [in] */
122 HRESULT WINAPI
HGLOBALLockBytesImpl_LockRegion(
124 ULARGE_INTEGER libOffset
, /* [in] */
125 ULARGE_INTEGER cb
, /* [in] */
126 DWORD dwLockType
); /* [in] */
128 HRESULT WINAPI
HGLOBALLockBytesImpl_UnlockRegion(
130 ULARGE_INTEGER libOffset
, /* [in] */
131 ULARGE_INTEGER cb
, /* [in] */
132 DWORD dwLockType
); /* [in] */
134 HRESULT WINAPI
HGLOBALLockBytesImpl_Stat(
136 STATSTG
* pstatstg
, /* [out] */
137 DWORD grfStatFlag
); /* [in] */
140 * Virtual function table for the HGLOBALLockBytesImpl class.
142 static const ILockBytesVtbl HGLOBALLockBytesImpl_Vtbl
=
144 HGLOBALLockBytesImpl_QueryInterface
,
145 HGLOBALLockBytesImpl_AddRef
,
146 HGLOBALLockBytesImpl_Release
,
147 HGLOBALLockBytesImpl_ReadAt
,
148 HGLOBALLockBytesImpl_WriteAt
,
149 HGLOBALLockBytesImpl_Flush
,
150 HGLOBALLockBytesImpl_SetSize
,
151 HGLOBALLockBytesImpl_LockRegion
,
152 HGLOBALLockBytesImpl_UnlockRegion
,
153 HGLOBALLockBytesImpl_Stat
,
156 /******************************************************************************
157 * CreateILockBytesOnHGlobal [OLE32.@]
159 * Create a byte array object which is intended to be the compound file foundation.
160 * This object supports a COM implementation of the ILockBytes interface.
163 * hGlobal [ I] Global memory handle
164 * fDeleteOnRelease [ I] Whether the handle should be freed when the object is released.
165 * ppLkbyt [ O] Address of ILockBytes pointer that receives
166 * the interface pointer to the new byte array object.
172 * The supplied ILockBytes pointer can be used by the StgCreateDocfileOnILockBytes
173 * function to build a compound file on top of this byte array object.
174 * The ILockBytes interface instance calls the GlobalReAlloc function to grow
175 * the memory block as required.
177 HRESULT WINAPI
CreateILockBytesOnHGlobal(HGLOBAL hGlobal
,
178 BOOL fDeleteOnRelease
,
179 LPLOCKBYTES
* ppLkbyt
)
181 HGLOBALLockBytesImpl
* newLockBytes
;
183 newLockBytes
= HGLOBALLockBytesImpl_Construct(hGlobal
, fDeleteOnRelease
);
185 if (newLockBytes
!= NULL
)
187 return IUnknown_QueryInterface((IUnknown
*)newLockBytes
,
192 return E_OUTOFMEMORY
;
195 /******************************************************************************
196 * GetHGlobalFromILockBytes [OLE32.@]
198 * Retrieve a global memory handle to a byte array object created
199 * using the CreateILockBytesOnHGlobal function.
202 * plkbyt [ I] Pointer to the ILockBytes interface on byte array object
203 * phglobal [ O] Address to store a global memory handle
205 * S_OK if *phglobal has a correct value
206 * E_INVALIDARG if any parameters are invalid
209 HRESULT WINAPI
GetHGlobalFromILockBytes(ILockBytes
* plkbyt
, HGLOBAL
* phglobal
)
211 HGLOBALLockBytesImpl
* const pMemLockBytes
= (HGLOBALLockBytesImpl
*)plkbyt
;
214 ULARGE_INTEGER start
;
218 if (pMemLockBytes
->lpVtbl
== &HGLOBALLockBytesImpl_Vtbl
) {
219 *phglobal
= pMemLockBytes
->supportHandle
;
224 /* It is not our lockbytes implementation, so use a more generic way */
225 hres
= ILockBytes_Stat(plkbyt
,&stbuf
,0);
227 ERR("Cannot ILockBytes_Stat, %lx\n",hres
);
230 FIXME("cbSize is %ld\n",stbuf
.cbSize
.u
.LowPart
);
231 *phglobal
= GlobalAlloc( GMEM_MOVEABLE
|GMEM_SHARE
, stbuf
.cbSize
.u
.LowPart
);
234 memset(&start
,0,sizeof(start
));
235 hres
= ILockBytes_ReadAt(plkbyt
, start
, GlobalLock(*phglobal
), stbuf
.cbSize
.u
.LowPart
, &xread
);
236 GlobalUnlock(*phglobal
);
238 FIXME("%p->ReadAt failed with %lx\n",plkbyt
,hres
);
241 if (stbuf
.cbSize
.u
.LowPart
!= xread
) {
242 FIXME("Read size is not requested size %ld vs %ld?\n",stbuf
.cbSize
.u
.LowPart
, xread
);
247 /******************************************************************************
249 * HGLOBALLockBytesImpl implementation
253 /******************************************************************************
254 * This is the constructor for the HGLOBALLockBytesImpl class.
257 * hGlobal [ I] Handle that will support the stream. can be NULL.
258 * fDeleteOnRelease [ I] Flag set to TRUE if the HGLOBAL will be released
259 * when the IStream object is destroyed.
261 HGLOBALLockBytesImpl
* HGLOBALLockBytesImpl_Construct(HGLOBAL hGlobal
,
262 BOOL fDeleteOnRelease
)
264 HGLOBALLockBytesImpl
* newLockBytes
;
265 newLockBytes
= HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALLockBytesImpl
));
270 * Set up the virtual function table and reference count.
272 newLockBytes
->lpVtbl
= &HGLOBALLockBytesImpl_Vtbl
;
273 newLockBytes
->ref
= 0;
276 * Initialize the support.
278 newLockBytes
->supportHandle
= hGlobal
;
279 newLockBytes
->deleteOnRelease
= fDeleteOnRelease
;
282 * This method will allocate a handle if one is not supplied.
284 if (newLockBytes
->supportHandle
== 0)
286 newLockBytes
->supportHandle
= GlobalAlloc(GMEM_MOVEABLE
|
292 * Initialize the size of the array to the size of the handle.
294 newLockBytes
->byteArraySize
.u
.HighPart
= 0;
295 newLockBytes
->byteArraySize
.u
.LowPart
= GlobalSize(
296 newLockBytes
->supportHandle
);
302 /******************************************************************************
303 * This is the destructor of the HGLOBALStreamImpl class.
305 * This method will clean-up all the resources used-up by the given
306 * HGLOBALLockBytesImpl class. The pointer passed-in to this function will be
307 * freed and will not be valid anymore.
309 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl
* This
)
312 * Release the HGlobal if the constructor asked for that.
314 if (This
->deleteOnRelease
)
316 GlobalFree(This
->supportHandle
);
317 This
->supportHandle
= 0;
321 * Finally, free the memory used-up by the class.
323 HeapFree(GetProcessHeap(), 0, This
);
326 /******************************************************************************
327 * This implements the IUnknown method QueryInterface for this
330 HRESULT WINAPI
HGLOBALLockBytesImpl_QueryInterface(
332 REFIID riid
, /* [in] */
333 void** ppvObject
) /* [iid_is][out] */
335 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
338 * Perform a sanity check on the parameters.
344 * Initialize the return parameter.
349 * Compare the riid with the interface IDs implemented by this object.
351 if (memcmp(&IID_IUnknown
, riid
, sizeof(IID_IUnknown
)) == 0)
353 *ppvObject
= (ILockBytes
*)This
;
355 else if (memcmp(&IID_ILockBytes
, riid
, sizeof(IID_ILockBytes
)) == 0)
357 *ppvObject
= (ILockBytes
*)This
;
361 * Check that we obtained an interface.
364 return E_NOINTERFACE
;
367 * Query Interface always increases the reference count by one when it is
370 HGLOBALLockBytesImpl_AddRef(iface
);
375 /******************************************************************************
376 * This implements the IUnknown method AddRef for this
379 ULONG WINAPI
HGLOBALLockBytesImpl_AddRef(ILockBytes
* iface
)
381 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
382 return InterlockedIncrement(&This
->ref
);
385 /******************************************************************************
386 * This implements the IUnknown method Release for this
389 ULONG WINAPI
HGLOBALLockBytesImpl_Release(ILockBytes
* iface
)
391 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
394 ref
= InterlockedDecrement(&This
->ref
);
397 * If the reference count goes down to 0, perform suicide.
401 HGLOBALLockBytesImpl_Destroy(This
);
407 /******************************************************************************
408 * This method is part of the ILockBytes interface.
410 * It reads a block of information from the byte array at the specified
413 * See the documentation of ILockBytes for more info.
415 HRESULT WINAPI
HGLOBALLockBytesImpl_ReadAt(
417 ULARGE_INTEGER ulOffset
, /* [in] */
418 void* pv
, /* [length_is][size_is][out] */
420 ULONG
* pcbRead
) /* [out] */
422 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
425 ULONG bytesReadBuffer
= 0;
426 ULONG bytesToReadFromBuffer
;
429 * If the caller is not interested in the number of bytes read,
430 * we use another buffer to avoid "if" statements in the code.
433 pcbRead
= &bytesReadBuffer
;
436 * Make sure the offset is valid.
438 if (ulOffset
.u
.LowPart
> This
->byteArraySize
.u
.LowPart
)
442 * Using the known size of the array, calculate the number of bytes
445 bytesToReadFromBuffer
= min(This
->byteArraySize
.u
.LowPart
-
446 ulOffset
.u
.LowPart
, cb
);
449 * Lock the buffer in position and copy the data.
451 supportBuffer
= GlobalLock(This
->supportHandle
);
454 (char *) supportBuffer
+ ulOffset
.u
.LowPart
,
455 bytesToReadFromBuffer
);
458 * Return the number of bytes read.
460 *pcbRead
= bytesToReadFromBuffer
;
465 GlobalUnlock(This
->supportHandle
);
468 * The function returns S_OK if the specified number of bytes were read
469 * or the end of the array was reached.
470 * It returns STG_E_READFAULT if the number of bytes to read does not equal
471 * the number of bytes actually read.
476 return STG_E_READFAULT
;
479 /******************************************************************************
480 * This method is part of the ILockBytes interface.
482 * It writes the specified bytes at the specified offset.
483 * position. If the array is too small, it will be resized.
485 * See the documentation of ILockBytes for more info.
487 HRESULT WINAPI
HGLOBALLockBytesImpl_WriteAt(
489 ULARGE_INTEGER ulOffset
, /* [in] */
490 const void* pv
, /* [size_is][in] */
492 ULONG
* pcbWritten
) /* [out] */
494 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
497 ULARGE_INTEGER newSize
;
498 ULONG bytesWritten
= 0;
501 * If the caller is not interested in the number of bytes written,
502 * we use another buffer to avoid "if" statements in the code.
505 pcbWritten
= &bytesWritten
;
513 newSize
.u
.HighPart
= 0;
514 newSize
.u
.LowPart
= ulOffset
.u
.LowPart
+ cb
;
518 * Verify if we need to grow the stream
520 if (newSize
.u
.LowPart
> This
->byteArraySize
.u
.LowPart
)
523 if (HGLOBALLockBytesImpl_SetSize(iface
, newSize
) == STG_E_MEDIUMFULL
)
524 return STG_E_MEDIUMFULL
;
528 * Lock the buffer in position and copy the data.
530 supportBuffer
= GlobalLock(This
->supportHandle
);
532 memcpy((char *) supportBuffer
+ ulOffset
.u
.LowPart
, pv
, cb
);
535 * Return the number of bytes written.
542 GlobalUnlock(This
->supportHandle
);
547 /******************************************************************************
548 * This method is part of the ILockBytes interface.
550 * See the documentation of ILockBytes for more info.
552 HRESULT WINAPI
HGLOBALLockBytesImpl_Flush(ILockBytes
* iface
)
557 /******************************************************************************
558 * This method is part of the ILockBytes interface.
560 * It will change the size of the byte array.
562 * See the documentation of ILockBytes for more info.
564 HRESULT WINAPI
HGLOBALLockBytesImpl_SetSize(
566 ULARGE_INTEGER libNewSize
) /* [in] */
568 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
569 HGLOBAL supportHandle
;
574 if (libNewSize
.u
.HighPart
!= 0)
575 return STG_E_INVALIDFUNCTION
;
577 if (This
->byteArraySize
.u
.LowPart
== libNewSize
.u
.LowPart
)
581 * Re allocate the HGlobal to fit the new size of the stream.
583 supportHandle
= GlobalReAlloc(This
->supportHandle
, libNewSize
.u
.LowPart
, 0);
585 if (supportHandle
== 0)
586 return STG_E_MEDIUMFULL
;
588 This
->supportHandle
= supportHandle
;
589 This
->byteArraySize
.u
.LowPart
= libNewSize
.u
.LowPart
;
594 /******************************************************************************
595 * This method is part of the ILockBytes interface.
597 * The global memory implementation of ILockBytes does not support locking.
599 * See the documentation of ILockBytes for more info.
601 HRESULT WINAPI
HGLOBALLockBytesImpl_LockRegion(
603 ULARGE_INTEGER libOffset
, /* [in] */
604 ULARGE_INTEGER cb
, /* [in] */
605 DWORD dwLockType
) /* [in] */
607 return STG_E_INVALIDFUNCTION
;
610 /******************************************************************************
611 * This method is part of the ILockBytes interface.
613 * The global memory implementation of ILockBytes does not support locking.
615 * See the documentation of ILockBytes for more info.
617 HRESULT WINAPI
HGLOBALLockBytesImpl_UnlockRegion(
619 ULARGE_INTEGER libOffset
, /* [in] */
620 ULARGE_INTEGER cb
, /* [in] */
621 DWORD dwLockType
) /* [in] */
623 return STG_E_INVALIDFUNCTION
;
626 /******************************************************************************
627 * This method is part of the ILockBytes interface.
629 * This method returns information about the current
632 * See the documentation of ILockBytes for more info.
634 HRESULT WINAPI
HGLOBALLockBytesImpl_Stat(
636 STATSTG
* pstatstg
, /* [out] */
637 DWORD grfStatFlag
) /* [in] */
639 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
641 memset(pstatstg
, 0, sizeof(STATSTG
));
643 pstatstg
->pwcsName
= NULL
;
644 pstatstg
->type
= STGTY_LOCKBYTES
;
645 pstatstg
->cbSize
= This
->byteArraySize
;