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
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
30 #include "wine/winbase16.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ole);
42 /******************************************************************************
43 * HGLOBALLockBytesImpl definition.
45 * This class imlements the ILockBytes inteface and represents a byte array
46 * object supported by an HGLOBAL pointer.
48 struct HGLOBALLockBytesImpl
51 * Needs to be the first item in the stuct
52 * since we want to cast this in an ILockBytes pointer
54 ICOM_VFIELD(ILockBytes);
62 * Support for the LockBytes object
64 HGLOBAL supportHandle;
67 * This flag is TRUE if the HGLOBAL is destroyed when the object
68 * is finally released.
73 * Helper variable that contains the size of the byte array
75 ULARGE_INTEGER byteArraySize;
78 typedef struct HGLOBALLockBytesImpl HGLOBALLockBytesImpl;
81 * Method definition for the HGLOBALLockBytesImpl class.
83 HGLOBALLockBytesImpl* HGLOBALLockBytesImpl_Construct(
85 BOOL fDeleteOnRelease);
87 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This);
89 HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(
91 REFIID riid, /* [in] */
92 void** ppvObject); /* [iid_is][out] */
94 ULONG WINAPI HGLOBALLockBytesImpl_AddRef(
97 ULONG WINAPI HGLOBALLockBytesImpl_Release(
100 HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(
102 ULARGE_INTEGER ulOffset, /* [in] */
103 void* pv, /* [length_is][size_is][out] */
105 ULONG* pcbRead); /* [out] */
107 HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt(
109 ULARGE_INTEGER ulOffset, /* [in] */
110 const void* pv, /* [size_is][in] */
112 ULONG* pcbWritten); /* [out] */
114 HRESULT WINAPI HGLOBALLockBytesImpl_Flush(
117 HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(
119 ULARGE_INTEGER libNewSize); /* [in] */
121 HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion(
123 ULARGE_INTEGER libOffset, /* [in] */
124 ULARGE_INTEGER cb, /* [in] */
125 DWORD dwLockType); /* [in] */
127 HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion(
129 ULARGE_INTEGER libOffset, /* [in] */
130 ULARGE_INTEGER cb, /* [in] */
131 DWORD dwLockType); /* [in] */
133 HRESULT WINAPI HGLOBALLockBytesImpl_Stat(
135 STATSTG* pstatstg, /* [out] */
136 DWORD grfStatFlag); /* [in] */
139 * Virtual function table for the HGLOBALLockBytesImpl class.
141 static ICOM_VTABLE(ILockBytes) HGLOBALLockBytesImpl_Vtbl =
143 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
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.57]
159 HRESULT WINAPI CreateILockBytesOnHGlobal(HGLOBAL hGlobal,
160 BOOL fDeleteOnRelease,
161 LPLOCKBYTES* ppLkbyt)
163 HGLOBALLockBytesImpl* newLockBytes;
165 newLockBytes = HGLOBALLockBytesImpl_Construct(hGlobal, fDeleteOnRelease);
167 if (newLockBytes != NULL)
169 return IUnknown_QueryInterface((IUnknown*)newLockBytes,
174 return E_OUTOFMEMORY;
177 /******************************************************************************
178 * GetHGlobalFromILockBytes [OLE32.70]
180 HRESULT WINAPI GetHGlobalFromILockBytes(ILockBytes* plkbyt, HGLOBAL* phglobal)
182 HGLOBALLockBytesImpl* const pMemLockBytes = (HGLOBALLockBytesImpl*)plkbyt;
185 ULARGE_INTEGER start;
189 if (pMemLockBytes->lpVtbl == &HGLOBALLockBytesImpl_Vtbl) {
190 *phglobal = pMemLockBytes->supportHandle;
195 /* It is not our lockbytes implementation, so use a more generic way */
196 hres = ILockBytes_Stat(plkbyt,&stbuf,0);
198 ERR("Cannot ILockBytes_Stat, %lx\n",hres);
201 FIXME("cbSize is %ld\n",stbuf.cbSize.s.LowPart);
202 *phglobal = GlobalAlloc( GMEM_MOVEABLE|GMEM_SHARE, stbuf.cbSize.s.LowPart);
205 memset(&start,0,sizeof(start));
206 hres = ILockBytes_ReadAt(plkbyt, start, GlobalLock(*phglobal), stbuf.cbSize.s.LowPart, &xread);
207 GlobalUnlock(*phglobal);
209 FIXME("%p->ReadAt failed with %lx\n",plkbyt,hres);
212 if (stbuf.cbSize.s.LowPart != xread) {
213 FIXME("Read size is not requested size %ld vs %ld?\n",stbuf.cbSize.s.LowPart, xread);
218 /******************************************************************************
220 * HGLOBALLockBytesImpl implementation
224 /******************************************************************************
225 * This is the constructor for the HGLOBALLockBytesImpl class.
228 * hGlobal - Handle that will support the stream. can be NULL.
229 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
230 * when the IStream object is destroyed.
232 HGLOBALLockBytesImpl* HGLOBALLockBytesImpl_Construct(HGLOBAL hGlobal,
233 BOOL fDeleteOnRelease)
235 HGLOBALLockBytesImpl* newLockBytes;
236 newLockBytes = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALLockBytesImpl));
241 * Set up the virtual function table and reference count.
243 newLockBytes->lpVtbl = &HGLOBALLockBytesImpl_Vtbl;
244 newLockBytes->ref = 0;
247 * Initialize the support.
249 newLockBytes->supportHandle = hGlobal;
250 newLockBytes->deleteOnRelease = fDeleteOnRelease;
253 * This method will allocate a handle if one is not supplied.
255 if (newLockBytes->supportHandle == 0)
257 newLockBytes->supportHandle = GlobalAlloc(GMEM_MOVEABLE |
263 * Initialize the size of the array to the size of the handle.
265 newLockBytes->byteArraySize.s.HighPart = 0;
266 newLockBytes->byteArraySize.s.LowPart = GlobalSize(
267 newLockBytes->supportHandle);
273 /******************************************************************************
274 * This is the destructor of the HGLOBALStreamImpl class.
276 * This method will clean-up all the resources used-up by the given
277 * HGLOBALLockBytesImpl class. The pointer passed-in to this function will be
278 * freed and will not be valid anymore.
280 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This)
283 * Release the HGlobal if the constructor asked for that.
285 if (This->deleteOnRelease)
287 GlobalFree(This->supportHandle);
288 This->supportHandle = 0;
292 * Finally, free the memory used-up by the class.
294 HeapFree(GetProcessHeap(), 0, This);
297 /******************************************************************************
298 * This implements the IUnknown method QueryInterface for this
301 HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(
303 REFIID riid, /* [in] */
304 void** ppvObject) /* [iid_is][out] */
306 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
309 * Perform a sanity check on the parameters.
315 * Initialize the return parameter.
320 * Compare the riid with the interface IDs implemented by this object.
322 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
324 *ppvObject = (ILockBytes*)This;
326 else if (memcmp(&IID_ILockBytes, riid, sizeof(IID_ILockBytes)) == 0)
328 *ppvObject = (ILockBytes*)This;
332 * Check that we obtained an interface.
335 return E_NOINTERFACE;
338 * Query Interface always increases the reference count by one when it is
341 HGLOBALLockBytesImpl_AddRef(iface);
346 /******************************************************************************
347 * This implements the IUnknown method AddRef for this
350 ULONG WINAPI HGLOBALLockBytesImpl_AddRef(ILockBytes* iface)
352 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
359 /******************************************************************************
360 * This implements the IUnknown method Release for this
363 ULONG WINAPI HGLOBALLockBytesImpl_Release(ILockBytes* iface)
365 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
374 * If the reference count goes down to 0, perform suicide.
378 HGLOBALLockBytesImpl_Destroy(This);
384 /******************************************************************************
385 * This method is part of the ILockBytes interface.
387 * It reads a block of information from the byte array at the specified
390 * See the documentation of ILockBytes for more info.
392 HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(
394 ULARGE_INTEGER ulOffset, /* [in] */
395 void* pv, /* [length_is][size_is][out] */
397 ULONG* pcbRead) /* [out] */
399 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
402 ULONG bytesReadBuffer = 0;
403 ULONG bytesToReadFromBuffer;
406 * If the caller is not interested in the number of bytes read,
407 * we use another buffer to avoid "if" statements in the code.
410 pcbRead = &bytesReadBuffer;
413 * Make sure the offset is valid.
415 if (ulOffset.s.LowPart > This->byteArraySize.s.LowPart)
419 * Using the known size of the array, calculate the number of bytes
422 bytesToReadFromBuffer = min(This->byteArraySize.s.LowPart -
423 ulOffset.s.LowPart, cb);
426 * Lock the buffer in position and copy the data.
428 supportBuffer = GlobalLock(This->supportHandle);
431 (char *) supportBuffer + ulOffset.s.LowPart,
432 bytesToReadFromBuffer);
435 * Return the number of bytes read.
437 *pcbRead = bytesToReadFromBuffer;
442 GlobalUnlock(This->supportHandle);
445 * The function returns S_OK if the specified number of bytes were read
446 * or the end of the array was reached.
447 * It returns STG_E_READFAULT if the number of bytes to read does not equal
448 * the number of bytes actually read.
453 return STG_E_READFAULT;
456 /******************************************************************************
457 * This method is part of the ILockBytes interface.
459 * It writes the specified bytes at the specified offset.
460 * position. If the array is too small, it will be resized.
462 * See the documentation of ILockBytes for more info.
464 HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt(
466 ULARGE_INTEGER ulOffset, /* [in] */
467 const void* pv, /* [size_is][in] */
469 ULONG* pcbWritten) /* [out] */
471 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
474 ULARGE_INTEGER newSize;
475 ULONG bytesWritten = 0;
478 * If the caller is not interested in the number of bytes written,
479 * we use another buffer to avoid "if" statements in the code.
482 pcbWritten = &bytesWritten;
490 newSize.s.HighPart = 0;
491 newSize.s.LowPart = ulOffset.s.LowPart + cb;
495 * Verify if we need to grow the stream
497 if (newSize.s.LowPart > This->byteArraySize.s.LowPart)
500 if (HGLOBALLockBytesImpl_SetSize(iface, newSize) == STG_E_MEDIUMFULL)
501 return STG_E_MEDIUMFULL;
505 * Lock the buffer in position and copy the data.
507 supportBuffer = GlobalLock(This->supportHandle);
509 memcpy((char *) supportBuffer + ulOffset.s.LowPart, pv, cb);
512 * Return the number of bytes written.
519 GlobalUnlock(This->supportHandle);
524 /******************************************************************************
525 * This method is part of the ILockBytes interface.
527 * See the documentation of ILockBytes for more info.
529 HRESULT WINAPI HGLOBALLockBytesImpl_Flush(ILockBytes* iface)
534 /******************************************************************************
535 * This method is part of the ILockBytes interface.
537 * It will change the size of the byte array.
539 * See the documentation of ILockBytes for more info.
541 HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(
543 ULARGE_INTEGER libNewSize) /* [in] */
545 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
550 if (libNewSize.s.HighPart != 0)
551 return STG_E_INVALIDFUNCTION;
553 if (This->byteArraySize.s.LowPart == libNewSize.s.LowPart)
557 * Re allocate the HGlobal to fit the new size of the stream.
559 This->supportHandle = GlobalReAlloc(This->supportHandle,
560 libNewSize.s.LowPart,
563 if (This->supportHandle == 0)
564 return STG_E_MEDIUMFULL;
566 This->byteArraySize.s.LowPart = libNewSize.s.LowPart;
571 /******************************************************************************
572 * This method is part of the ILockBytes interface.
574 * The global memory implementation of ILockBytes does not support locking.
576 * See the documentation of ILockBytes for more info.
578 HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion(
580 ULARGE_INTEGER libOffset, /* [in] */
581 ULARGE_INTEGER cb, /* [in] */
582 DWORD dwLockType) /* [in] */
584 return STG_E_INVALIDFUNCTION;
587 /******************************************************************************
588 * This method is part of the ILockBytes interface.
590 * The global memory implementation of ILockBytes does not support locking.
592 * See the documentation of ILockBytes for more info.
594 HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion(
596 ULARGE_INTEGER libOffset, /* [in] */
597 ULARGE_INTEGER cb, /* [in] */
598 DWORD dwLockType) /* [in] */
600 return STG_E_INVALIDFUNCTION;
603 /******************************************************************************
604 * This method is part of the ILockBytes interface.
606 * This method returns information about the current
609 * See the documentation of ILockBytes for more info.
611 HRESULT WINAPI HGLOBALLockBytesImpl_Stat(
613 STATSTG* pstatstg, /* [out] */
614 DWORD grfStatFlag) /* [in] */
616 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
618 memset(pstatstg, 0, sizeof(STATSTG));
620 pstatstg->pwcsName = NULL;
621 pstatstg->type = STGTY_LOCKBYTES;
622 pstatstg->cbSize = This->byteArraySize;
627 /******************************************************************************
628 * HGLOBALLockBytesImpl16 definition.
630 * This class imlements the ILockBytes inteface and represents a byte array
631 * object supported by an HGLOBAL pointer.
633 struct HGLOBALLockBytesImpl16
636 * Needs to be the first item in the stuct
637 * since we want to cast this in an ILockBytes pointer
639 ICOM_VFIELD(ILockBytes16);
643 * Support for the LockBytes object
645 HGLOBAL16 supportHandle;
648 * This flag is TRUE if the HGLOBAL is destroyed when the object
649 * is finally released.
651 BOOL deleteOnRelease;
653 * Helper variable that contains the size of the byte array
655 ULARGE_INTEGER byteArraySize;
658 typedef struct HGLOBALLockBytesImpl16 HGLOBALLockBytesImpl16;
660 HGLOBALLockBytesImpl16* HGLOBALLockBytesImpl16_Construct(
662 BOOL16 fDeleteOnRelease);
664 void HGLOBALLockBytesImpl16_Destroy(HGLOBALLockBytesImpl16* This);
666 HRESULT WINAPI HGLOBALLockBytesImpl16_QueryInterface(
668 REFIID riid, /* [in] */
669 void** ppvObject); /* [iid_is][out] */
671 ULONG WINAPI HGLOBALLockBytesImpl16_AddRef(
672 ILockBytes16* iface);
674 ULONG WINAPI HGLOBALLockBytesImpl16_Release(
675 ILockBytes16* iface);
677 HRESULT WINAPI HGLOBALLockBytesImpl16_ReadAt(
679 ULARGE_INTEGER ulOffset, /* [in] */
680 void* pv, /* [length_is][size_is][out] */
682 ULONG* pcbRead); /* [out] */
684 HRESULT WINAPI HGLOBALLockBytesImpl16_WriteAt(
686 ULARGE_INTEGER ulOffset, /* [in] */
687 const void* pv, /* [size_is][in] */
689 ULONG* pcbWritten); /* [out] */
691 HRESULT WINAPI HGLOBALLockBytesImpl16_Flush(
692 ILockBytes16* iface);
694 HRESULT WINAPI HGLOBALLockBytesImpl16_SetSize(
696 ULARGE_INTEGER libNewSize); /* [in] */
698 HRESULT WINAPI HGLOBALLockBytesImpl16_LockRegion(
700 ULARGE_INTEGER libOffset, /* [in] */
701 ULARGE_INTEGER cb, /* [in] */
702 DWORD dwLockType); /* [in] */
704 HRESULT WINAPI HGLOBALLockBytesImpl16_UnlockRegion(
706 ULARGE_INTEGER libOffset, /* [in] */
707 ULARGE_INTEGER cb, /* [in] */
708 DWORD dwLockType); /* [in] */
710 HRESULT WINAPI HGLOBALLockBytesImpl16_Stat(
712 STATSTG16* pstatstg, /* [out] */
713 DWORD grfStatFlag); /* [in] */
715 /******************************************************************************
717 * HGLOBALLockBytesImpl16 implementation
721 /******************************************************************************
722 * This is the constructor for the HGLOBALLockBytesImpl16 class.
725 * hGlobal - Handle that will support the stream. can be NULL.
726 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL16 will be released
727 * when the IStream object is destroyed.
729 HGLOBALLockBytesImpl16*
730 HGLOBALLockBytesImpl16_Construct(HGLOBAL16 hGlobal,
731 BOOL16 fDeleteOnRelease)
733 HGLOBALLockBytesImpl16* newLockBytes;
735 static ICOM_VTABLE(ILockBytes16) vt16;
736 static SEGPTR msegvt16;
737 HMODULE16 hcomp = GetModuleHandle16("OLE2");
740 TRACE("(%x,%d)\n",hGlobal,fDeleteOnRelease);
741 newLockBytes = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALLockBytesImpl16));
742 if (newLockBytes == NULL)
746 * Set up the virtual function table and reference count.
750 #define VTENT(x) vt16.x = (void*)GetProcAddress16(hcomp,"HGLOBALLockBytesImpl16_"#x);assert(vt16.x)
751 VTENT(QueryInterface);
761 msegvt16 = MapLS( &vt16 );
763 newLockBytes->lpVtbl = (ICOM_VTABLE(ILockBytes16)*)msegvt16;
764 newLockBytes->ref = 0;
766 * Initialize the support.
768 newLockBytes->supportHandle = hGlobal;
769 newLockBytes->deleteOnRelease = fDeleteOnRelease;
772 * This method will allocate a handle if one is not supplied.
774 if (newLockBytes->supportHandle == 0)
775 newLockBytes->supportHandle = GlobalAlloc16(GMEM_MOVEABLE | GMEM_NODISCARD, 0);
778 * Initialize the size of the array to the size of the handle.
780 newLockBytes->byteArraySize.s.HighPart = 0;
781 newLockBytes->byteArraySize.s.LowPart = GlobalSize16(
782 newLockBytes->supportHandle);
784 return (HGLOBALLockBytesImpl16*)MapLS(newLockBytes);
787 /******************************************************************************
788 * This is the destructor of the HGLOBALStreamImpl class.
790 * This method will clean-up all the resources used-up by the given
791 * HGLOBALLockBytesImpl16 class. The pointer passed-in to this function will be
792 * freed and will not be valid anymore.
794 void HGLOBALLockBytesImpl16_Destroy(HGLOBALLockBytesImpl16* This)
798 * Release the HGlobal if the constructor asked for that.
800 if (This->deleteOnRelease)
802 GlobalFree16(This->supportHandle);
803 This->supportHandle = 0;
807 * Finally, free the memory used-up by the class.
809 HeapFree(GetProcessHeap(), 0, This);
812 /******************************************************************************
813 * This implements the IUnknown method QueryInterface for this
816 HRESULT WINAPI HGLOBALLockBytesImpl16_QueryInterface(
817 ILockBytes16* iface, /* [in] SEGPTR */
818 REFIID riid, /* [in] */
819 void** ppvObject) /* [iid_is][out] (ptr to SEGPTR!) */
821 HGLOBALLockBytesImpl16* const This=(HGLOBALLockBytesImpl16*)MapSL((SEGPTR)iface);
823 TRACE("(%p,%s,%p)\n",iface,debugstr_guid(riid),ppvObject);
825 * Perform a sanity check on the parameters.
831 * Initialize the return parameter.
835 * Compare the riid with the interface IDs implemented by this object.
837 if ( !memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) ||
838 !memcmp(&IID_ILockBytes, riid, sizeof(IID_ILockBytes))
840 *ppvObject = (void*)iface;
843 * Check that we obtained an interface.
846 return E_NOINTERFACE;
849 * Query Interface always increases the reference count by one when it is
852 HGLOBALLockBytesImpl16_AddRef((ILockBytes16*)This);
857 /******************************************************************************
858 * This implements the IUnknown method AddRef for this
861 ULONG WINAPI HGLOBALLockBytesImpl16_AddRef(ILockBytes16* iface)
863 HGLOBALLockBytesImpl16* const This=(HGLOBALLockBytesImpl16*)iface;
865 TRACE("(%p)\n",This);
872 /******************************************************************************
873 * This implements the IUnknown method Release for this
876 ULONG WINAPI HGLOBALLockBytesImpl16_Release(ILockBytes16* iface)
878 HGLOBALLockBytesImpl16* const This=(HGLOBALLockBytesImpl16*)iface;
881 TRACE("(%p)\n",This);
888 * If the reference count goes down to 0, perform suicide.
891 HGLOBALLockBytesImpl16_Destroy(This);
895 /******************************************************************************
896 * This method is part of the ILockBytes interface.
898 * It reads a block of information from the byte array at the specified
901 * See the documentation of ILockBytes for more info.
903 HRESULT WINAPI HGLOBALLockBytesImpl16_ReadAt(
905 ULARGE_INTEGER ulOffset, /* [in] */
906 void* pv, /* [length_is][size_is][out] */
908 ULONG* pcbRead) /* [out] */
910 HGLOBALLockBytesImpl16* const This=(HGLOBALLockBytesImpl16*)iface;
913 ULONG bytesReadBuffer = 0;
914 ULONG bytesToReadFromBuffer;
916 TRACE("(%p,%ld,%p,%ld,%p)\n",This,ulOffset.s.LowPart,pv,cb,pcbRead);
918 * If the caller is not interested in the number of bytes read,
919 * we use another buffer to avoid "if" statements in the code.
922 pcbRead = &bytesReadBuffer;
925 * Make sure the offset is valid.
927 if (ulOffset.s.LowPart > This->byteArraySize.s.LowPart)
931 * Using the known size of the array, calculate the number of bytes
934 bytesToReadFromBuffer = min(This->byteArraySize.s.LowPart -
935 ulOffset.s.LowPart, cb);
938 * Lock the buffer in position and copy the data.
940 supportBuffer = GlobalLock16(This->supportHandle);
943 (char *) supportBuffer + ulOffset.s.LowPart,
944 bytesToReadFromBuffer);
947 * Return the number of bytes read.
949 *pcbRead = bytesToReadFromBuffer;
954 GlobalUnlock16(This->supportHandle);
957 * The function returns S_OK if the specified number of bytes were read
958 * or the end of the array was reached.
959 * It returns STG_E_READFAULT if the number of bytes to read does not equal
960 * the number of bytes actually read.
965 return STG_E_READFAULT;
968 /******************************************************************************
969 * This method is part of the ILockBytes interface.
971 * It writes the specified bytes at the specified offset.
972 * position. If the array is too small, it will be resized.
974 * See the documentation of ILockBytes for more info.
976 HRESULT WINAPI HGLOBALLockBytesImpl16_WriteAt(
978 ULARGE_INTEGER ulOffset, /* [in] */
979 const void* pv, /* [size_is][in] */
981 ULONG* pcbWritten) /* [out] */
983 HGLOBALLockBytesImpl16* const This=(HGLOBALLockBytesImpl16*)iface;
986 ULARGE_INTEGER newSize;
987 ULONG bytesWritten = 0;
989 TRACE("(%p,%ld,%p,%ld,%p)\n",This,ulOffset.s.LowPart,pv,cb,pcbWritten);
991 * If the caller is not interested in the number of bytes written,
992 * we use another buffer to avoid "if" statements in the code.
995 pcbWritten = &bytesWritten;
1000 newSize.s.HighPart = 0;
1001 newSize.s.LowPart = ulOffset.s.LowPart + cb;
1004 * Verify if we need to grow the stream
1006 if (newSize.s.LowPart > This->byteArraySize.s.LowPart)
1009 if (HGLOBALLockBytesImpl16_SetSize(iface, newSize) == STG_E_MEDIUMFULL)
1010 return STG_E_MEDIUMFULL;
1014 * Lock the buffer in position and copy the data.
1016 supportBuffer = GlobalLock16(This->supportHandle);
1018 memcpy((char *) supportBuffer + ulOffset.s.LowPart, pv, cb);
1021 * Return the number of bytes written.
1028 GlobalUnlock16(This->supportHandle);
1033 /******************************************************************************
1034 * This method is part of the ILockBytes interface.
1036 * See the documentation of ILockBytes for more info.
1038 HRESULT WINAPI HGLOBALLockBytesImpl16_Flush(ILockBytes16* iface)
1040 TRACE("(%p)\n",iface);
1044 /******************************************************************************
1045 * This method is part of the ILockBytes interface.
1047 * It will change the size of the byte array.
1049 * See the documentation of ILockBytes for more info.
1051 HRESULT WINAPI HGLOBALLockBytesImpl16_SetSize(
1052 ILockBytes16* iface,
1053 ULARGE_INTEGER libNewSize) /* [in] */
1055 HGLOBALLockBytesImpl16* const This=(HGLOBALLockBytesImpl16*)iface;
1057 TRACE("(%p,%ld)\n",This,libNewSize.s.LowPart);
1061 if (libNewSize.s.HighPart != 0)
1062 return STG_E_INVALIDFUNCTION;
1064 if (This->byteArraySize.s.LowPart == libNewSize.s.LowPart)
1068 * Re allocate the HGlobal to fit the new size of the stream.
1070 This->supportHandle = GlobalReAlloc16(This->supportHandle,
1071 libNewSize.s.LowPart,
1074 if (This->supportHandle == 0)
1075 return STG_E_MEDIUMFULL;
1077 This->byteArraySize.s.LowPart = libNewSize.s.LowPart;
1082 /******************************************************************************
1083 * This method is part of the ILockBytes interface.
1085 * The global memory implementation of ILockBytes does not support locking.
1087 * See the documentation of ILockBytes for more info.
1089 HRESULT WINAPI HGLOBALLockBytesImpl16_LockRegion(
1090 ILockBytes16* iface,
1091 ULARGE_INTEGER libOffset, /* [in] */
1092 ULARGE_INTEGER cb, /* [in] */
1093 DWORD dwLockType) /* [in] */
1095 return STG_E_INVALIDFUNCTION;
1098 /******************************************************************************
1099 * This method is part of the ILockBytes interface.
1101 * The global memory implementation of ILockBytes does not support locking.
1103 * See the documentation of ILockBytes for more info.
1105 HRESULT WINAPI HGLOBALLockBytesImpl16_UnlockRegion(
1106 ILockBytes16* iface,
1107 ULARGE_INTEGER libOffset, /* [in] */
1108 ULARGE_INTEGER cb, /* [in] */
1109 DWORD dwLockType) /* [in] */
1111 return STG_E_INVALIDFUNCTION;
1114 /******************************************************************************
1115 * This method is part of the ILockBytes interface.
1117 * This method returns information about the current
1118 * byte array object.
1120 * See the documentation of ILockBytes for more info.
1122 HRESULT WINAPI HGLOBALLockBytesImpl16_Stat(
1124 STATSTG16* pstatstg, /* [out] */
1125 DWORD grfStatFlag) /* [in] */
1127 HGLOBALLockBytesImpl16* const This=(HGLOBALLockBytesImpl16*)iface;
1129 memset(pstatstg, 0, sizeof(STATSTG16));
1131 pstatstg->pwcsName = NULL;
1132 pstatstg->type = STGTY_LOCKBYTES;
1133 pstatstg->cbSize = This->byteArraySize;
1138 /******************************************************************************
1139 * CreateILockBytesOnHGlobal [OLE2.54]
1141 * Creates an ILockBytes interface for a HGLOBAL handle.
1144 * hGlobal the global handle (16bit)
1145 * fDeleteOnRelease delete handle on release.
1146 * ppLkbyt pointer to ILockBytes interface.
1149 * Staddard OLE error return codes.
1152 HRESULT WINAPI CreateILockBytesOnHGlobal16(HGLOBAL16 hGlobal,
1153 BOOL16 fDeleteOnRelease,
1154 /*SEGPTR**/ LPLOCKBYTES16* ppLkbyt)
1156 HGLOBALLockBytesImpl16* newLockBytes; /* SEGPTR */
1158 newLockBytes = HGLOBALLockBytesImpl16_Construct(hGlobal, fDeleteOnRelease);
1160 if (newLockBytes != NULL)
1161 return HGLOBALLockBytesImpl16_QueryInterface((ILockBytes16*)newLockBytes,
1164 return E_OUTOFMEMORY;