Added support for forwarded ordinals in built-in dlls.
[wine/multimedia.git] / ole / memlockbytes.c
blob2381db8926588b600aa0590f17ed5db8808a2aed
1 /******************************************************************************
3 * Global memory implementation of ILockBytes.
5 * Copyright 1999 Thuy Nguyen
7 */
9 #include <string.h>
10 #include "winbase.h"
11 #include "winerror.h"
12 #include "objbase.h"
14 #include "debug.h"
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;
33 * Reference count
35 ULONG ref;
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.
46 BOOL deleteOnRelease;
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(
60 HGLOBAL hGlobal,
61 BOOL fDeleteOnRelease);
63 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This);
65 HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(
66 ILockBytes* iface,
67 REFIID riid, /* [in] */
68 void** ppvObject); /* [iid_is][out] */
70 ULONG WINAPI HGLOBALLockBytesImpl_AddRef(
71 ILockBytes* iface);
73 ULONG WINAPI HGLOBALLockBytesImpl_Release(
74 ILockBytes* iface);
76 HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(
77 ILockBytes* iface,
78 ULARGE_INTEGER ulOffset, /* [in] */
79 void* pv, /* [length_is][size_is][out] */
80 ULONG cb, /* [in] */
81 ULONG* pcbRead); /* [out] */
83 HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt(
84 ILockBytes* iface,
85 ULARGE_INTEGER ulOffset, /* [in] */
86 const void* pv, /* [size_is][in] */
87 ULONG cb, /* [in] */
88 ULONG* pcbWritten); /* [out] */
90 HRESULT WINAPI HGLOBALLockBytesImpl_Flush(
91 ILockBytes* iface);
93 HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(
94 ILockBytes* iface,
95 ULARGE_INTEGER libNewSize); /* [in] */
97 HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion(
98 ILockBytes* iface,
99 ULARGE_INTEGER libOffset, /* [in] */
100 ULARGE_INTEGER cb, /* [in] */
101 DWORD dwLockType); /* [in] */
103 HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion(
104 ILockBytes* iface,
105 ULARGE_INTEGER libOffset, /* [in] */
106 ULARGE_INTEGER cb, /* [in] */
107 DWORD dwLockType); /* [in] */
109 HRESULT WINAPI HGLOBALLockBytesImpl_Stat(
110 ILockBytes* iface,
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,
145 &IID_ILockBytes,
146 (void**)ppLkbyt);
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;
161 else
162 *phglobal = 0;
164 if (*phglobal == 0)
165 return E_INVALIDARG;
167 return S_OK;
170 /******************************************************************************
172 * HGLOBALLockBytesImpl implementation
176 /******************************************************************************
177 * This is the constructor for the HGLOBALLockBytesImpl class.
179 * Params:
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));
190 if (newLockBytes!=0)
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 |
210 GMEM_NODISCARD,
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);
222 return newLockBytes;
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
251 * class
253 HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(
254 ILockBytes* iface,
255 REFIID riid, /* [in] */
256 void** ppvObject) /* [iid_is][out] */
258 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
261 * Perform a sanity check on the parameters.
263 if (ppvObject==0)
264 return E_INVALIDARG;
267 * Initialize the return parameter.
269 *ppvObject = 0;
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.
286 if ((*ppvObject)==0)
287 return E_NOINTERFACE;
290 * Query Interface always increases the reference count by one when it is
291 * successful
293 HGLOBALLockBytesImpl_AddRef(iface);
295 return S_OK;;
298 /******************************************************************************
299 * This implements the IUnknown method AddRef for this
300 * class
302 ULONG WINAPI HGLOBALLockBytesImpl_AddRef(ILockBytes* iface)
304 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
306 This->ref++;
308 return This->ref;
311 /******************************************************************************
312 * This implements the IUnknown method Release for this
313 * class
315 ULONG WINAPI HGLOBALLockBytesImpl_Release(ILockBytes* iface)
317 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
319 ULONG newRef;
321 This->ref--;
323 newRef = This->ref;
326 * If the reference count goes down to 0, perform suicide.
328 if (newRef==0)
330 HGLOBALLockBytesImpl_Destroy(This);
333 return newRef;
336 /******************************************************************************
337 * This method is part of the ILockBytes interface.
339 * It reads a block of information from the byte array at the specified
340 * offset.
342 * See the documentation of ILockBytes for more info.
344 HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(
345 ILockBytes* iface,
346 ULARGE_INTEGER ulOffset, /* [in] */
347 void* pv, /* [length_is][size_is][out] */
348 ULONG cb, /* [in] */
349 ULONG* pcbRead) /* [out] */
351 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
353 void* supportBuffer;
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.
361 if (pcbRead == 0)
362 pcbRead = &bytesReadBuffer;
365 * Make sure the offset is valid.
367 if (ulOffset.LowPart > This->byteArraySize.LowPart)
368 return E_FAIL;
371 * Using the known size of the array, calculate the number of bytes
372 * to read.
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);
382 memcpy(pv,
383 (char *) supportBuffer + ulOffset.LowPart,
384 bytesToReadFromBuffer);
387 * Return the number of bytes read.
389 *pcbRead = bytesToReadFromBuffer;
392 * Cleanup
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.
402 if(*pcbRead == cb)
403 return S_OK;
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(
417 ILockBytes* iface,
418 ULARGE_INTEGER ulOffset, /* [in] */
419 const void* pv, /* [size_is][in] */
420 ULONG cb, /* [in] */
421 ULONG* pcbWritten) /* [out] */
423 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
425 void* supportBuffer;
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.
433 if (pcbWritten == 0)
434 pcbWritten = &bytesWritten;
436 if (cb == 0)
438 return S_OK;
440 else
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)
451 /* grow stream */
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.
466 *pcbWritten = cb;
469 * Cleanup
471 GlobalUnlock(This->supportHandle);
473 return S_OK;
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)
483 return S_OK;
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(
494 ILockBytes* iface,
495 ULARGE_INTEGER libNewSize) /* [in] */
497 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
500 * As documented.
502 if (libNewSize.HighPart != 0)
503 return STG_E_INVALIDFUNCTION;
505 if (This->byteArraySize.LowPart == libNewSize.LowPart)
506 return S_OK;
509 * Re allocate the HGlobal to fit the new size of the stream.
511 This->supportHandle = GlobalReAlloc(This->supportHandle,
512 libNewSize.LowPart,
515 if (This->supportHandle == 0)
516 return STG_E_MEDIUMFULL;
518 This->byteArraySize.LowPart = libNewSize.LowPart;
520 return S_OK;
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(
531 ILockBytes* iface,
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(
547 ILockBytes* iface,
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
559 * byte array object.
561 * See the documentation of ILockBytes for more info.
563 HRESULT WINAPI HGLOBALLockBytesImpl_Stat(
564 ILockBytes* iface,
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;
576 return S_OK;