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
35 #include "wine/winbase16.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
46 /******************************************************************************
47 * HGLOBALLockBytesImpl definition.
49 * This class imlements the ILockBytes inteface and represents a byte array
50 * object supported by an HGLOBAL pointer.
52 struct HGLOBALLockBytesImpl
55 * Needs to be the first item in the stuct
56 * since we want to cast this in an ILockBytes pointer
58 ILockBytesVtbl
*lpVtbl
;
66 * Support for the LockBytes object
68 HGLOBAL supportHandle
;
71 * This flag is TRUE if the HGLOBAL is destroyed when the object
72 * is finally released.
77 * Helper variable that contains the size of the byte array
79 ULARGE_INTEGER byteArraySize
;
82 typedef struct HGLOBALLockBytesImpl HGLOBALLockBytesImpl
;
85 * Method definition for the HGLOBALLockBytesImpl class.
87 HGLOBALLockBytesImpl
* HGLOBALLockBytesImpl_Construct(
89 BOOL fDeleteOnRelease
);
91 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl
* This
);
93 HRESULT WINAPI
HGLOBALLockBytesImpl_QueryInterface(
95 REFIID riid
, /* [in] */
96 void** ppvObject
); /* [iid_is][out] */
98 ULONG WINAPI
HGLOBALLockBytesImpl_AddRef(
101 ULONG WINAPI
HGLOBALLockBytesImpl_Release(
104 HRESULT WINAPI
HGLOBALLockBytesImpl_ReadAt(
106 ULARGE_INTEGER ulOffset
, /* [in] */
107 void* pv
, /* [length_is][size_is][out] */
109 ULONG
* pcbRead
); /* [out] */
111 HRESULT WINAPI
HGLOBALLockBytesImpl_WriteAt(
113 ULARGE_INTEGER ulOffset
, /* [in] */
114 const void* pv
, /* [size_is][in] */
116 ULONG
* pcbWritten
); /* [out] */
118 HRESULT WINAPI
HGLOBALLockBytesImpl_Flush(
121 HRESULT WINAPI
HGLOBALLockBytesImpl_SetSize(
123 ULARGE_INTEGER libNewSize
); /* [in] */
125 HRESULT WINAPI
HGLOBALLockBytesImpl_LockRegion(
127 ULARGE_INTEGER libOffset
, /* [in] */
128 ULARGE_INTEGER cb
, /* [in] */
129 DWORD dwLockType
); /* [in] */
131 HRESULT WINAPI
HGLOBALLockBytesImpl_UnlockRegion(
133 ULARGE_INTEGER libOffset
, /* [in] */
134 ULARGE_INTEGER cb
, /* [in] */
135 DWORD dwLockType
); /* [in] */
137 HRESULT WINAPI
HGLOBALLockBytesImpl_Stat(
139 STATSTG
* pstatstg
, /* [out] */
140 DWORD grfStatFlag
); /* [in] */
143 * Virtual function table for the HGLOBALLockBytesImpl class.
145 static ILockBytesVtbl HGLOBALLockBytesImpl_Vtbl
=
147 HGLOBALLockBytesImpl_QueryInterface
,
148 HGLOBALLockBytesImpl_AddRef
,
149 HGLOBALLockBytesImpl_Release
,
150 HGLOBALLockBytesImpl_ReadAt
,
151 HGLOBALLockBytesImpl_WriteAt
,
152 HGLOBALLockBytesImpl_Flush
,
153 HGLOBALLockBytesImpl_SetSize
,
154 HGLOBALLockBytesImpl_LockRegion
,
155 HGLOBALLockBytesImpl_UnlockRegion
,
156 HGLOBALLockBytesImpl_Stat
,
159 /******************************************************************************
160 * CreateILockBytesOnHGlobal [OLE32.@]
162 HRESULT WINAPI
CreateILockBytesOnHGlobal(HGLOBAL hGlobal
,
163 BOOL fDeleteOnRelease
,
164 LPLOCKBYTES
* ppLkbyt
)
166 HGLOBALLockBytesImpl
* newLockBytes
;
168 newLockBytes
= HGLOBALLockBytesImpl_Construct(hGlobal
, fDeleteOnRelease
);
170 if (newLockBytes
!= NULL
)
172 return IUnknown_QueryInterface((IUnknown
*)newLockBytes
,
177 return E_OUTOFMEMORY
;
180 /******************************************************************************
181 * GetHGlobalFromILockBytes [OLE32.@]
183 HRESULT WINAPI
GetHGlobalFromILockBytes(ILockBytes
* plkbyt
, HGLOBAL
* phglobal
)
185 HGLOBALLockBytesImpl
* const pMemLockBytes
= (HGLOBALLockBytesImpl
*)plkbyt
;
188 ULARGE_INTEGER start
;
192 if (pMemLockBytes
->lpVtbl
== &HGLOBALLockBytesImpl_Vtbl
) {
193 *phglobal
= pMemLockBytes
->supportHandle
;
198 /* It is not our lockbytes implementation, so use a more generic way */
199 hres
= ILockBytes_Stat(plkbyt
,&stbuf
,0);
201 ERR("Cannot ILockBytes_Stat, %lx\n",hres
);
204 FIXME("cbSize is %ld\n",stbuf
.cbSize
.u
.LowPart
);
205 *phglobal
= GlobalAlloc( GMEM_MOVEABLE
|GMEM_SHARE
, stbuf
.cbSize
.u
.LowPart
);
208 memset(&start
,0,sizeof(start
));
209 hres
= ILockBytes_ReadAt(plkbyt
, start
, GlobalLock(*phglobal
), stbuf
.cbSize
.u
.LowPart
, &xread
);
210 GlobalUnlock(*phglobal
);
212 FIXME("%p->ReadAt failed with %lx\n",plkbyt
,hres
);
215 if (stbuf
.cbSize
.u
.LowPart
!= xread
) {
216 FIXME("Read size is not requested size %ld vs %ld?\n",stbuf
.cbSize
.u
.LowPart
, xread
);
221 /******************************************************************************
223 * HGLOBALLockBytesImpl implementation
227 /******************************************************************************
228 * This is the constructor for the HGLOBALLockBytesImpl class.
231 * hGlobal - Handle that will support the stream. can be NULL.
232 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
233 * when the IStream object is destroyed.
235 HGLOBALLockBytesImpl
* HGLOBALLockBytesImpl_Construct(HGLOBAL hGlobal
,
236 BOOL fDeleteOnRelease
)
238 HGLOBALLockBytesImpl
* newLockBytes
;
239 newLockBytes
= HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALLockBytesImpl
));
244 * Set up the virtual function table and reference count.
246 newLockBytes
->lpVtbl
= &HGLOBALLockBytesImpl_Vtbl
;
247 newLockBytes
->ref
= 0;
250 * Initialize the support.
252 newLockBytes
->supportHandle
= hGlobal
;
253 newLockBytes
->deleteOnRelease
= fDeleteOnRelease
;
256 * This method will allocate a handle if one is not supplied.
258 if (newLockBytes
->supportHandle
== 0)
260 newLockBytes
->supportHandle
= GlobalAlloc(GMEM_MOVEABLE
|
266 * Initialize the size of the array to the size of the handle.
268 newLockBytes
->byteArraySize
.u
.HighPart
= 0;
269 newLockBytes
->byteArraySize
.u
.LowPart
= GlobalSize(
270 newLockBytes
->supportHandle
);
276 /******************************************************************************
277 * This is the destructor of the HGLOBALStreamImpl class.
279 * This method will clean-up all the resources used-up by the given
280 * HGLOBALLockBytesImpl class. The pointer passed-in to this function will be
281 * freed and will not be valid anymore.
283 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl
* This
)
286 * Release the HGlobal if the constructor asked for that.
288 if (This
->deleteOnRelease
)
290 GlobalFree(This
->supportHandle
);
291 This
->supportHandle
= 0;
295 * Finally, free the memory used-up by the class.
297 HeapFree(GetProcessHeap(), 0, This
);
300 /******************************************************************************
301 * This implements the IUnknown method QueryInterface for this
304 HRESULT WINAPI
HGLOBALLockBytesImpl_QueryInterface(
306 REFIID riid
, /* [in] */
307 void** ppvObject
) /* [iid_is][out] */
309 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
312 * Perform a sanity check on the parameters.
318 * Initialize the return parameter.
323 * Compare the riid with the interface IDs implemented by this object.
325 if (memcmp(&IID_IUnknown
, riid
, sizeof(IID_IUnknown
)) == 0)
327 *ppvObject
= (ILockBytes
*)This
;
329 else if (memcmp(&IID_ILockBytes
, riid
, sizeof(IID_ILockBytes
)) == 0)
331 *ppvObject
= (ILockBytes
*)This
;
335 * Check that we obtained an interface.
338 return E_NOINTERFACE
;
341 * Query Interface always increases the reference count by one when it is
344 HGLOBALLockBytesImpl_AddRef(iface
);
349 /******************************************************************************
350 * This implements the IUnknown method AddRef for this
353 ULONG WINAPI
HGLOBALLockBytesImpl_AddRef(ILockBytes
* iface
)
355 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
356 return InterlockedIncrement(&This
->ref
);
359 /******************************************************************************
360 * This implements the IUnknown method Release for this
363 ULONG WINAPI
HGLOBALLockBytesImpl_Release(ILockBytes
* iface
)
365 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
368 ref
= InterlockedDecrement(&This
->ref
);
371 * If the reference count goes down to 0, perform suicide.
375 HGLOBALLockBytesImpl_Destroy(This
);
381 /******************************************************************************
382 * This method is part of the ILockBytes interface.
384 * It reads a block of information from the byte array at the specified
387 * See the documentation of ILockBytes for more info.
389 HRESULT WINAPI
HGLOBALLockBytesImpl_ReadAt(
391 ULARGE_INTEGER ulOffset
, /* [in] */
392 void* pv
, /* [length_is][size_is][out] */
394 ULONG
* pcbRead
) /* [out] */
396 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
399 ULONG bytesReadBuffer
= 0;
400 ULONG bytesToReadFromBuffer
;
403 * If the caller is not interested in the number of bytes read,
404 * we use another buffer to avoid "if" statements in the code.
407 pcbRead
= &bytesReadBuffer
;
410 * Make sure the offset is valid.
412 if (ulOffset
.u
.LowPart
> This
->byteArraySize
.u
.LowPart
)
416 * Using the known size of the array, calculate the number of bytes
419 bytesToReadFromBuffer
= min(This
->byteArraySize
.u
.LowPart
-
420 ulOffset
.u
.LowPart
, cb
);
423 * Lock the buffer in position and copy the data.
425 supportBuffer
= GlobalLock(This
->supportHandle
);
428 (char *) supportBuffer
+ ulOffset
.u
.LowPart
,
429 bytesToReadFromBuffer
);
432 * Return the number of bytes read.
434 *pcbRead
= bytesToReadFromBuffer
;
439 GlobalUnlock(This
->supportHandle
);
442 * The function returns S_OK if the specified number of bytes were read
443 * or the end of the array was reached.
444 * It returns STG_E_READFAULT if the number of bytes to read does not equal
445 * the number of bytes actually read.
450 return STG_E_READFAULT
;
453 /******************************************************************************
454 * This method is part of the ILockBytes interface.
456 * It writes the specified bytes at the specified offset.
457 * position. If the array is too small, it will be resized.
459 * See the documentation of ILockBytes for more info.
461 HRESULT WINAPI
HGLOBALLockBytesImpl_WriteAt(
463 ULARGE_INTEGER ulOffset
, /* [in] */
464 const void* pv
, /* [size_is][in] */
466 ULONG
* pcbWritten
) /* [out] */
468 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
471 ULARGE_INTEGER newSize
;
472 ULONG bytesWritten
= 0;
475 * If the caller is not interested in the number of bytes written,
476 * we use another buffer to avoid "if" statements in the code.
479 pcbWritten
= &bytesWritten
;
487 newSize
.u
.HighPart
= 0;
488 newSize
.u
.LowPart
= ulOffset
.u
.LowPart
+ cb
;
492 * Verify if we need to grow the stream
494 if (newSize
.u
.LowPart
> This
->byteArraySize
.u
.LowPart
)
497 if (HGLOBALLockBytesImpl_SetSize(iface
, newSize
) == STG_E_MEDIUMFULL
)
498 return STG_E_MEDIUMFULL
;
502 * Lock the buffer in position and copy the data.
504 supportBuffer
= GlobalLock(This
->supportHandle
);
506 memcpy((char *) supportBuffer
+ ulOffset
.u
.LowPart
, pv
, cb
);
509 * Return the number of bytes written.
516 GlobalUnlock(This
->supportHandle
);
521 /******************************************************************************
522 * This method is part of the ILockBytes interface.
524 * See the documentation of ILockBytes for more info.
526 HRESULT WINAPI
HGLOBALLockBytesImpl_Flush(ILockBytes
* iface
)
531 /******************************************************************************
532 * This method is part of the ILockBytes interface.
534 * It will change the size of the byte array.
536 * See the documentation of ILockBytes for more info.
538 HRESULT WINAPI
HGLOBALLockBytesImpl_SetSize(
540 ULARGE_INTEGER libNewSize
) /* [in] */
542 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
543 HGLOBAL supportHandle
;
548 if (libNewSize
.u
.HighPart
!= 0)
549 return STG_E_INVALIDFUNCTION
;
551 if (This
->byteArraySize
.u
.LowPart
== libNewSize
.u
.LowPart
)
555 * Re allocate the HGlobal to fit the new size of the stream.
557 supportHandle
= GlobalReAlloc(This
->supportHandle
, libNewSize
.u
.LowPart
, 0);
559 if (supportHandle
== 0)
560 return STG_E_MEDIUMFULL
;
562 This
->supportHandle
= supportHandle
;
563 This
->byteArraySize
.u
.LowPart
= libNewSize
.u
.LowPart
;
568 /******************************************************************************
569 * This method is part of the ILockBytes interface.
571 * The global memory implementation of ILockBytes does not support locking.
573 * See the documentation of ILockBytes for more info.
575 HRESULT WINAPI
HGLOBALLockBytesImpl_LockRegion(
577 ULARGE_INTEGER libOffset
, /* [in] */
578 ULARGE_INTEGER cb
, /* [in] */
579 DWORD dwLockType
) /* [in] */
581 return STG_E_INVALIDFUNCTION
;
584 /******************************************************************************
585 * This method is part of the ILockBytes interface.
587 * The global memory implementation of ILockBytes does not support locking.
589 * See the documentation of ILockBytes for more info.
591 HRESULT WINAPI
HGLOBALLockBytesImpl_UnlockRegion(
593 ULARGE_INTEGER libOffset
, /* [in] */
594 ULARGE_INTEGER cb
, /* [in] */
595 DWORD dwLockType
) /* [in] */
597 return STG_E_INVALIDFUNCTION
;
600 /******************************************************************************
601 * This method is part of the ILockBytes interface.
603 * This method returns information about the current
606 * See the documentation of ILockBytes for more info.
608 HRESULT WINAPI
HGLOBALLockBytesImpl_Stat(
610 STATSTG
* pstatstg
, /* [out] */
611 DWORD grfStatFlag
) /* [in] */
613 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
615 memset(pstatstg
, 0, sizeof(STATSTG
));
617 pstatstg
->pwcsName
= NULL
;
618 pstatstg
->type
= STGTY_LOCKBYTES
;
619 pstatstg
->cbSize
= This
->byteArraySize
;