1 /******************************************************************************
3 * Global memory implementation of ILockBytes.
5 * Copyright 1999 Thuy Nguyen
14 #include <ole32/ole32.h>
16 #include <storage32.h>
20 /******************************************************************************
21 * HGLOBALLockBytesImpl definition.
23 * This class imlements the ILockBytes inteface and represents a byte array
24 * object supported by an HGLOBAL pointer.
26 struct HGLOBALLockBytesImpl
29 * Needs to be the first item in the stuct
30 * since we want to cast this in an ILockBytes pointer
32 ICOM_VFIELD(ILockBytes
);
40 * Support for the LockBytes object
42 HGLOBAL supportHandle
;
45 * This flag is TRUE if the HGLOBAL is destroyed when the object
46 * is finally released.
51 * Helper variable that contains the size of the byte array
53 ULARGE_INTEGER byteArraySize
;
56 typedef struct HGLOBALLockBytesImpl HGLOBALLockBytesImpl
;
59 * Method definition for the HGLOBALLockBytesImpl class.
61 HGLOBALLockBytesImpl
* HGLOBALLockBytesImpl_Construct(
63 BOOL fDeleteOnRelease
);
65 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl
* This
);
67 HRESULT WINAPI
HGLOBALLockBytesImpl_QueryInterface(
69 REFIID riid
, /* [in] */
70 void** ppvObject
); /* [iid_is][out] */
72 ULONG WINAPI
HGLOBALLockBytesImpl_AddRef(
75 ULONG WINAPI
HGLOBALLockBytesImpl_Release(
78 HRESULT WINAPI
HGLOBALLockBytesImpl_ReadAt(
80 ULARGE_INTEGER ulOffset
, /* [in] */
81 void* pv
, /* [length_is][size_is][out] */
83 ULONG
* pcbRead
); /* [out] */
85 HRESULT WINAPI
HGLOBALLockBytesImpl_WriteAt(
87 ULARGE_INTEGER ulOffset
, /* [in] */
88 const void* pv
, /* [size_is][in] */
90 ULONG
* pcbWritten
); /* [out] */
92 HRESULT WINAPI
HGLOBALLockBytesImpl_Flush(
95 HRESULT WINAPI
HGLOBALLockBytesImpl_SetSize(
97 ULARGE_INTEGER libNewSize
); /* [in] */
99 HRESULT WINAPI
HGLOBALLockBytesImpl_LockRegion(
101 ULARGE_INTEGER libOffset
, /* [in] */
102 ULARGE_INTEGER cb
, /* [in] */
103 DWORD dwLockType
); /* [in] */
105 HRESULT WINAPI
HGLOBALLockBytesImpl_UnlockRegion(
107 ULARGE_INTEGER libOffset
, /* [in] */
108 ULARGE_INTEGER cb
, /* [in] */
109 DWORD dwLockType
); /* [in] */
111 HRESULT WINAPI
HGLOBALLockBytesImpl_Stat(
113 STATSTG
* pstatstg
, /* [out] */
114 DWORD grfStatFlag
); /* [in] */
117 * Virtual function table for the HGLOBALLockBytesImpl class.
119 static ICOM_VTABLE(ILockBytes
) HGLOBALLockBytesImpl_Vtbl
=
121 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
122 HGLOBALLockBytesImpl_QueryInterface
,
123 HGLOBALLockBytesImpl_AddRef
,
124 HGLOBALLockBytesImpl_Release
,
125 HGLOBALLockBytesImpl_ReadAt
,
126 HGLOBALLockBytesImpl_WriteAt
,
127 HGLOBALLockBytesImpl_Flush
,
128 HGLOBALLockBytesImpl_SetSize
,
129 HGLOBALLockBytesImpl_LockRegion
,
130 HGLOBALLockBytesImpl_UnlockRegion
,
131 HGLOBALLockBytesImpl_Stat
,
134 /******************************************************************************
135 * CreateILockBytesOnHGlobal [OLE32.57]
137 HRESULT WINAPI
CreateILockBytesOnHGlobal(HGLOBAL hGlobal
,
138 BOOL fDeleteOnRelease
,
139 LPLOCKBYTES
* ppLkbyt
)
141 HGLOBALLockBytesImpl
* newLockBytes
;
143 newLockBytes
= HGLOBALLockBytesImpl_Construct(hGlobal
, fDeleteOnRelease
);
145 if (newLockBytes
!= NULL
)
147 return IUnknown_QueryInterface((IUnknown
*)newLockBytes
,
152 return E_OUTOFMEMORY
;
155 /******************************************************************************
156 * GetHGlobalFromILockBytes [OLE32.70]
158 HRESULT WINAPI
GetHGlobalFromILockBytes(ILockBytes
* plkbyt
, HGLOBAL
* phglobal
)
160 HGLOBALLockBytesImpl
* const pMemLockBytes
= (HGLOBALLockBytesImpl
*)plkbyt
;
162 if (ICOM_VTBL(pMemLockBytes
) == &HGLOBALLockBytesImpl_Vtbl
)
163 *phglobal
= pMemLockBytes
->supportHandle
;
173 /******************************************************************************
175 * HGLOBALLockBytesImpl implementation
179 /******************************************************************************
180 * This is the constructor for the HGLOBALLockBytesImpl class.
183 * hGlobal - Handle that will support the stream. can be NULL.
184 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
185 * when the IStream object is destroyed.
187 HGLOBALLockBytesImpl
* HGLOBALLockBytesImpl_Construct(HGLOBAL hGlobal
,
188 BOOL fDeleteOnRelease
)
190 HGLOBALLockBytesImpl
* newLockBytes
;
191 newLockBytes
= HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALLockBytesImpl
));
196 * Set up the virtual function table and reference count.
198 ICOM_VTBL(newLockBytes
) = &HGLOBALLockBytesImpl_Vtbl
;
199 newLockBytes
->ref
= 0;
202 * Initialize the support.
204 newLockBytes
->supportHandle
= hGlobal
;
205 newLockBytes
->deleteOnRelease
= fDeleteOnRelease
;
208 * This method will allocate a handle if one is not supplied.
210 if (newLockBytes
->supportHandle
== 0)
212 newLockBytes
->supportHandle
= GlobalAlloc(GMEM_MOVEABLE
|
218 * Initialize the size of the array to the size of the handle.
220 newLockBytes
->byteArraySize
.u
.HighPart
= 0;
221 newLockBytes
->byteArraySize
.u
.LowPart
= GlobalSize(
222 newLockBytes
->supportHandle
);
228 /******************************************************************************
229 * This is the destructor of the HGLOBALStreamImpl class.
231 * This method will clean-up all the resources used-up by the given
232 * HGLOBALLockBytesImpl class. The pointer passed-in to this function will be
233 * freed and will not be valid anymore.
235 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl
* This
)
238 * Release the HGlobal if the constructor asked for that.
240 if (This
->deleteOnRelease
)
242 GlobalFree(This
->supportHandle
);
243 This
->supportHandle
= 0;
247 * Finally, free the memory used-up by the class.
249 HeapFree(GetProcessHeap(), 0, This
);
252 /******************************************************************************
253 * This implements the IUnknown method QueryInterface for this
256 HRESULT WINAPI
HGLOBALLockBytesImpl_QueryInterface(
258 REFIID riid
, /* [in] */
259 void** ppvObject
) /* [iid_is][out] */
261 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
264 * Perform a sanity check on the parameters.
270 * Initialize the return parameter.
275 * Compare the riid with the interface IDs implemented by this object.
277 if (memcmp(&IID_IUnknown
, riid
, sizeof(IID_IUnknown
)) == 0)
279 *ppvObject
= (ILockBytes
*)This
;
281 else if (memcmp(&IID_ILockBytes
, riid
, sizeof(IID_ILockBytes
)) == 0)
283 *ppvObject
= (ILockBytes
*)This
;
287 * Check that we obtained an interface.
290 return E_NOINTERFACE
;
293 * Query Interface always increases the reference count by one when it is
296 HGLOBALLockBytesImpl_AddRef(iface
);
301 /******************************************************************************
302 * This implements the IUnknown method AddRef for this
305 ULONG WINAPI
HGLOBALLockBytesImpl_AddRef(ILockBytes
* iface
)
307 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
314 /******************************************************************************
315 * This implements the IUnknown method Release for this
318 ULONG WINAPI
HGLOBALLockBytesImpl_Release(ILockBytes
* iface
)
320 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
329 * If the reference count goes down to 0, perform suicide.
333 HGLOBALLockBytesImpl_Destroy(This
);
339 /******************************************************************************
340 * This method is part of the ILockBytes interface.
342 * It reads a block of information from the byte array at the specified
345 * See the documentation of ILockBytes for more info.
347 HRESULT WINAPI
HGLOBALLockBytesImpl_ReadAt(
349 ULARGE_INTEGER ulOffset
, /* [in] */
350 void* pv
, /* [length_is][size_is][out] */
352 ULONG
* pcbRead
) /* [out] */
354 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
357 ULONG bytesReadBuffer
= 0;
358 ULONG bytesToReadFromBuffer
;
361 * If the caller is not interested in the number of bytes read,
362 * we use another buffer to avoid "if" statements in the code.
365 pcbRead
= &bytesReadBuffer
;
368 * Make sure the offset is valid.
370 if (ulOffset
.u
.LowPart
> This
->byteArraySize
.u
.LowPart
)
374 * Using the known size of the array, calculate the number of bytes
377 bytesToReadFromBuffer
= min(This
->byteArraySize
.u
.LowPart
-
378 ulOffset
.u
.LowPart
, cb
);
381 * Lock the buffer in position and copy the data.
383 supportBuffer
= GlobalLock(This
->supportHandle
);
386 (char *) supportBuffer
+ ulOffset
.u
.LowPart
,
387 bytesToReadFromBuffer
);
390 * Return the number of bytes read.
392 *pcbRead
= bytesToReadFromBuffer
;
397 GlobalUnlock(This
->supportHandle
);
400 * The function returns S_OK if the specified number of bytes were read
401 * or the end of the array was reached.
402 * It returns STG_E_READFAULT if the number of bytes to read does not equal
403 * the number of bytes actually read.
408 return STG_E_READFAULT
;
411 /******************************************************************************
412 * This method is part of the ILockBytes interface.
414 * It writes the specified bytes at the specified offset.
415 * position. If the array is too small, it will be resized.
417 * See the documentation of ILockBytes for more info.
419 HRESULT WINAPI
HGLOBALLockBytesImpl_WriteAt(
421 ULARGE_INTEGER ulOffset
, /* [in] */
422 const void* pv
, /* [size_is][in] */
424 ULONG
* pcbWritten
) /* [out] */
426 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
429 ULARGE_INTEGER newSize
;
430 ULONG bytesWritten
= 0;
433 * If the caller is not interested in the number of bytes written,
434 * we use another buffer to avoid "if" statements in the code.
437 pcbWritten
= &bytesWritten
;
445 newSize
.u
.HighPart
= 0;
446 newSize
.u
.LowPart
= ulOffset
.u
.LowPart
+ cb
;
450 * Verify if we need to grow the stream
452 if (newSize
.u
.LowPart
> This
->byteArraySize
.u
.LowPart
)
455 if (HGLOBALLockBytesImpl_SetSize(iface
, newSize
) == STG_E_MEDIUMFULL
)
456 return STG_E_MEDIUMFULL
;
460 * Lock the buffer in position and copy the data.
462 supportBuffer
= GlobalLock(This
->supportHandle
);
464 memcpy((char *) supportBuffer
+ ulOffset
.u
.LowPart
, pv
, cb
);
467 * Return the number of bytes written.
474 GlobalUnlock(This
->supportHandle
);
479 /******************************************************************************
480 * This method is part of the ILockBytes interface.
482 * See the documentation of ILockBytes for more info.
484 HRESULT WINAPI
HGLOBALLockBytesImpl_Flush(ILockBytes
* iface
)
489 /******************************************************************************
490 * This method is part of the ILockBytes interface.
492 * It will change the size of the byte array.
494 * See the documentation of ILockBytes for more info.
496 HRESULT WINAPI
HGLOBALLockBytesImpl_SetSize(
498 ULARGE_INTEGER libNewSize
) /* [in] */
500 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
505 if (libNewSize
.u
.HighPart
!= 0)
506 return STG_E_INVALIDFUNCTION
;
508 if (This
->byteArraySize
.u
.LowPart
== libNewSize
.u
.LowPart
)
512 * Re allocate the HGlobal to fit the new size of the stream.
514 This
->supportHandle
= GlobalReAlloc(This
->supportHandle
,
515 libNewSize
.u
.LowPart
,
518 if (This
->supportHandle
== 0)
519 return STG_E_MEDIUMFULL
;
521 This
->byteArraySize
.u
.LowPart
= libNewSize
.u
.LowPart
;
526 /******************************************************************************
527 * This method is part of the ILockBytes interface.
529 * The global memory implementation of ILockBytes does not support locking.
531 * See the documentation of ILockBytes for more info.
533 HRESULT WINAPI
HGLOBALLockBytesImpl_LockRegion(
535 ULARGE_INTEGER libOffset
, /* [in] */
536 ULARGE_INTEGER cb
, /* [in] */
537 DWORD dwLockType
) /* [in] */
539 return STG_E_INVALIDFUNCTION
;
542 /******************************************************************************
543 * This method is part of the ILockBytes interface.
545 * The global memory implementation of ILockBytes does not support locking.
547 * See the documentation of ILockBytes for more info.
549 HRESULT WINAPI
HGLOBALLockBytesImpl_UnlockRegion(
551 ULARGE_INTEGER libOffset
, /* [in] */
552 ULARGE_INTEGER cb
, /* [in] */
553 DWORD dwLockType
) /* [in] */
555 return STG_E_INVALIDFUNCTION
;
558 /******************************************************************************
559 * This method is part of the ILockBytes interface.
561 * This method returns information about the current
564 * See the documentation of ILockBytes for more info.
566 HRESULT WINAPI
HGLOBALLockBytesImpl_Stat(
568 STATSTG
* pstatstg
, /* [out] */
569 DWORD grfStatFlag
) /* [in] */
571 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
573 memset(pstatstg
, 0, sizeof(STATSTG
));
575 pstatstg
->pwcsName
= NULL
;
576 pstatstg
->type
= STGTY_LOCKBYTES
;
577 pstatstg
->cbSize
= This
->byteArraySize
;