winedos: Make sure that the main message queue exists before the DOS thread starts.
[wine.git] / dlls / ole32 / memlockbytes.c
blob57c5b0bfa4f301785dd37ae27ddebd05bf1c4cbc
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 implements the ILockBytes interface 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 static HGLOBALLockBytesImpl* HGLOBALLockBytesImpl_Construct(
85 HGLOBAL hGlobal,
86 BOOL fDeleteOnRelease);
88 static void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This);
90 static HRESULT WINAPI HGLOBALLockBytesImpl_SetSize( ILockBytes* iface, ULARGE_INTEGER libNewSize );
92 static const ILockBytesVtbl HGLOBALLockBytesImpl_Vtbl;
94 /******************************************************************************
95 * CreateILockBytesOnHGlobal [OLE32.@]
97 * Create a byte array object which is intended to be the compound file foundation.
98 * This object supports a COM implementation of the ILockBytes interface.
100 * PARAMS
101 * hGlobal [ I] Global memory handle
102 * fDeleteOnRelease [ I] Whether the handle should be freed when the object is released.
103 * ppLkbyt [ O] Address of ILockBytes pointer that receives
104 * the interface pointer to the new byte array object.
106 * RETURNS
107 * Success: S_OK
109 * NOTES
110 * The supplied ILockBytes pointer can be used by the StgCreateDocfileOnILockBytes
111 * function to build a compound file on top of this byte array object.
112 * The ILockBytes interface instance calls the GlobalReAlloc function to grow
113 * the memory block as required.
115 HRESULT WINAPI CreateILockBytesOnHGlobal(HGLOBAL hGlobal,
116 BOOL fDeleteOnRelease,
117 LPLOCKBYTES* ppLkbyt)
119 HGLOBALLockBytesImpl* newLockBytes;
121 newLockBytes = HGLOBALLockBytesImpl_Construct(hGlobal, fDeleteOnRelease);
123 if (newLockBytes != NULL)
125 return IUnknown_QueryInterface((IUnknown*)newLockBytes,
126 &IID_ILockBytes,
127 (void**)ppLkbyt);
130 return E_OUTOFMEMORY;
133 /******************************************************************************
134 * GetHGlobalFromILockBytes [OLE32.@]
136 * Retrieve a global memory handle to a byte array object created
137 * using the CreateILockBytesOnHGlobal function.
139 * PARAMS
140 * plkbyt [ I] Pointer to the ILockBytes interface on byte array object
141 * phglobal [ O] Address to store a global memory handle
142 * RETURNS
143 * S_OK if *phglobal has a correct value
144 * E_INVALIDARG if any parameters are invalid
147 HRESULT WINAPI GetHGlobalFromILockBytes(ILockBytes* plkbyt, HGLOBAL* phglobal)
149 HGLOBALLockBytesImpl* const pMemLockBytes = (HGLOBALLockBytesImpl*)plkbyt;
150 STATSTG stbuf;
151 HRESULT hres;
152 ULARGE_INTEGER start;
153 ULONG xread;
155 *phglobal = 0;
156 if (pMemLockBytes->lpVtbl == &HGLOBALLockBytesImpl_Vtbl) {
157 *phglobal = pMemLockBytes->supportHandle;
158 if (*phglobal == 0)
159 return E_INVALIDARG;
160 return S_OK;
162 /* It is not our lockbytes implementation, so use a more generic way */
163 hres = ILockBytes_Stat(plkbyt,&stbuf,0);
164 if (hres != S_OK) {
165 ERR("Cannot ILockBytes_Stat, %x\n",hres);
166 return hres;
168 FIXME("cbSize is %d\n",stbuf.cbSize.u.LowPart);
169 *phglobal = GlobalAlloc( GMEM_MOVEABLE|GMEM_SHARE, stbuf.cbSize.u.LowPart);
170 if (!*phglobal)
171 return E_INVALIDARG;
172 memset(&start,0,sizeof(start));
173 hres = ILockBytes_ReadAt(plkbyt, start, GlobalLock(*phglobal), stbuf.cbSize.u.LowPart, &xread);
174 GlobalUnlock(*phglobal);
175 if (hres != S_OK) {
176 FIXME("%p->ReadAt failed with %x\n",plkbyt,hres);
177 return hres;
179 if (stbuf.cbSize.u.LowPart != xread) {
180 FIXME("Read size is not requested size %d vs %d?\n",stbuf.cbSize.u.LowPart, xread);
182 return S_OK;
185 /******************************************************************************
187 * HGLOBALLockBytesImpl implementation
191 /******************************************************************************
192 * This is the constructor for the HGLOBALLockBytesImpl class.
194 * PARAMS
195 * hGlobal [ I] Handle that will support the stream. can be NULL.
196 * fDeleteOnRelease [ I] Flag set to TRUE if the HGLOBAL will be released
197 * when the IStream object is destroyed.
199 static HGLOBALLockBytesImpl* HGLOBALLockBytesImpl_Construct(HGLOBAL hGlobal,
200 BOOL fDeleteOnRelease)
202 HGLOBALLockBytesImpl* newLockBytes;
203 newLockBytes = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALLockBytesImpl));
205 if (newLockBytes!=0)
208 * Set up the virtual function table and reference count.
210 newLockBytes->lpVtbl = &HGLOBALLockBytesImpl_Vtbl;
211 newLockBytes->ref = 0;
214 * Initialize the support.
216 newLockBytes->supportHandle = hGlobal;
217 newLockBytes->deleteOnRelease = fDeleteOnRelease;
220 * This method will allocate a handle if one is not supplied.
222 if (newLockBytes->supportHandle == 0)
224 newLockBytes->supportHandle = GlobalAlloc(GMEM_MOVEABLE |
225 GMEM_NODISCARD,
230 * Initialize the size of the array to the size of the handle.
232 newLockBytes->byteArraySize.u.HighPart = 0;
233 newLockBytes->byteArraySize.u.LowPart = GlobalSize(
234 newLockBytes->supportHandle);
237 return newLockBytes;
240 /******************************************************************************
241 * This is the destructor of the HGLOBALStreamImpl class.
243 * This method will clean-up all the resources used-up by the given
244 * HGLOBALLockBytesImpl class. The pointer passed-in to this function will be
245 * freed and will not be valid anymore.
247 static void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This)
250 * Release the HGlobal if the constructor asked for that.
252 if (This->deleteOnRelease)
254 GlobalFree(This->supportHandle);
255 This->supportHandle = 0;
259 * Finally, free the memory used-up by the class.
261 HeapFree(GetProcessHeap(), 0, This);
264 /******************************************************************************
265 * This implements the IUnknown method QueryInterface for this
266 * class
268 static HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(
269 ILockBytes* iface,
270 REFIID riid, /* [in] */
271 void** ppvObject) /* [iid_is][out] */
273 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
276 * Perform a sanity check on the parameters.
278 if (ppvObject==0)
279 return E_INVALIDARG;
282 * Initialize the return parameter.
284 *ppvObject = 0;
287 * Compare the riid with the interface IDs implemented by this object.
289 if (IsEqualIID(riid, &IID_IUnknown) ||
290 IsEqualIID(riid, &IID_ILockBytes))
292 *ppvObject = (ILockBytes*)This;
296 * Check that we obtained an interface.
298 if ((*ppvObject)==0)
299 return E_NOINTERFACE;
302 * Query Interface always increases the reference count by one when it is
303 * successful
305 IUnknown_AddRef(iface);
307 return S_OK;
310 /******************************************************************************
311 * This implements the IUnknown method AddRef for this
312 * class
314 static ULONG WINAPI HGLOBALLockBytesImpl_AddRef(ILockBytes* iface)
316 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
317 return InterlockedIncrement(&This->ref);
320 /******************************************************************************
321 * This implements the IUnknown method Release for this
322 * class
324 static ULONG WINAPI HGLOBALLockBytesImpl_Release(ILockBytes* iface)
326 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
327 ULONG ref;
329 ref = InterlockedDecrement(&This->ref);
332 * If the reference count goes down to 0, perform suicide.
334 if (ref==0)
336 HGLOBALLockBytesImpl_Destroy(This);
339 return ref;
342 /******************************************************************************
343 * This method is part of the ILockBytes interface.
345 * It reads a block of information from the byte array at the specified
346 * offset.
348 * See the documentation of ILockBytes for more info.
350 static HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(
351 ILockBytes* iface,
352 ULARGE_INTEGER ulOffset, /* [in] */
353 void* pv, /* [length_is][size_is][out] */
354 ULONG cb, /* [in] */
355 ULONG* pcbRead) /* [out] */
357 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
359 void* supportBuffer;
360 ULONG bytesReadBuffer = 0;
361 ULONG bytesToReadFromBuffer;
364 * If the caller is not interested in the number of bytes read,
365 * we use another buffer to avoid "if" statements in the code.
367 if (pcbRead == 0)
368 pcbRead = &bytesReadBuffer;
371 * Make sure the offset is valid.
373 if (ulOffset.u.LowPart > This->byteArraySize.u.LowPart)
374 return E_FAIL;
377 * Using the known size of the array, calculate the number of bytes
378 * to read.
380 bytesToReadFromBuffer = min(This->byteArraySize.u.LowPart -
381 ulOffset.u.LowPart, cb);
384 * Lock the buffer in position and copy the data.
386 supportBuffer = GlobalLock(This->supportHandle);
388 memcpy(pv,
389 (char *) supportBuffer + ulOffset.u.LowPart,
390 bytesToReadFromBuffer);
393 * Return the number of bytes read.
395 *pcbRead = bytesToReadFromBuffer;
398 * Cleanup
400 GlobalUnlock(This->supportHandle);
403 * The function returns S_OK if the specified number of bytes were read
404 * or the end of the array was reached.
405 * It returns STG_E_READFAULT if the number of bytes to read does not equal
406 * the number of bytes actually read.
408 if(*pcbRead == cb)
409 return S_OK;
411 return STG_E_READFAULT;
414 /******************************************************************************
415 * This method is part of the ILockBytes interface.
417 * It writes the specified bytes at the specified offset.
418 * position. If the array is too small, it will be resized.
420 * See the documentation of ILockBytes for more info.
422 static HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt(
423 ILockBytes* iface,
424 ULARGE_INTEGER ulOffset, /* [in] */
425 const void* pv, /* [size_is][in] */
426 ULONG cb, /* [in] */
427 ULONG* pcbWritten) /* [out] */
429 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
431 void* supportBuffer;
432 ULARGE_INTEGER newSize;
433 ULONG bytesWritten = 0;
436 * If the caller is not interested in the number of bytes written,
437 * we use another buffer to avoid "if" statements in the code.
439 if (pcbWritten == 0)
440 pcbWritten = &bytesWritten;
442 if (cb == 0)
444 return S_OK;
446 else
448 newSize.u.HighPart = 0;
449 newSize.u.LowPart = ulOffset.u.LowPart + cb;
453 * Verify if we need to grow the stream
455 if (newSize.u.LowPart > This->byteArraySize.u.LowPart)
457 /* grow stream */
458 if (HGLOBALLockBytesImpl_SetSize(iface, newSize) == STG_E_MEDIUMFULL)
459 return STG_E_MEDIUMFULL;
463 * Lock the buffer in position and copy the data.
465 supportBuffer = GlobalLock(This->supportHandle);
467 memcpy((char *) supportBuffer + ulOffset.u.LowPart, pv, cb);
470 * Return the number of bytes written.
472 *pcbWritten = cb;
475 * Cleanup
477 GlobalUnlock(This->supportHandle);
479 return S_OK;
482 /******************************************************************************
483 * This method is part of the ILockBytes interface.
485 * See the documentation of ILockBytes for more info.
487 static HRESULT WINAPI HGLOBALLockBytesImpl_Flush(ILockBytes* iface)
489 return S_OK;
492 /******************************************************************************
493 * This method is part of the ILockBytes interface.
495 * It will change the size of the byte array.
497 * See the documentation of ILockBytes for more info.
499 static HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(
500 ILockBytes* iface,
501 ULARGE_INTEGER libNewSize) /* [in] */
503 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
504 HGLOBAL supportHandle;
507 * As documented.
509 if (libNewSize.u.HighPart != 0)
510 return STG_E_INVALIDFUNCTION;
512 if (This->byteArraySize.u.LowPart == libNewSize.u.LowPart)
513 return S_OK;
516 * Re allocate the HGlobal to fit the new size of the stream.
518 supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
520 if (supportHandle == 0)
521 return STG_E_MEDIUMFULL;
523 This->supportHandle = supportHandle;
524 This->byteArraySize.u.LowPart = libNewSize.u.LowPart;
526 return S_OK;
529 /******************************************************************************
530 * This method is part of the ILockBytes interface.
532 * The global memory implementation of ILockBytes does not support locking.
534 * See the documentation of ILockBytes for more info.
536 static HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion(
537 ILockBytes* iface,
538 ULARGE_INTEGER libOffset, /* [in] */
539 ULARGE_INTEGER cb, /* [in] */
540 DWORD dwLockType) /* [in] */
542 return STG_E_INVALIDFUNCTION;
545 /******************************************************************************
546 * This method is part of the ILockBytes interface.
548 * The global memory implementation of ILockBytes does not support locking.
550 * See the documentation of ILockBytes for more info.
552 static HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion(
553 ILockBytes* iface,
554 ULARGE_INTEGER libOffset, /* [in] */
555 ULARGE_INTEGER cb, /* [in] */
556 DWORD dwLockType) /* [in] */
558 return STG_E_INVALIDFUNCTION;
561 /******************************************************************************
562 * This method is part of the ILockBytes interface.
564 * This method returns information about the current
565 * byte array object.
567 * See the documentation of ILockBytes for more info.
569 static HRESULT WINAPI HGLOBALLockBytesImpl_Stat(
570 ILockBytes* iface,
571 STATSTG* pstatstg, /* [out] */
572 DWORD grfStatFlag) /* [in] */
574 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
576 memset(pstatstg, 0, sizeof(STATSTG));
578 pstatstg->pwcsName = NULL;
579 pstatstg->type = STGTY_LOCKBYTES;
580 pstatstg->cbSize = This->byteArraySize;
582 return S_OK;
586 * Virtual function table for the HGLOBALLockBytesImpl class.
588 static const ILockBytesVtbl HGLOBALLockBytesImpl_Vtbl =
590 HGLOBALLockBytesImpl_QueryInterface,
591 HGLOBALLockBytesImpl_AddRef,
592 HGLOBALLockBytesImpl_Release,
593 HGLOBALLockBytesImpl_ReadAt,
594 HGLOBALLockBytesImpl_WriteAt,
595 HGLOBALLockBytesImpl_Flush,
596 HGLOBALLockBytesImpl_SetSize,
597 HGLOBALLockBytesImpl_LockRegion,
598 HGLOBALLockBytesImpl_UnlockRegion,
599 HGLOBALLockBytesImpl_Stat,