Get rid of the no longer used ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
[wine/multimedia.git] / dlls / ole32 / memlockbytes.c
blob65a0fce3f7d9e82c83d2f5a1b08ae29bf2a1702f
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
22 #include "config.h"
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <string.h>
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winuser.h"
33 #include "wine/winbase16.h"
34 #include "objbase.h"
35 #include "ole2.h"
36 #include "winerror.h"
38 #include "ifs.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(ole);
44 /******************************************************************************
45 * HGLOBALLockBytesImpl definition.
47 * This class imlements the ILockBytes inteface and represents a byte array
48 * object supported by an HGLOBAL pointer.
50 struct HGLOBALLockBytesImpl
53 * Needs to be the first item in the stuct
54 * since we want to cast this in an ILockBytes pointer
56 ILockBytesVtbl *lpVtbl;
59 * Reference count
61 ULONG ref;
64 * Support for the LockBytes object
66 HGLOBAL supportHandle;
69 * This flag is TRUE if the HGLOBAL is destroyed when the object
70 * is finally released.
72 BOOL deleteOnRelease;
75 * Helper variable that contains the size of the byte array
77 ULARGE_INTEGER byteArraySize;
80 typedef struct HGLOBALLockBytesImpl HGLOBALLockBytesImpl;
83 * Method definition for the HGLOBALLockBytesImpl class.
85 HGLOBALLockBytesImpl* HGLOBALLockBytesImpl_Construct(
86 HGLOBAL hGlobal,
87 BOOL fDeleteOnRelease);
89 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This);
91 HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(
92 ILockBytes* iface,
93 REFIID riid, /* [in] */
94 void** ppvObject); /* [iid_is][out] */
96 ULONG WINAPI HGLOBALLockBytesImpl_AddRef(
97 ILockBytes* iface);
99 ULONG WINAPI HGLOBALLockBytesImpl_Release(
100 ILockBytes* iface);
102 HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(
103 ILockBytes* iface,
104 ULARGE_INTEGER ulOffset, /* [in] */
105 void* pv, /* [length_is][size_is][out] */
106 ULONG cb, /* [in] */
107 ULONG* pcbRead); /* [out] */
109 HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt(
110 ILockBytes* iface,
111 ULARGE_INTEGER ulOffset, /* [in] */
112 const void* pv, /* [size_is][in] */
113 ULONG cb, /* [in] */
114 ULONG* pcbWritten); /* [out] */
116 HRESULT WINAPI HGLOBALLockBytesImpl_Flush(
117 ILockBytes* iface);
119 HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(
120 ILockBytes* iface,
121 ULARGE_INTEGER libNewSize); /* [in] */
123 HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion(
124 ILockBytes* iface,
125 ULARGE_INTEGER libOffset, /* [in] */
126 ULARGE_INTEGER cb, /* [in] */
127 DWORD dwLockType); /* [in] */
129 HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion(
130 ILockBytes* iface,
131 ULARGE_INTEGER libOffset, /* [in] */
132 ULARGE_INTEGER cb, /* [in] */
133 DWORD dwLockType); /* [in] */
135 HRESULT WINAPI HGLOBALLockBytesImpl_Stat(
136 ILockBytes* iface,
137 STATSTG* pstatstg, /* [out] */
138 DWORD grfStatFlag); /* [in] */
141 * Virtual function table for the HGLOBALLockBytesImpl class.
143 static ILockBytesVtbl HGLOBALLockBytesImpl_Vtbl =
145 HGLOBALLockBytesImpl_QueryInterface,
146 HGLOBALLockBytesImpl_AddRef,
147 HGLOBALLockBytesImpl_Release,
148 HGLOBALLockBytesImpl_ReadAt,
149 HGLOBALLockBytesImpl_WriteAt,
150 HGLOBALLockBytesImpl_Flush,
151 HGLOBALLockBytesImpl_SetSize,
152 HGLOBALLockBytesImpl_LockRegion,
153 HGLOBALLockBytesImpl_UnlockRegion,
154 HGLOBALLockBytesImpl_Stat,
157 /******************************************************************************
158 * CreateILockBytesOnHGlobal [OLE32.@]
160 HRESULT WINAPI CreateILockBytesOnHGlobal(HGLOBAL hGlobal,
161 BOOL fDeleteOnRelease,
162 LPLOCKBYTES* ppLkbyt)
164 HGLOBALLockBytesImpl* newLockBytes;
166 newLockBytes = HGLOBALLockBytesImpl_Construct(hGlobal, fDeleteOnRelease);
168 if (newLockBytes != NULL)
170 return IUnknown_QueryInterface((IUnknown*)newLockBytes,
171 &IID_ILockBytes,
172 (void**)ppLkbyt);
175 return E_OUTOFMEMORY;
178 /******************************************************************************
179 * GetHGlobalFromILockBytes [OLE32.@]
181 HRESULT WINAPI GetHGlobalFromILockBytes(ILockBytes* plkbyt, HGLOBAL* phglobal)
183 HGLOBALLockBytesImpl* const pMemLockBytes = (HGLOBALLockBytesImpl*)plkbyt;
184 STATSTG stbuf;
185 HRESULT hres;
186 ULARGE_INTEGER start;
187 ULONG xread;
189 *phglobal = 0;
190 if (pMemLockBytes->lpVtbl == &HGLOBALLockBytesImpl_Vtbl) {
191 *phglobal = pMemLockBytes->supportHandle;
192 if (*phglobal == 0)
193 return E_INVALIDARG;
194 return S_OK;
196 /* It is not our lockbytes implementation, so use a more generic way */
197 hres = ILockBytes_Stat(plkbyt,&stbuf,0);
198 if (hres != S_OK) {
199 ERR("Cannot ILockBytes_Stat, %lx\n",hres);
200 return hres;
202 FIXME("cbSize is %ld\n",stbuf.cbSize.u.LowPart);
203 *phglobal = GlobalAlloc( GMEM_MOVEABLE|GMEM_SHARE, stbuf.cbSize.u.LowPart);
204 if (!*phglobal)
205 return E_INVALIDARG;
206 memset(&start,0,sizeof(start));
207 hres = ILockBytes_ReadAt(plkbyt, start, GlobalLock(*phglobal), stbuf.cbSize.u.LowPart, &xread);
208 GlobalUnlock(*phglobal);
209 if (hres != S_OK) {
210 FIXME("%p->ReadAt failed with %lx\n",plkbyt,hres);
211 return hres;
213 if (stbuf.cbSize.u.LowPart != xread) {
214 FIXME("Read size is not requested size %ld vs %ld?\n",stbuf.cbSize.u.LowPart, xread);
216 return S_OK;
219 /******************************************************************************
221 * HGLOBALLockBytesImpl implementation
225 /******************************************************************************
226 * This is the constructor for the HGLOBALLockBytesImpl class.
228 * Params:
229 * hGlobal - Handle that will support the stream. can be NULL.
230 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
231 * when the IStream object is destroyed.
233 HGLOBALLockBytesImpl* HGLOBALLockBytesImpl_Construct(HGLOBAL hGlobal,
234 BOOL fDeleteOnRelease)
236 HGLOBALLockBytesImpl* newLockBytes;
237 newLockBytes = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALLockBytesImpl));
239 if (newLockBytes!=0)
242 * Set up the virtual function table and reference count.
244 newLockBytes->lpVtbl = &HGLOBALLockBytesImpl_Vtbl;
245 newLockBytes->ref = 0;
248 * Initialize the support.
250 newLockBytes->supportHandle = hGlobal;
251 newLockBytes->deleteOnRelease = fDeleteOnRelease;
254 * This method will allocate a handle if one is not supplied.
256 if (newLockBytes->supportHandle == 0)
258 newLockBytes->supportHandle = GlobalAlloc(GMEM_MOVEABLE |
259 GMEM_NODISCARD,
264 * Initialize the size of the array to the size of the handle.
266 newLockBytes->byteArraySize.u.HighPart = 0;
267 newLockBytes->byteArraySize.u.LowPart = GlobalSize(
268 newLockBytes->supportHandle);
271 return newLockBytes;
274 /******************************************************************************
275 * This is the destructor of the HGLOBALStreamImpl class.
277 * This method will clean-up all the resources used-up by the given
278 * HGLOBALLockBytesImpl class. The pointer passed-in to this function will be
279 * freed and will not be valid anymore.
281 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This)
284 * Release the HGlobal if the constructor asked for that.
286 if (This->deleteOnRelease)
288 GlobalFree(This->supportHandle);
289 This->supportHandle = 0;
293 * Finally, free the memory used-up by the class.
295 HeapFree(GetProcessHeap(), 0, This);
298 /******************************************************************************
299 * This implements the IUnknown method QueryInterface for this
300 * class
302 HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(
303 ILockBytes* iface,
304 REFIID riid, /* [in] */
305 void** ppvObject) /* [iid_is][out] */
307 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
310 * Perform a sanity check on the parameters.
312 if (ppvObject==0)
313 return E_INVALIDARG;
316 * Initialize the return parameter.
318 *ppvObject = 0;
321 * Compare the riid with the interface IDs implemented by this object.
323 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
325 *ppvObject = (ILockBytes*)This;
327 else if (memcmp(&IID_ILockBytes, riid, sizeof(IID_ILockBytes)) == 0)
329 *ppvObject = (ILockBytes*)This;
333 * Check that we obtained an interface.
335 if ((*ppvObject)==0)
336 return E_NOINTERFACE;
339 * Query Interface always increases the reference count by one when it is
340 * successful
342 HGLOBALLockBytesImpl_AddRef(iface);
344 return S_OK;
347 /******************************************************************************
348 * This implements the IUnknown method AddRef for this
349 * class
351 ULONG WINAPI HGLOBALLockBytesImpl_AddRef(ILockBytes* iface)
353 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
355 This->ref++;
357 return This->ref;
360 /******************************************************************************
361 * This implements the IUnknown method Release for this
362 * class
364 ULONG WINAPI HGLOBALLockBytesImpl_Release(ILockBytes* iface)
366 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
368 ULONG newRef;
370 This->ref--;
372 newRef = This->ref;
375 * If the reference count goes down to 0, perform suicide.
377 if (newRef==0)
379 HGLOBALLockBytesImpl_Destroy(This);
382 return newRef;
385 /******************************************************************************
386 * This method is part of the ILockBytes interface.
388 * It reads a block of information from the byte array at the specified
389 * offset.
391 * See the documentation of ILockBytes for more info.
393 HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(
394 ILockBytes* iface,
395 ULARGE_INTEGER ulOffset, /* [in] */
396 void* pv, /* [length_is][size_is][out] */
397 ULONG cb, /* [in] */
398 ULONG* pcbRead) /* [out] */
400 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
402 void* supportBuffer;
403 ULONG bytesReadBuffer = 0;
404 ULONG bytesToReadFromBuffer;
407 * If the caller is not interested in the number of bytes read,
408 * we use another buffer to avoid "if" statements in the code.
410 if (pcbRead == 0)
411 pcbRead = &bytesReadBuffer;
414 * Make sure the offset is valid.
416 if (ulOffset.u.LowPart > This->byteArraySize.u.LowPart)
417 return E_FAIL;
420 * Using the known size of the array, calculate the number of bytes
421 * to read.
423 bytesToReadFromBuffer = min(This->byteArraySize.u.LowPart -
424 ulOffset.u.LowPart, cb);
427 * Lock the buffer in position and copy the data.
429 supportBuffer = GlobalLock(This->supportHandle);
431 memcpy(pv,
432 (char *) supportBuffer + ulOffset.u.LowPart,
433 bytesToReadFromBuffer);
436 * Return the number of bytes read.
438 *pcbRead = bytesToReadFromBuffer;
441 * Cleanup
443 GlobalUnlock(This->supportHandle);
446 * The function returns S_OK if the specified number of bytes were read
447 * or the end of the array was reached.
448 * It returns STG_E_READFAULT if the number of bytes to read does not equal
449 * the number of bytes actually read.
451 if(*pcbRead == cb)
452 return S_OK;
454 return STG_E_READFAULT;
457 /******************************************************************************
458 * This method is part of the ILockBytes interface.
460 * It writes the specified bytes at the specified offset.
461 * position. If the array is too small, it will be resized.
463 * See the documentation of ILockBytes for more info.
465 HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt(
466 ILockBytes* iface,
467 ULARGE_INTEGER ulOffset, /* [in] */
468 const void* pv, /* [size_is][in] */
469 ULONG cb, /* [in] */
470 ULONG* pcbWritten) /* [out] */
472 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
474 void* supportBuffer;
475 ULARGE_INTEGER newSize;
476 ULONG bytesWritten = 0;
479 * If the caller is not interested in the number of bytes written,
480 * we use another buffer to avoid "if" statements in the code.
482 if (pcbWritten == 0)
483 pcbWritten = &bytesWritten;
485 if (cb == 0)
487 return S_OK;
489 else
491 newSize.u.HighPart = 0;
492 newSize.u.LowPart = ulOffset.u.LowPart + cb;
496 * Verify if we need to grow the stream
498 if (newSize.u.LowPart > This->byteArraySize.u.LowPart)
500 /* grow stream */
501 if (HGLOBALLockBytesImpl_SetSize(iface, newSize) == STG_E_MEDIUMFULL)
502 return STG_E_MEDIUMFULL;
506 * Lock the buffer in position and copy the data.
508 supportBuffer = GlobalLock(This->supportHandle);
510 memcpy((char *) supportBuffer + ulOffset.u.LowPart, pv, cb);
513 * Return the number of bytes written.
515 *pcbWritten = cb;
518 * Cleanup
520 GlobalUnlock(This->supportHandle);
522 return S_OK;
525 /******************************************************************************
526 * This method is part of the ILockBytes interface.
528 * See the documentation of ILockBytes for more info.
530 HRESULT WINAPI HGLOBALLockBytesImpl_Flush(ILockBytes* iface)
532 return S_OK;
535 /******************************************************************************
536 * This method is part of the ILockBytes interface.
538 * It will change the size of the byte array.
540 * See the documentation of ILockBytes for more info.
542 HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(
543 ILockBytes* iface,
544 ULARGE_INTEGER libNewSize) /* [in] */
546 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
547 HGLOBAL supportHandle;
550 * As documented.
552 if (libNewSize.u.HighPart != 0)
553 return STG_E_INVALIDFUNCTION;
555 if (This->byteArraySize.u.LowPart == libNewSize.u.LowPart)
556 return S_OK;
559 * Re allocate the HGlobal to fit the new size of the stream.
561 supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
563 if (supportHandle == 0)
564 return STG_E_MEDIUMFULL;
566 This->supportHandle = supportHandle;
567 This->byteArraySize.u.LowPart = libNewSize.u.LowPart;
569 return S_OK;
572 /******************************************************************************
573 * This method is part of the ILockBytes interface.
575 * The global memory implementation of ILockBytes does not support locking.
577 * See the documentation of ILockBytes for more info.
579 HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion(
580 ILockBytes* iface,
581 ULARGE_INTEGER libOffset, /* [in] */
582 ULARGE_INTEGER cb, /* [in] */
583 DWORD dwLockType) /* [in] */
585 return STG_E_INVALIDFUNCTION;
588 /******************************************************************************
589 * This method is part of the ILockBytes interface.
591 * The global memory implementation of ILockBytes does not support locking.
593 * See the documentation of ILockBytes for more info.
595 HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion(
596 ILockBytes* iface,
597 ULARGE_INTEGER libOffset, /* [in] */
598 ULARGE_INTEGER cb, /* [in] */
599 DWORD dwLockType) /* [in] */
601 return STG_E_INVALIDFUNCTION;
604 /******************************************************************************
605 * This method is part of the ILockBytes interface.
607 * This method returns information about the current
608 * byte array object.
610 * See the documentation of ILockBytes for more info.
612 HRESULT WINAPI HGLOBALLockBytesImpl_Stat(
613 ILockBytes* iface,
614 STATSTG* pstatstg, /* [out] */
615 DWORD grfStatFlag) /* [in] */
617 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
619 memset(pstatstg, 0, sizeof(STATSTG));
621 pstatstg->pwcsName = NULL;
622 pstatstg->type = STGTY_LOCKBYTES;
623 pstatstg->cbSize = This->byteArraySize;
625 return S_OK;