Added Interlocked*Pointer functions.
[wine/hacks.git] / dlls / ole32 / memlockbytes.c
blob0d28dbceefdf4e3fe3affbcd3e8dc78359fc7fe0
1 /******************************************************************************
3 * Global memory implementation of ILockBytes.
5 * Copyright 1999 Thuy Nguyen
7 */
9 #include "config.h"
11 #include <string.h>
13 #include "windef.h"
14 #include "objbase.h"
15 #include "ole2.h"
16 #include "winbase.h"
17 #include "winerror.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(ole);
23 /******************************************************************************
24 * HGLOBALLockBytesImpl definition.
26 * This class imlements the ILockBytes inteface and represents a byte array
27 * object supported by an HGLOBAL pointer.
29 struct HGLOBALLockBytesImpl
32 * Needs to be the first item in the stuct
33 * since we want to cast this in an ILockBytes pointer
35 ICOM_VFIELD(ILockBytes);
38 * Reference count
40 ULONG ref;
43 * Support for the LockBytes object
45 HGLOBAL supportHandle;
48 * This flag is TRUE if the HGLOBAL is destroyed when the object
49 * is finally released.
51 BOOL deleteOnRelease;
54 * Helper variable that contains the size of the byte array
56 ULARGE_INTEGER byteArraySize;
59 typedef struct HGLOBALLockBytesImpl HGLOBALLockBytesImpl;
62 * Method definition for the HGLOBALLockBytesImpl class.
64 HGLOBALLockBytesImpl* HGLOBALLockBytesImpl_Construct(
65 HGLOBAL hGlobal,
66 BOOL fDeleteOnRelease);
68 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This);
70 HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(
71 ILockBytes* iface,
72 REFIID riid, /* [in] */
73 void** ppvObject); /* [iid_is][out] */
75 ULONG WINAPI HGLOBALLockBytesImpl_AddRef(
76 ILockBytes* iface);
78 ULONG WINAPI HGLOBALLockBytesImpl_Release(
79 ILockBytes* iface);
81 HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(
82 ILockBytes* iface,
83 ULARGE_INTEGER ulOffset, /* [in] */
84 void* pv, /* [length_is][size_is][out] */
85 ULONG cb, /* [in] */
86 ULONG* pcbRead); /* [out] */
88 HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt(
89 ILockBytes* iface,
90 ULARGE_INTEGER ulOffset, /* [in] */
91 const void* pv, /* [size_is][in] */
92 ULONG cb, /* [in] */
93 ULONG* pcbWritten); /* [out] */
95 HRESULT WINAPI HGLOBALLockBytesImpl_Flush(
96 ILockBytes* iface);
98 HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(
99 ILockBytes* iface,
100 ULARGE_INTEGER libNewSize); /* [in] */
102 HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion(
103 ILockBytes* iface,
104 ULARGE_INTEGER libOffset, /* [in] */
105 ULARGE_INTEGER cb, /* [in] */
106 DWORD dwLockType); /* [in] */
108 HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion(
109 ILockBytes* iface,
110 ULARGE_INTEGER libOffset, /* [in] */
111 ULARGE_INTEGER cb, /* [in] */
112 DWORD dwLockType); /* [in] */
114 HRESULT WINAPI HGLOBALLockBytesImpl_Stat(
115 ILockBytes* iface,
116 STATSTG* pstatstg, /* [out] */
117 DWORD grfStatFlag); /* [in] */
120 * Virtual function table for the HGLOBALLockBytesImpl class.
122 static ICOM_VTABLE(ILockBytes) HGLOBALLockBytesImpl_Vtbl =
124 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
125 HGLOBALLockBytesImpl_QueryInterface,
126 HGLOBALLockBytesImpl_AddRef,
127 HGLOBALLockBytesImpl_Release,
128 HGLOBALLockBytesImpl_ReadAt,
129 HGLOBALLockBytesImpl_WriteAt,
130 HGLOBALLockBytesImpl_Flush,
131 HGLOBALLockBytesImpl_SetSize,
132 HGLOBALLockBytesImpl_LockRegion,
133 HGLOBALLockBytesImpl_UnlockRegion,
134 HGLOBALLockBytesImpl_Stat,
137 /******************************************************************************
138 * CreateILockBytesOnHGlobal [OLE32.57]
140 HRESULT WINAPI CreateILockBytesOnHGlobal(HGLOBAL hGlobal,
141 BOOL fDeleteOnRelease,
142 LPLOCKBYTES* ppLkbyt)
144 HGLOBALLockBytesImpl* newLockBytes;
146 newLockBytes = HGLOBALLockBytesImpl_Construct(hGlobal, fDeleteOnRelease);
148 if (newLockBytes != NULL)
150 return IUnknown_QueryInterface((IUnknown*)newLockBytes,
151 &IID_ILockBytes,
152 (void**)ppLkbyt);
155 return E_OUTOFMEMORY;
158 /******************************************************************************
159 * GetHGlobalFromILockBytes [OLE32.70]
161 HRESULT WINAPI GetHGlobalFromILockBytes(ILockBytes* plkbyt, HGLOBAL* phglobal)
163 HGLOBALLockBytesImpl* const pMemLockBytes = (HGLOBALLockBytesImpl*)plkbyt;
165 if (ICOM_VTBL(pMemLockBytes) == &HGLOBALLockBytesImpl_Vtbl)
166 *phglobal = pMemLockBytes->supportHandle;
167 else
168 *phglobal = 0;
170 if (*phglobal == 0)
171 return E_INVALIDARG;
173 return S_OK;
176 /******************************************************************************
178 * HGLOBALLockBytesImpl implementation
182 /******************************************************************************
183 * This is the constructor for the HGLOBALLockBytesImpl class.
185 * Params:
186 * hGlobal - Handle that will support the stream. can be NULL.
187 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
188 * when the IStream object is destroyed.
190 HGLOBALLockBytesImpl* HGLOBALLockBytesImpl_Construct(HGLOBAL hGlobal,
191 BOOL fDeleteOnRelease)
193 HGLOBALLockBytesImpl* newLockBytes;
194 newLockBytes = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALLockBytesImpl));
196 if (newLockBytes!=0)
199 * Set up the virtual function table and reference count.
201 ICOM_VTBL(newLockBytes) = &HGLOBALLockBytesImpl_Vtbl;
202 newLockBytes->ref = 0;
205 * Initialize the support.
207 newLockBytes->supportHandle = hGlobal;
208 newLockBytes->deleteOnRelease = fDeleteOnRelease;
211 * This method will allocate a handle if one is not supplied.
213 if (newLockBytes->supportHandle == 0)
215 newLockBytes->supportHandle = GlobalAlloc(GMEM_MOVEABLE |
216 GMEM_NODISCARD,
221 * Initialize the size of the array to the size of the handle.
223 newLockBytes->byteArraySize.s.HighPart = 0;
224 newLockBytes->byteArraySize.s.LowPart = GlobalSize(
225 newLockBytes->supportHandle);
228 return newLockBytes;
231 /******************************************************************************
232 * This is the destructor of the HGLOBALStreamImpl class.
234 * This method will clean-up all the resources used-up by the given
235 * HGLOBALLockBytesImpl class. The pointer passed-in to this function will be
236 * freed and will not be valid anymore.
238 void HGLOBALLockBytesImpl_Destroy(HGLOBALLockBytesImpl* This)
241 * Release the HGlobal if the constructor asked for that.
243 if (This->deleteOnRelease)
245 GlobalFree(This->supportHandle);
246 This->supportHandle = 0;
250 * Finally, free the memory used-up by the class.
252 HeapFree(GetProcessHeap(), 0, This);
255 /******************************************************************************
256 * This implements the IUnknown method QueryInterface for this
257 * class
259 HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(
260 ILockBytes* iface,
261 REFIID riid, /* [in] */
262 void** ppvObject) /* [iid_is][out] */
264 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
267 * Perform a sanity check on the parameters.
269 if (ppvObject==0)
270 return E_INVALIDARG;
273 * Initialize the return parameter.
275 *ppvObject = 0;
278 * Compare the riid with the interface IDs implemented by this object.
280 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
282 *ppvObject = (ILockBytes*)This;
284 else if (memcmp(&IID_ILockBytes, riid, sizeof(IID_ILockBytes)) == 0)
286 *ppvObject = (ILockBytes*)This;
290 * Check that we obtained an interface.
292 if ((*ppvObject)==0)
293 return E_NOINTERFACE;
296 * Query Interface always increases the reference count by one when it is
297 * successful
299 HGLOBALLockBytesImpl_AddRef(iface);
301 return S_OK;;
304 /******************************************************************************
305 * This implements the IUnknown method AddRef for this
306 * class
308 ULONG WINAPI HGLOBALLockBytesImpl_AddRef(ILockBytes* iface)
310 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
312 This->ref++;
314 return This->ref;
317 /******************************************************************************
318 * This implements the IUnknown method Release for this
319 * class
321 ULONG WINAPI HGLOBALLockBytesImpl_Release(ILockBytes* iface)
323 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
325 ULONG newRef;
327 This->ref--;
329 newRef = This->ref;
332 * If the reference count goes down to 0, perform suicide.
334 if (newRef==0)
336 HGLOBALLockBytesImpl_Destroy(This);
339 return newRef;
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 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.s.LowPart > This->byteArraySize.s.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.s.LowPart -
381 ulOffset.s.LowPart, cb);
384 * Lock the buffer in position and copy the data.
386 supportBuffer = GlobalLock(This->supportHandle);
388 memcpy(pv,
389 (char *) supportBuffer + ulOffset.s.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 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.s.HighPart = 0;
449 newSize.s.LowPart = ulOffset.s.LowPart + cb;
453 * Verify if we need to grow the stream
455 if (newSize.s.LowPart > This->byteArraySize.s.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.s.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 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 HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(
500 ILockBytes* iface,
501 ULARGE_INTEGER libNewSize) /* [in] */
503 HGLOBALLockBytesImpl* const This=(HGLOBALLockBytesImpl*)iface;
506 * As documented.
508 if (libNewSize.s.HighPart != 0)
509 return STG_E_INVALIDFUNCTION;
511 if (This->byteArraySize.s.LowPart == libNewSize.s.LowPart)
512 return S_OK;
515 * Re allocate the HGlobal to fit the new size of the stream.
517 This->supportHandle = GlobalReAlloc(This->supportHandle,
518 libNewSize.s.LowPart,
521 if (This->supportHandle == 0)
522 return STG_E_MEDIUMFULL;
524 This->byteArraySize.s.LowPart = libNewSize.s.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 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 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 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;