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 implements the ILockBytes interface 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 static HGLOBALLockBytesImpl
* HGLOBALLockBytesImpl_Construct(
86 BOOL fDeleteOnRelease
);
88 static 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, %x\n",hres
);
168 FIXME("cbSize is %d\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 %x\n",plkbyt
,hres
);
179 if (stbuf
.cbSize
.u
.LowPart
!= xread
) {
180 FIXME("Read size is not requested size %d vs %d?\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 static 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 static 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 (IsEqualIID(riid
, &IID_IUnknown
) ||
290 IsEqualIID(riid
, &IID_ILockBytes
))
292 *ppvObject
= (ILockBytes
*)This
;
296 * Check that we obtained an interface.
299 return E_NOINTERFACE
;
302 * Query Interface always increases the reference count by one when it is
305 IUnknown_AddRef(iface
);
310 /******************************************************************************
311 * This implements the IUnknown method AddRef for this
314 static ULONG WINAPI
HGLOBALLockBytesImpl_AddRef(ILockBytes
* iface
)
316 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
317 return InterlockedIncrement(&This
->ref
);
320 /******************************************************************************
321 * This implements the IUnknown method Release for this
324 static ULONG WINAPI
HGLOBALLockBytesImpl_Release(ILockBytes
* iface
)
326 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
329 ref
= InterlockedDecrement(&This
->ref
);
332 * If the reference count goes down to 0, perform suicide.
336 HGLOBALLockBytesImpl_Destroy(This
);
342 /******************************************************************************
343 * This method is part of the ILockBytes interface.
345 * It reads a block of information from the byte array at the specified
348 * See the documentation of ILockBytes for more info.
350 static HRESULT WINAPI
HGLOBALLockBytesImpl_ReadAt(
352 ULARGE_INTEGER ulOffset
, /* [in] */
353 void* pv
, /* [length_is][size_is][out] */
355 ULONG
* pcbRead
) /* [out] */
357 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
360 ULONG bytesReadBuffer
= 0;
361 ULONG bytesToReadFromBuffer
;
364 * If the caller is not interested in the number of bytes read,
365 * we use another buffer to avoid "if" statements in the code.
368 pcbRead
= &bytesReadBuffer
;
371 * Make sure the offset is valid.
373 if (ulOffset
.u
.LowPart
> This
->byteArraySize
.u
.LowPart
)
377 * Using the known size of the array, calculate the number of bytes
380 bytesToReadFromBuffer
= min(This
->byteArraySize
.u
.LowPart
-
381 ulOffset
.u
.LowPart
, cb
);
384 * Lock the buffer in position and copy the data.
386 supportBuffer
= GlobalLock(This
->supportHandle
);
389 (char *) supportBuffer
+ ulOffset
.u
.LowPart
,
390 bytesToReadFromBuffer
);
393 * Return the number of bytes read.
395 *pcbRead
= bytesToReadFromBuffer
;
400 GlobalUnlock(This
->supportHandle
);
403 * The function returns S_OK if the specified number of bytes were read
404 * or the end of the array was reached.
405 * It returns STG_E_READFAULT if the number of bytes to read does not equal
406 * the number of bytes actually read.
411 return STG_E_READFAULT
;
414 /******************************************************************************
415 * This method is part of the ILockBytes interface.
417 * It writes the specified bytes at the specified offset.
418 * position. If the array is too small, it will be resized.
420 * See the documentation of ILockBytes for more info.
422 static HRESULT WINAPI
HGLOBALLockBytesImpl_WriteAt(
424 ULARGE_INTEGER ulOffset
, /* [in] */
425 const void* pv
, /* [size_is][in] */
427 ULONG
* pcbWritten
) /* [out] */
429 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
432 ULARGE_INTEGER newSize
;
433 ULONG bytesWritten
= 0;
436 * If the caller is not interested in the number of bytes written,
437 * we use another buffer to avoid "if" statements in the code.
440 pcbWritten
= &bytesWritten
;
448 newSize
.u
.HighPart
= 0;
449 newSize
.u
.LowPart
= ulOffset
.u
.LowPart
+ cb
;
453 * Verify if we need to grow the stream
455 if (newSize
.u
.LowPart
> This
->byteArraySize
.u
.LowPart
)
458 if (HGLOBALLockBytesImpl_SetSize(iface
, newSize
) == STG_E_MEDIUMFULL
)
459 return STG_E_MEDIUMFULL
;
463 * Lock the buffer in position and copy the data.
465 supportBuffer
= GlobalLock(This
->supportHandle
);
467 memcpy((char *) supportBuffer
+ ulOffset
.u
.LowPart
, pv
, cb
);
470 * Return the number of bytes written.
477 GlobalUnlock(This
->supportHandle
);
482 /******************************************************************************
483 * This method is part of the ILockBytes interface.
485 * See the documentation of ILockBytes for more info.
487 static HRESULT WINAPI
HGLOBALLockBytesImpl_Flush(ILockBytes
* iface
)
492 /******************************************************************************
493 * This method is part of the ILockBytes interface.
495 * It will change the size of the byte array.
497 * See the documentation of ILockBytes for more info.
499 static HRESULT WINAPI
HGLOBALLockBytesImpl_SetSize(
501 ULARGE_INTEGER libNewSize
) /* [in] */
503 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
504 HGLOBAL supportHandle
;
509 if (libNewSize
.u
.HighPart
!= 0)
510 return STG_E_INVALIDFUNCTION
;
512 if (This
->byteArraySize
.u
.LowPart
== libNewSize
.u
.LowPart
)
516 * Re allocate the HGlobal to fit the new size of the stream.
518 supportHandle
= GlobalReAlloc(This
->supportHandle
, libNewSize
.u
.LowPart
, 0);
520 if (supportHandle
== 0)
521 return STG_E_MEDIUMFULL
;
523 This
->supportHandle
= supportHandle
;
524 This
->byteArraySize
.u
.LowPart
= libNewSize
.u
.LowPart
;
529 /******************************************************************************
530 * This method is part of the ILockBytes interface.
532 * The global memory implementation of ILockBytes does not support locking.
534 * See the documentation of ILockBytes for more info.
536 static HRESULT WINAPI
HGLOBALLockBytesImpl_LockRegion(
538 ULARGE_INTEGER libOffset
, /* [in] */
539 ULARGE_INTEGER cb
, /* [in] */
540 DWORD dwLockType
) /* [in] */
542 return STG_E_INVALIDFUNCTION
;
545 /******************************************************************************
546 * This method is part of the ILockBytes interface.
548 * The global memory implementation of ILockBytes does not support locking.
550 * See the documentation of ILockBytes for more info.
552 static HRESULT WINAPI
HGLOBALLockBytesImpl_UnlockRegion(
554 ULARGE_INTEGER libOffset
, /* [in] */
555 ULARGE_INTEGER cb
, /* [in] */
556 DWORD dwLockType
) /* [in] */
558 return STG_E_INVALIDFUNCTION
;
561 /******************************************************************************
562 * This method is part of the ILockBytes interface.
564 * This method returns information about the current
567 * See the documentation of ILockBytes for more info.
569 static HRESULT WINAPI
HGLOBALLockBytesImpl_Stat(
571 STATSTG
* pstatstg
, /* [out] */
572 DWORD grfStatFlag
) /* [in] */
574 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
576 memset(pstatstg
, 0, sizeof(STATSTG
));
578 pstatstg
->pwcsName
= NULL
;
579 pstatstg
->type
= STGTY_LOCKBYTES
;
580 pstatstg
->cbSize
= This
->byteArraySize
;
586 * Virtual function table for the HGLOBALLockBytesImpl class.
588 static const ILockBytesVtbl HGLOBALLockBytesImpl_Vtbl
=
590 HGLOBALLockBytesImpl_QueryInterface
,
591 HGLOBALLockBytesImpl_AddRef
,
592 HGLOBALLockBytesImpl_Release
,
593 HGLOBALLockBytesImpl_ReadAt
,
594 HGLOBALLockBytesImpl_WriteAt
,
595 HGLOBALLockBytesImpl_Flush
,
596 HGLOBALLockBytesImpl_SetSize
,
597 HGLOBALLockBytesImpl_LockRegion
,
598 HGLOBALLockBytesImpl_UnlockRegion
,
599 HGLOBALLockBytesImpl_Stat
,