1 /******************************************************************************
3 * Global memory implementation of ILockBytes.
5 * Copyright 1999 Thuy Nguyen
16 DEFAULT_DEBUG_CHANNEL(ole
)
18 /******************************************************************************
19 * HGLOBALLockBytesImpl definition.
21 * This class imlements the ILockBytes inteface and represents a byte array
22 * object supported by an HGLOBAL pointer.
24 struct HGLOBALLockBytesImpl
27 * Needs to be the first item in the stuct
28 * since we want to cast this in an ILockBytes pointer
30 ICOM_VTABLE(ILockBytes
) *lpvtbl
;
38 * Support for the LockBytes object
40 HGLOBAL supportHandle
;
43 * This flag is TRUE if the HGLOBAL is destroyed when the object
44 * is finally released.
49 * Helper variable that contains the size of the byte array
51 ULARGE_INTEGER byteArraySize
;
54 typedef struct HGLOBALLockBytesImpl HGLOBALLockBytesImpl
;
57 * Method definition for the HGLOBALLockBytesImpl class.
59 HGLOBALLockBytesImpl
* HGLOBALLockBytesImpl_Construct(
61 BOOL fDeleteOnRelease
);
63 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl
* This
);
65 HRESULT WINAPI
HGLOBALLockBytesImpl_QueryInterface(
67 REFIID riid
, /* [in] */
68 void** ppvObject
); /* [iid_is][out] */
70 ULONG WINAPI
HGLOBALLockBytesImpl_AddRef(
73 ULONG WINAPI
HGLOBALLockBytesImpl_Release(
76 HRESULT WINAPI
HGLOBALLockBytesImpl_ReadAt(
78 ULARGE_INTEGER ulOffset
, /* [in] */
79 void* pv
, /* [length_is][size_is][out] */
81 ULONG
* pcbRead
); /* [out] */
83 HRESULT WINAPI
HGLOBALLockBytesImpl_WriteAt(
85 ULARGE_INTEGER ulOffset
, /* [in] */
86 const void* pv
, /* [size_is][in] */
88 ULONG
* pcbWritten
); /* [out] */
90 HRESULT WINAPI
HGLOBALLockBytesImpl_Flush(
93 HRESULT WINAPI
HGLOBALLockBytesImpl_SetSize(
95 ULARGE_INTEGER libNewSize
); /* [in] */
97 HRESULT WINAPI
HGLOBALLockBytesImpl_LockRegion(
99 ULARGE_INTEGER libOffset
, /* [in] */
100 ULARGE_INTEGER cb
, /* [in] */
101 DWORD dwLockType
); /* [in] */
103 HRESULT WINAPI
HGLOBALLockBytesImpl_UnlockRegion(
105 ULARGE_INTEGER libOffset
, /* [in] */
106 ULARGE_INTEGER cb
, /* [in] */
107 DWORD dwLockType
); /* [in] */
109 HRESULT WINAPI
HGLOBALLockBytesImpl_Stat(
111 STATSTG
* pstatstg
, /* [out] */
112 DWORD grfStatFlag
); /* [in] */
115 * Virtual function table for the HGLOBALLockBytesImpl class.
117 static ICOM_VTABLE(ILockBytes
) HGLOBALLockBytesImpl_Vtbl
=
119 HGLOBALLockBytesImpl_QueryInterface
,
120 HGLOBALLockBytesImpl_AddRef
,
121 HGLOBALLockBytesImpl_Release
,
122 HGLOBALLockBytesImpl_ReadAt
,
123 HGLOBALLockBytesImpl_WriteAt
,
124 HGLOBALLockBytesImpl_Flush
,
125 HGLOBALLockBytesImpl_SetSize
,
126 HGLOBALLockBytesImpl_LockRegion
,
127 HGLOBALLockBytesImpl_UnlockRegion
,
128 HGLOBALLockBytesImpl_Stat
,
131 /******************************************************************************
132 * CreateILockBytesOnHGlobal [OLE32.57]
134 HRESULT WINAPI
CreateILockBytesOnHGlobal(HGLOBAL hGlobal
,
135 BOOL fDeleteOnRelease
,
136 LPLOCKBYTES
* ppLkbyt
)
138 HGLOBALLockBytesImpl
* newLockBytes
;
140 newLockBytes
= HGLOBALLockBytesImpl_Construct(hGlobal
, fDeleteOnRelease
);
142 if (newLockBytes
!= NULL
)
144 return IUnknown_QueryInterface((IUnknown
*)newLockBytes
,
149 return E_OUTOFMEMORY
;
152 /******************************************************************************
153 * GetHGlobalFromILockBytes [OLE32.70]
155 HRESULT WINAPI
GetHGlobalFromILockBytes(ILockBytes
* plkbyt
, HGLOBAL
* phglobal
)
157 HGLOBALLockBytesImpl
* const pMemLockBytes
= (HGLOBALLockBytesImpl
*)plkbyt
;
159 if (pMemLockBytes
->lpvtbl
== &HGLOBALLockBytesImpl_Vtbl
)
160 *phglobal
= pMemLockBytes
->supportHandle
;
170 /******************************************************************************
172 * HGLOBALLockBytesImpl implementation
176 /******************************************************************************
177 * This is the constructor for the HGLOBALLockBytesImpl class.
180 * hGlobal - Handle that will support the stream. can be NULL.
181 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
182 * when the IStream object is destroyed.
184 HGLOBALLockBytesImpl
* HGLOBALLockBytesImpl_Construct(HGLOBAL hGlobal
,
185 BOOL fDeleteOnRelease
)
187 HGLOBALLockBytesImpl
* newLockBytes
;
188 newLockBytes
= HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALLockBytesImpl
));
193 * Set up the virtual function table and reference count.
195 newLockBytes
->lpvtbl
= &HGLOBALLockBytesImpl_Vtbl
;
196 newLockBytes
->ref
= 0;
199 * Initialize the support.
201 newLockBytes
->supportHandle
= hGlobal
;
202 newLockBytes
->deleteOnRelease
= fDeleteOnRelease
;
205 * This method will allocate a handle if one is not supplied.
207 if (newLockBytes
->supportHandle
== 0)
209 newLockBytes
->supportHandle
= GlobalAlloc(GMEM_MOVEABLE
|
215 * Initialize the size of the array to the size of the handle.
217 newLockBytes
->byteArraySize
.HighPart
= 0;
218 newLockBytes
->byteArraySize
.LowPart
= GlobalSize(
219 newLockBytes
->supportHandle
);
225 /******************************************************************************
226 * This is the destructor of the HGLOBALStreamImpl class.
228 * This method will clean-up all the resources used-up by the given
229 * HGLOBALLockBytesImpl class. The pointer passed-in to this function will be
230 * freed and will not be valid anymore.
232 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl
* This
)
235 * Release the HGlobal if the constructor asked for that.
237 if (This
->deleteOnRelease
)
239 GlobalFree(This
->supportHandle
);
240 This
->supportHandle
= 0;
244 * Finally, free the memory used-up by the class.
246 HeapFree(GetProcessHeap(), 0, This
);
249 /******************************************************************************
250 * This implements the IUnknown method QueryInterface for this
253 HRESULT WINAPI
HGLOBALLockBytesImpl_QueryInterface(
255 REFIID riid
, /* [in] */
256 void** ppvObject
) /* [iid_is][out] */
258 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
261 * Perform a sanity check on the parameters.
267 * Initialize the return parameter.
272 * Compare the riid with the interface IDs implemented by this object.
274 if (memcmp(&IID_IUnknown
, riid
, sizeof(IID_IUnknown
)) == 0)
276 *ppvObject
= (ILockBytes
*)This
;
278 else if (memcmp(&IID_ILockBytes
, riid
, sizeof(IID_ILockBytes
)) == 0)
280 *ppvObject
= (ILockBytes
*)This
;
284 * Check that we obtained an interface.
287 return E_NOINTERFACE
;
290 * Query Interface always increases the reference count by one when it is
293 HGLOBALLockBytesImpl_AddRef(iface
);
298 /******************************************************************************
299 * This implements the IUnknown method AddRef for this
302 ULONG WINAPI
HGLOBALLockBytesImpl_AddRef(ILockBytes
* iface
)
304 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
311 /******************************************************************************
312 * This implements the IUnknown method Release for this
315 ULONG WINAPI
HGLOBALLockBytesImpl_Release(ILockBytes
* iface
)
317 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
326 * If the reference count goes down to 0, perform suicide.
330 HGLOBALLockBytesImpl_Destroy(This
);
336 /******************************************************************************
337 * This method is part of the ILockBytes interface.
339 * It reads a block of information from the byte array at the specified
342 * See the documentation of ILockBytes for more info.
344 HRESULT WINAPI
HGLOBALLockBytesImpl_ReadAt(
346 ULARGE_INTEGER ulOffset
, /* [in] */
347 void* pv
, /* [length_is][size_is][out] */
349 ULONG
* pcbRead
) /* [out] */
351 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
354 ULONG bytesReadBuffer
= 0;
355 ULONG bytesToReadFromBuffer
;
358 * If the caller is not interested in the number of bytes read,
359 * we use another buffer to avoid "if" statements in the code.
362 pcbRead
= &bytesReadBuffer
;
365 * Make sure the offset is valid.
367 if (ulOffset
.LowPart
> This
->byteArraySize
.LowPart
)
371 * Using the known size of the array, calculate the number of bytes
374 bytesToReadFromBuffer
= MIN(This
->byteArraySize
.LowPart
-
375 ulOffset
.LowPart
, cb
);
378 * Lock the buffer in position and copy the data.
380 supportBuffer
= GlobalLock(This
->supportHandle
);
383 (char *) supportBuffer
+ ulOffset
.LowPart
,
384 bytesToReadFromBuffer
);
387 * Return the number of bytes read.
389 *pcbRead
= bytesToReadFromBuffer
;
394 GlobalUnlock(This
->supportHandle
);
397 * The function returns S_OK if the specified number of bytes were read
398 * or the end of the array was reached.
399 * It returns STG_E_READFAULT if the number of bytes to read does not equal
400 * the number of bytes actually read.
405 return STG_E_READFAULT
;
408 /******************************************************************************
409 * This method is part of the ILockBytes interface.
411 * It writes the specified bytes at the specified offset.
412 * position. If the array is too small, it will be resized.
414 * See the documentation of ILockBytes for more info.
416 HRESULT WINAPI
HGLOBALLockBytesImpl_WriteAt(
418 ULARGE_INTEGER ulOffset
, /* [in] */
419 const void* pv
, /* [size_is][in] */
421 ULONG
* pcbWritten
) /* [out] */
423 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
426 ULARGE_INTEGER newSize
;
427 ULONG bytesWritten
= 0;
430 * If the caller is not interested in the number of bytes written,
431 * we use another buffer to avoid "if" statements in the code.
434 pcbWritten
= &bytesWritten
;
442 newSize
.HighPart
= 0;
443 newSize
.LowPart
= ulOffset
.LowPart
+ cb
;
447 * Verify if we need to grow the stream
449 if (newSize
.LowPart
> This
->byteArraySize
.LowPart
)
452 if (HGLOBALLockBytesImpl_SetSize(iface
, newSize
) == STG_E_MEDIUMFULL
)
453 return STG_E_MEDIUMFULL
;
457 * Lock the buffer in position and copy the data.
459 supportBuffer
= GlobalLock(This
->supportHandle
);
461 memcpy((char *) supportBuffer
+ ulOffset
.LowPart
, pv
, cb
);
464 * Return the number of bytes written.
471 GlobalUnlock(This
->supportHandle
);
476 /******************************************************************************
477 * This method is part of the ILockBytes interface.
479 * See the documentation of ILockBytes for more info.
481 HRESULT WINAPI
HGLOBALLockBytesImpl_Flush(ILockBytes
* iface
)
486 /******************************************************************************
487 * This method is part of the ILockBytes interface.
489 * It will change the size of the byte array.
491 * See the documentation of ILockBytes for more info.
493 HRESULT WINAPI
HGLOBALLockBytesImpl_SetSize(
495 ULARGE_INTEGER libNewSize
) /* [in] */
497 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
502 if (libNewSize
.HighPart
!= 0)
503 return STG_E_INVALIDFUNCTION
;
505 if (This
->byteArraySize
.LowPart
== libNewSize
.LowPart
)
509 * Re allocate the HGlobal to fit the new size of the stream.
511 This
->supportHandle
= GlobalReAlloc(This
->supportHandle
,
515 if (This
->supportHandle
== 0)
516 return STG_E_MEDIUMFULL
;
518 This
->byteArraySize
.LowPart
= libNewSize
.LowPart
;
523 /******************************************************************************
524 * This method is part of the ILockBytes interface.
526 * The global memory implementation of ILockBytes does not support locking.
528 * See the documentation of ILockBytes for more info.
530 HRESULT WINAPI
HGLOBALLockBytesImpl_LockRegion(
532 ULARGE_INTEGER libOffset
, /* [in] */
533 ULARGE_INTEGER cb
, /* [in] */
534 DWORD dwLockType
) /* [in] */
536 return STG_E_INVALIDFUNCTION
;
539 /******************************************************************************
540 * This method is part of the ILockBytes interface.
542 * The global memory implementation of ILockBytes does not support locking.
544 * See the documentation of ILockBytes for more info.
546 HRESULT WINAPI
HGLOBALLockBytesImpl_UnlockRegion(
548 ULARGE_INTEGER libOffset
, /* [in] */
549 ULARGE_INTEGER cb
, /* [in] */
550 DWORD dwLockType
) /* [in] */
552 return STG_E_INVALIDFUNCTION
;
555 /******************************************************************************
556 * This method is part of the ILockBytes interface.
558 * This method returns information about the current
561 * See the documentation of ILockBytes for more info.
563 HRESULT WINAPI
HGLOBALLockBytesImpl_Stat(
565 STATSTG
* pstatstg
, /* [out] */
566 DWORD grfStatFlag
) /* [in] */
568 HGLOBALLockBytesImpl
* const This
=(HGLOBALLockBytesImpl
*)iface
;
570 memset(pstatstg
, 0, sizeof(STATSTG
));
572 pstatstg
->pwcsName
= NULL
;
573 pstatstg
->type
= STGTY_LOCKBYTES
;
574 pstatstg
->cbSize
= This
->byteArraySize
;