opengl: Add support for rendering on bitmaps.
[wine/wine64.git] / dlls / ole32 / memlockbytes.c
blob2b5503475d76861cc332973b85c12253b02a8d62
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 COBJMACROS
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winuser.h"
35 #include "objbase.h"
36 #include "ole2.h"
37 #include "winerror.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
43 /******************************************************************************
44 * HGLOBALLockBytesImpl definition.
46 * This class imlements the ILockBytes inteface and represents a byte array
47 * object supported by an HGLOBAL pointer.
49 struct HGLOBALLockBytesImpl
52 * Needs to be the first item in the struct
53 * since we want to cast this in an ILockBytes pointer
55 const ILockBytesVtbl *lpVtbl;
58 * Reference count
60 LONG ref;
63 * Support for the LockBytes object
65 HGLOBAL supportHandle;
68 * This flag is TRUE if the HGLOBAL is destroyed when the object
69 * is finally released.
71 BOOL deleteOnRelease;
74 * Helper variable that contains the size of the byte array
76 ULARGE_INTEGER byteArraySize;
79 typedef struct HGLOBALLockBytesImpl HGLOBALLockBytesImpl;
82 * Method definition for the HGLOBALLockBytesImpl class.
84 HGLOBALLockBytesImpl* HGLOBALLockBytesImpl_Construct(
85 HGLOBAL hGlobal,
86 BOOL fDeleteOnRelease);
88 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This);
90 HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(
91 ILockBytes* iface,
92 REFIID riid, /* [in] */
93 void** ppvObject); /* [iid_is][out] */
95 ULONG WINAPI HGLOBALLockBytesImpl_AddRef(
96 ILockBytes* iface);
98 ULONG WINAPI HGLOBALLockBytesImpl_Release(
99 ILockBytes* iface);
101 HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(
102 ILockBytes* iface,
103 ULARGE_INTEGER ulOffset, /* [in] */
104 void* pv, /* [length_is][size_is][out] */
105 ULONG cb, /* [in] */
106 ULONG* pcbRead); /* [out] */
108 HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt(
109 ILockBytes* iface,
110 ULARGE_INTEGER ulOffset, /* [in] */
111 const void* pv, /* [size_is][in] */
112 ULONG cb, /* [in] */
113 ULONG* pcbWritten); /* [out] */
115 HRESULT WINAPI HGLOBALLockBytesImpl_Flush(
116 ILockBytes* iface);
118 HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(
119 ILockBytes* iface,
120 ULARGE_INTEGER libNewSize); /* [in] */
122 HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion(
123 ILockBytes* iface,
124 ULARGE_INTEGER libOffset, /* [in] */
125 ULARGE_INTEGER cb, /* [in] */
126 DWORD dwLockType); /* [in] */
128 HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion(
129 ILockBytes* iface,
130 ULARGE_INTEGER libOffset, /* [in] */
131 ULARGE_INTEGER cb, /* [in] */
132 DWORD dwLockType); /* [in] */
134 HRESULT WINAPI HGLOBALLockBytesImpl_Stat(
135 ILockBytes* iface,
136 STATSTG* pstatstg, /* [out] */
137 DWORD grfStatFlag); /* [in] */
140 * Virtual function table for the HGLOBALLockBytesImpl class.
142 static const ILockBytesVtbl HGLOBALLockBytesImpl_Vtbl =
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.@]
159 * Create a byte array object which is intended to be the compound file foundation.
160 * This object supports a COM implementation of the ILockBytes interface.
162 * PARAMS
163 * hGlobal [ I] Global memory handle
164 * fDeleteOnRelease [ I] Whether the handle should be freed when the object is released.
165 * ppLkbyt [ O] Address of ILockBytes pointer that receives
166 * the interface pointer to the new byte array object.
168 * RETURNS
169 * Success: S_OK
171 * NOTES
172 * The supplied ILockBytes pointer can be used by the StgCreateDocfileOnILockBytes
173 * function to build a compound file on top of this byte array object.
174 * The ILockBytes interface instance calls the GlobalReAlloc function to grow
175 * the memory block as required.
177 HRESULT WINAPI CreateILockBytesOnHGlobal(HGLOBAL hGlobal,
178 BOOL fDeleteOnRelease,
179 LPLOCKBYTES* ppLkbyt)
181 HGLOBALLockBytesImpl* newLockBytes;
183 newLockBytes = HGLOBALLockBytesImpl_Construct(hGlobal, fDeleteOnRelease);
185 if (newLockBytes != NULL)
187 return IUnknown_QueryInterface((IUnknown*)newLockBytes,
188 &IID_ILockBytes,
189 (void**)ppLkbyt);
192 return E_OUTOFMEMORY;
195 /******************************************************************************
196 * GetHGlobalFromILockBytes [OLE32.@]
198 * Retrieve a global memory handle to a byte array object created
199 * using the CreateILockBytesOnHGlobal function.
201 * PARAMS
202 * plkbyt [ I] Pointer to the ILockBytes interface on byte array object
203 * phglobal [ O] Address to store a global memory handle
204 * RETURNS
205 * S_OK if *phglobal has a correct value
206 * E_INVALIDARG if any parameters are invalid
209 HRESULT WINAPI GetHGlobalFromILockBytes(ILockBytes* plkbyt, HGLOBAL* phglobal)
211 HGLOBALLockBytesImpl* const pMemLockBytes = (HGLOBALLockBytesImpl*)plkbyt;
212 STATSTG stbuf;
213 HRESULT hres;
214 ULARGE_INTEGER start;
215 ULONG xread;
217 *phglobal = 0;
218 if (pMemLockBytes->lpVtbl == &HGLOBALLockBytesImpl_Vtbl) {
219 *phglobal = pMemLockBytes->supportHandle;
220 if (*phglobal == 0)
221 return E_INVALIDARG;
222 return S_OK;
224 /* It is not our lockbytes implementation, so use a more generic way */
225 hres = ILockBytes_Stat(plkbyt,&stbuf,0);
226 if (hres != S_OK) {
227 ERR("Cannot ILockBytes_Stat, %lx\n",hres);
228 return hres;
230 FIXME("cbSize is %ld\n",stbuf.cbSize.u.LowPart);
231 *phglobal = GlobalAlloc( GMEM_MOVEABLE|GMEM_SHARE, stbuf.cbSize.u.LowPart);
232 if (!*phglobal)
233 return E_INVALIDARG;
234 memset(&start,0,sizeof(start));
235 hres = ILockBytes_ReadAt(plkbyt, start, GlobalLock(*phglobal), stbuf.cbSize.u.LowPart, &xread);
236 GlobalUnlock(*phglobal);
237 if (hres != S_OK) {
238 FIXME("%p->ReadAt failed with %lx\n",plkbyt,hres);
239 return hres;
241 if (stbuf.cbSize.u.LowPart != xread) {
242 FIXME("Read size is not requested size %ld vs %ld?\n",stbuf.cbSize.u.LowPart, xread);
244 return S_OK;
247 /******************************************************************************
249 * HGLOBALLockBytesImpl implementation
253 /******************************************************************************
254 * This is the constructor for the HGLOBALLockBytesImpl class.
256 * PARAMS
257 * hGlobal [ I] Handle that will support the stream. can be NULL.
258 * fDeleteOnRelease [ I] Flag set to TRUE if the HGLOBAL will be released
259 * when the IStream object is destroyed.
261 HGLOBALLockBytesImpl* HGLOBALLockBytesImpl_Construct(HGLOBAL hGlobal,
262 BOOL fDeleteOnRelease)
264 HGLOBALLockBytesImpl* newLockBytes;
265 newLockBytes = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALLockBytesImpl));
267 if (newLockBytes!=0)
270 * Set up the virtual function table and reference count.
272 newLockBytes->lpVtbl = &HGLOBALLockBytesImpl_Vtbl;
273 newLockBytes->ref = 0;
276 * Initialize the support.
278 newLockBytes->supportHandle = hGlobal;
279 newLockBytes->deleteOnRelease = fDeleteOnRelease;
282 * This method will allocate a handle if one is not supplied.
284 if (newLockBytes->supportHandle == 0)
286 newLockBytes->supportHandle = GlobalAlloc(GMEM_MOVEABLE |
287 GMEM_NODISCARD,
292 * Initialize the size of the array to the size of the handle.
294 newLockBytes->byteArraySize.u.HighPart = 0;
295 newLockBytes->byteArraySize.u.LowPart = GlobalSize(
296 newLockBytes->supportHandle);
299 return newLockBytes;
302 /******************************************************************************
303 * This is the destructor of the HGLOBALStreamImpl class.
305 * This method will clean-up all the resources used-up by the given
306 * HGLOBALLockBytesImpl class. The pointer passed-in to this function will be
307 * freed and will not be valid anymore.
309 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This)
312 * Release the HGlobal if the constructor asked for that.
314 if (This->deleteOnRelease)
316 GlobalFree(This->supportHandle);
317 This->supportHandle = 0;
321 * Finally, free the memory used-up by the class.
323 HeapFree(GetProcessHeap(), 0, This);
326 /******************************************************************************
327 * This implements the IUnknown method QueryInterface for this
328 * class
330 HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(
331 ILockBytes* iface,
332 REFIID riid, /* [in] */
333 void** ppvObject) /* [iid_is][out] */
335 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
338 * Perform a sanity check on the parameters.
340 if (ppvObject==0)
341 return E_INVALIDARG;
344 * Initialize the return parameter.
346 *ppvObject = 0;
349 * Compare the riid with the interface IDs implemented by this object.
351 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
353 *ppvObject = (ILockBytes*)This;
355 else if (memcmp(&IID_ILockBytes, riid, sizeof(IID_ILockBytes)) == 0)
357 *ppvObject = (ILockBytes*)This;
361 * Check that we obtained an interface.
363 if ((*ppvObject)==0)
364 return E_NOINTERFACE;
367 * Query Interface always increases the reference count by one when it is
368 * successful
370 HGLOBALLockBytesImpl_AddRef(iface);
372 return S_OK;
375 /******************************************************************************
376 * This implements the IUnknown method AddRef for this
377 * class
379 ULONG WINAPI HGLOBALLockBytesImpl_AddRef(ILockBytes* iface)
381 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
382 return InterlockedIncrement(&This->ref);
385 /******************************************************************************
386 * This implements the IUnknown method Release for this
387 * class
389 ULONG WINAPI HGLOBALLockBytesImpl_Release(ILockBytes* iface)
391 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
392 ULONG ref;
394 ref = InterlockedDecrement(&This->ref);
397 * If the reference count goes down to 0, perform suicide.
399 if (ref==0)
401 HGLOBALLockBytesImpl_Destroy(This);
404 return ref;
407 /******************************************************************************
408 * This method is part of the ILockBytes interface.
410 * It reads a block of information from the byte array at the specified
411 * offset.
413 * See the documentation of ILockBytes for more info.
415 HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(
416 ILockBytes* iface,
417 ULARGE_INTEGER ulOffset, /* [in] */
418 void* pv, /* [length_is][size_is][out] */
419 ULONG cb, /* [in] */
420 ULONG* pcbRead) /* [out] */
422 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
424 void* supportBuffer;
425 ULONG bytesReadBuffer = 0;
426 ULONG bytesToReadFromBuffer;
429 * If the caller is not interested in the number of bytes read,
430 * we use another buffer to avoid "if" statements in the code.
432 if (pcbRead == 0)
433 pcbRead = &bytesReadBuffer;
436 * Make sure the offset is valid.
438 if (ulOffset.u.LowPart > This->byteArraySize.u.LowPart)
439 return E_FAIL;
442 * Using the known size of the array, calculate the number of bytes
443 * to read.
445 bytesToReadFromBuffer = min(This->byteArraySize.u.LowPart -
446 ulOffset.u.LowPart, cb);
449 * Lock the buffer in position and copy the data.
451 supportBuffer = GlobalLock(This->supportHandle);
453 memcpy(pv,
454 (char *) supportBuffer + ulOffset.u.LowPart,
455 bytesToReadFromBuffer);
458 * Return the number of bytes read.
460 *pcbRead = bytesToReadFromBuffer;
463 * Cleanup
465 GlobalUnlock(This->supportHandle);
468 * The function returns S_OK if the specified number of bytes were read
469 * or the end of the array was reached.
470 * It returns STG_E_READFAULT if the number of bytes to read does not equal
471 * the number of bytes actually read.
473 if(*pcbRead == cb)
474 return S_OK;
476 return STG_E_READFAULT;
479 /******************************************************************************
480 * This method is part of the ILockBytes interface.
482 * It writes the specified bytes at the specified offset.
483 * position. If the array is too small, it will be resized.
485 * See the documentation of ILockBytes for more info.
487 HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt(
488 ILockBytes* iface,
489 ULARGE_INTEGER ulOffset, /* [in] */
490 const void* pv, /* [size_is][in] */
491 ULONG cb, /* [in] */
492 ULONG* pcbWritten) /* [out] */
494 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
496 void* supportBuffer;
497 ULARGE_INTEGER newSize;
498 ULONG bytesWritten = 0;
501 * If the caller is not interested in the number of bytes written,
502 * we use another buffer to avoid "if" statements in the code.
504 if (pcbWritten == 0)
505 pcbWritten = &bytesWritten;
507 if (cb == 0)
509 return S_OK;
511 else
513 newSize.u.HighPart = 0;
514 newSize.u.LowPart = ulOffset.u.LowPart + cb;
518 * Verify if we need to grow the stream
520 if (newSize.u.LowPart > This->byteArraySize.u.LowPart)
522 /* grow stream */
523 if (HGLOBALLockBytesImpl_SetSize(iface, newSize) == STG_E_MEDIUMFULL)
524 return STG_E_MEDIUMFULL;
528 * Lock the buffer in position and copy the data.
530 supportBuffer = GlobalLock(This->supportHandle);
532 memcpy((char *) supportBuffer + ulOffset.u.LowPart, pv, cb);
535 * Return the number of bytes written.
537 *pcbWritten = cb;
540 * Cleanup
542 GlobalUnlock(This->supportHandle);
544 return S_OK;
547 /******************************************************************************
548 * This method is part of the ILockBytes interface.
550 * See the documentation of ILockBytes for more info.
552 HRESULT WINAPI HGLOBALLockBytesImpl_Flush(ILockBytes* iface)
554 return S_OK;
557 /******************************************************************************
558 * This method is part of the ILockBytes interface.
560 * It will change the size of the byte array.
562 * See the documentation of ILockBytes for more info.
564 HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(
565 ILockBytes* iface,
566 ULARGE_INTEGER libNewSize) /* [in] */
568 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
569 HGLOBAL supportHandle;
572 * As documented.
574 if (libNewSize.u.HighPart != 0)
575 return STG_E_INVALIDFUNCTION;
577 if (This->byteArraySize.u.LowPart == libNewSize.u.LowPart)
578 return S_OK;
581 * Re allocate the HGlobal to fit the new size of the stream.
583 supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
585 if (supportHandle == 0)
586 return STG_E_MEDIUMFULL;
588 This->supportHandle = supportHandle;
589 This->byteArraySize.u.LowPart = libNewSize.u.LowPart;
591 return S_OK;
594 /******************************************************************************
595 * This method is part of the ILockBytes interface.
597 * The global memory implementation of ILockBytes does not support locking.
599 * See the documentation of ILockBytes for more info.
601 HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion(
602 ILockBytes* iface,
603 ULARGE_INTEGER libOffset, /* [in] */
604 ULARGE_INTEGER cb, /* [in] */
605 DWORD dwLockType) /* [in] */
607 return STG_E_INVALIDFUNCTION;
610 /******************************************************************************
611 * This method is part of the ILockBytes interface.
613 * The global memory implementation of ILockBytes does not support locking.
615 * See the documentation of ILockBytes for more info.
617 HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion(
618 ILockBytes* iface,
619 ULARGE_INTEGER libOffset, /* [in] */
620 ULARGE_INTEGER cb, /* [in] */
621 DWORD dwLockType) /* [in] */
623 return STG_E_INVALIDFUNCTION;
626 /******************************************************************************
627 * This method is part of the ILockBytes interface.
629 * This method returns information about the current
630 * byte array object.
632 * See the documentation of ILockBytes for more info.
634 HRESULT WINAPI HGLOBALLockBytesImpl_Stat(
635 ILockBytes* iface,
636 STATSTG* pstatstg, /* [out] */
637 DWORD grfStatFlag) /* [in] */
639 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
641 memset(pstatstg, 0, sizeof(STATSTG));
643 pstatstg->pwcsName = NULL;
644 pstatstg->type = STGTY_LOCKBYTES;
645 pstatstg->cbSize = This->byteArraySize;
647 return S_OK;