Added a missing prototype.
[wine/wine-kai.git] / dlls / ole32 / memlockbytes16.c
blob0320b4524fc739ebee6ca05bab35755b6f33ea55
1 /*
2 * Global memory implementation of ILockBytes.
4 * Copyright 1999 Thuy Nguyen
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include <assert.h>
24 #include <stdarg.h>
25 #include <string.h>
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wine/winbase16.h"
32 #include "winuser.h"
33 #include "objbase.h"
34 #include "ole2.h"
35 #include "winerror.h"
37 #include "ifs.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
43 /******************************************************************************
44 * HGLOBALLockBytesImpl16 definition.
46 * This class imlements the ILockBytes inteface and represents a byte array
47 * object supported by an HGLOBAL pointer.
49 struct HGLOBALLockBytesImpl16
52 * Needs to be the first item in the stuct
53 * since we want to cast this in an ILockBytes pointer
55 ILockBytes16Vtbl *lpVtbl;
56 ULONG ref;
59 * Support for the LockBytes object
61 HGLOBAL16 supportHandle;
64 * This flag is TRUE if the HGLOBAL is destroyed when the object
65 * is finally released.
67 BOOL deleteOnRelease;
69 * Helper variable that contains the size of the byte array
71 ULARGE_INTEGER byteArraySize;
74 typedef struct HGLOBALLockBytesImpl16 HGLOBALLockBytesImpl16;
76 HGLOBALLockBytesImpl16* HGLOBALLockBytesImpl16_Construct(
77 HGLOBAL16 hGlobal,
78 BOOL16 fDeleteOnRelease);
80 void HGLOBALLockBytesImpl16_Destroy(HGLOBALLockBytesImpl16* This);
82 HRESULT WINAPI HGLOBALLockBytesImpl16_QueryInterface(
83 ILockBytes16* iface,
84 REFIID riid, /* [in] */
85 void** ppvObject); /* [out][iid_is] */
87 ULONG WINAPI HGLOBALLockBytesImpl16_AddRef(
88 ILockBytes16* iface);
90 ULONG WINAPI HGLOBALLockBytesImpl16_Release(
91 ILockBytes16* iface);
93 HRESULT WINAPI HGLOBALLockBytesImpl16_ReadAt(
94 ILockBytes16* iface,
95 ULARGE_INTEGER ulOffset, /* [in] */
96 void* pv, /* [out][length_is][size_is] */
97 ULONG cb, /* [in] */
98 ULONG* pcbRead); /* [out] */
100 HRESULT WINAPI HGLOBALLockBytesImpl16_WriteAt(
101 ILockBytes16* iface,
102 ULARGE_INTEGER ulOffset, /* [in] */
103 const void* pv, /* [in][size_is] */
104 ULONG cb, /* [in] */
105 ULONG* pcbWritten); /* [out] */
107 HRESULT WINAPI HGLOBALLockBytesImpl16_Flush(
108 ILockBytes16* iface);
110 HRESULT WINAPI HGLOBALLockBytesImpl16_SetSize(
111 ILockBytes16* iface,
112 ULARGE_INTEGER libNewSize); /* [in] */
114 HRESULT WINAPI HGLOBALLockBytesImpl16_LockRegion(
115 ILockBytes16* iface,
116 ULARGE_INTEGER libOffset, /* [in] */
117 ULARGE_INTEGER cb, /* [in] */
118 DWORD dwLockType); /* [in] */
120 HRESULT WINAPI HGLOBALLockBytesImpl16_UnlockRegion(
121 ILockBytes16* iface,
122 ULARGE_INTEGER libOffset, /* [in] */
123 ULARGE_INTEGER cb, /* [in] */
124 DWORD dwLockType); /* [in] */
126 HRESULT WINAPI HGLOBALLockBytesImpl16_Stat(
127 ILockBytes16* iface,
128 STATSTG16* pstatstg, /* [out] */
129 DWORD grfStatFlag); /* [in] */
131 /******************************************************************************
133 * HGLOBALLockBytesImpl16 implementation
137 /******************************************************************************
138 * This is the constructor for the HGLOBALLockBytesImpl16 class.
140 * Params:
141 * hGlobal - Handle that will support the stream. can be NULL.
142 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL16 will be released
143 * when the IStream object is destroyed.
145 HGLOBALLockBytesImpl16*
146 HGLOBALLockBytesImpl16_Construct(HGLOBAL16 hGlobal,
147 BOOL16 fDeleteOnRelease)
149 HGLOBALLockBytesImpl16* newLockBytes;
151 static ILockBytes16Vtbl vt16;
152 static SEGPTR msegvt16;
153 HMODULE16 hcomp = GetModuleHandle16("OLE2");
156 TRACE("(%x,%d)\n",hGlobal,fDeleteOnRelease);
157 newLockBytes = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALLockBytesImpl16));
158 if (newLockBytes == NULL)
159 return NULL;
162 * Set up the virtual function table and reference count.
164 if (!msegvt16)
166 #define VTENT(x) vt16.x = (void*)GetProcAddress16(hcomp,"HGLOBALLockBytesImpl16_"#x);assert(vt16.x)
167 VTENT(QueryInterface);
168 VTENT(AddRef);
169 VTENT(Release);
170 VTENT(ReadAt);
171 VTENT(WriteAt);
172 VTENT(Flush);
173 VTENT(SetSize);
174 VTENT(LockRegion);
175 VTENT(UnlockRegion);
176 #undef VTENT
177 msegvt16 = MapLS( &vt16 );
179 newLockBytes->lpVtbl = (ILockBytes16Vtbl*)msegvt16;
180 newLockBytes->ref = 0;
182 * Initialize the support.
184 newLockBytes->supportHandle = hGlobal;
185 newLockBytes->deleteOnRelease = fDeleteOnRelease;
188 * This method will allocate a handle if one is not supplied.
190 if (newLockBytes->supportHandle == 0)
191 newLockBytes->supportHandle = GlobalAlloc16(GMEM_MOVEABLE | GMEM_NODISCARD, 0);
194 * Initialize the size of the array to the size of the handle.
196 newLockBytes->byteArraySize.u.HighPart = 0;
197 newLockBytes->byteArraySize.u.LowPart = GlobalSize16(
198 newLockBytes->supportHandle);
200 return (HGLOBALLockBytesImpl16*)MapLS(newLockBytes);
203 /******************************************************************************
204 * This is the destructor of the HGLOBALStreamImpl class.
206 * This method will clean-up all the resources used-up by the given
207 * HGLOBALLockBytesImpl16 class. The pointer passed-in to this function will be
208 * freed and will not be valid anymore.
210 void HGLOBALLockBytesImpl16_Destroy(HGLOBALLockBytesImpl16* This)
212 TRACE("()\n");
214 * Release the HGlobal if the constructor asked for that.
216 if (This->deleteOnRelease)
218 GlobalFree16(This->supportHandle);
219 This->supportHandle = 0;
223 * Finally, free the memory used-up by the class.
225 HeapFree(GetProcessHeap(), 0, This);
228 /******************************************************************************
229 * This implements the IUnknown method QueryInterface for this
230 * class
232 HRESULT WINAPI HGLOBALLockBytesImpl16_QueryInterface(
233 ILockBytes16* iface, /* [in] SEGPTR */
234 REFIID riid, /* [in] */
235 void** ppvObject) /* [out][iid_is] (ptr to SEGPTR!) */
237 HGLOBALLockBytesImpl16* const This=(HGLOBALLockBytesImpl16*)MapSL((SEGPTR)iface);
239 TRACE("(%p,%s,%p)\n",iface,debugstr_guid(riid),ppvObject);
241 * Perform a sanity check on the parameters.
243 if (ppvObject==0)
244 return E_INVALIDARG;
247 * Initialize the return parameter.
249 *ppvObject = 0;
251 * Compare the riid with the interface IDs implemented by this object.
253 if ( !memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) ||
254 !memcmp(&IID_ILockBytes, riid, sizeof(IID_ILockBytes))
256 *ppvObject = (void*)iface;
259 * Check that we obtained an interface.
261 if ((*ppvObject)==0)
262 return E_NOINTERFACE;
265 * Query Interface always increases the reference count by one when it is
266 * successful
268 HGLOBALLockBytesImpl16_AddRef((ILockBytes16*)This);
270 return S_OK;
273 /******************************************************************************
274 * This implements the IUnknown method AddRef for this
275 * class
277 ULONG WINAPI HGLOBALLockBytesImpl16_AddRef(ILockBytes16* iface)
279 HGLOBALLockBytesImpl16* const This=(HGLOBALLockBytesImpl16*)iface;
281 TRACE("(%p)\n",This);
283 return InterlockedIncrement(&This->ref);
286 /******************************************************************************
287 * This implements the IUnknown method Release for this
288 * class
290 ULONG WINAPI HGLOBALLockBytesImpl16_Release(ILockBytes16* iface)
292 HGLOBALLockBytesImpl16* const This=(HGLOBALLockBytesImpl16*)iface;
293 ULONG ref;
295 TRACE("(%p)\n",This);
297 ref = InterlockedDecrement(&This->ref);
300 * If the reference count goes down to 0, perform suicide.
302 if (ref==0)
303 HGLOBALLockBytesImpl16_Destroy(This);
304 return ref;
307 /******************************************************************************
308 * This method is part of the ILockBytes interface.
310 * It reads a block of information from the byte array at the specified
311 * offset.
313 * See the documentation of ILockBytes for more info.
315 HRESULT WINAPI HGLOBALLockBytesImpl16_ReadAt(
316 ILockBytes16* iface,
317 ULARGE_INTEGER ulOffset, /* [in] */
318 void* pv, /* [out][length_is][size_is] */
319 ULONG cb, /* [in] */
320 ULONG* pcbRead) /* [out] */
322 HGLOBALLockBytesImpl16* const This=(HGLOBALLockBytesImpl16*)iface;
324 void* supportBuffer;
325 ULONG bytesReadBuffer = 0;
326 ULONG bytesToReadFromBuffer;
328 TRACE("(%p,%ld,%p,%ld,%p)\n",This,ulOffset.u.LowPart,pv,cb,pcbRead);
330 * If the caller is not interested in the number of bytes read,
331 * we use another buffer to avoid "if" statements in the code.
333 if (pcbRead == 0)
334 pcbRead = &bytesReadBuffer;
337 * Make sure the offset is valid.
339 if (ulOffset.u.LowPart > This->byteArraySize.u.LowPart)
340 return E_FAIL;
343 * Using the known size of the array, calculate the number of bytes
344 * to read.
346 bytesToReadFromBuffer = min(This->byteArraySize.u.LowPart -
347 ulOffset.u.LowPart, cb);
350 * Lock the buffer in position and copy the data.
352 supportBuffer = GlobalLock16(This->supportHandle);
354 memcpy(pv,
355 (char *) supportBuffer + ulOffset.u.LowPart,
356 bytesToReadFromBuffer);
359 * Return the number of bytes read.
361 *pcbRead = bytesToReadFromBuffer;
364 * Cleanup
366 GlobalUnlock16(This->supportHandle);
369 * The function returns S_OK if the specified number of bytes were read
370 * or the end of the array was reached.
371 * It returns STG_E_READFAULT if the number of bytes to read does not equal
372 * the number of bytes actually read.
374 if(*pcbRead == cb)
375 return S_OK;
377 return STG_E_READFAULT;
380 /******************************************************************************
381 * This method is part of the ILockBytes interface.
383 * It writes the specified bytes at the specified offset.
384 * position. If the array is too small, it will be resized.
386 * See the documentation of ILockBytes for more info.
388 HRESULT WINAPI HGLOBALLockBytesImpl16_WriteAt(
389 ILockBytes16* iface,
390 ULARGE_INTEGER ulOffset, /* [in] */
391 const void* pv, /* [in][size_is] */
392 ULONG cb, /* [in] */
393 ULONG* pcbWritten) /* [out] */
395 HGLOBALLockBytesImpl16* const This=(HGLOBALLockBytesImpl16*)iface;
397 void* supportBuffer;
398 ULARGE_INTEGER newSize;
399 ULONG bytesWritten = 0;
401 TRACE("(%p,%ld,%p,%ld,%p)\n",This,ulOffset.u.LowPart,pv,cb,pcbWritten);
403 * If the caller is not interested in the number of bytes written,
404 * we use another buffer to avoid "if" statements in the code.
406 if (pcbWritten == 0)
407 pcbWritten = &bytesWritten;
409 if (cb == 0)
410 return S_OK;
412 newSize.u.HighPart = 0;
413 newSize.u.LowPart = ulOffset.u.LowPart + cb;
416 * Verify if we need to grow the stream
418 if (newSize.u.LowPart > This->byteArraySize.u.LowPart)
420 /* grow stream */
421 if (HGLOBALLockBytesImpl16_SetSize(iface, newSize) == STG_E_MEDIUMFULL)
422 return STG_E_MEDIUMFULL;
426 * Lock the buffer in position and copy the data.
428 supportBuffer = GlobalLock16(This->supportHandle);
430 memcpy((char *) supportBuffer + ulOffset.u.LowPart, pv, cb);
433 * Return the number of bytes written.
435 *pcbWritten = cb;
438 * Cleanup
440 GlobalUnlock16(This->supportHandle);
442 return S_OK;
445 /******************************************************************************
446 * This method is part of the ILockBytes interface.
448 * See the documentation of ILockBytes for more info.
450 HRESULT WINAPI HGLOBALLockBytesImpl16_Flush(ILockBytes16* iface)
452 TRACE("(%p)\n",iface);
453 return S_OK;
456 /******************************************************************************
457 * This method is part of the ILockBytes interface.
459 * It will change the size of the byte array.
461 * See the documentation of ILockBytes for more info.
463 HRESULT WINAPI HGLOBALLockBytesImpl16_SetSize(
464 ILockBytes16* iface,
465 ULARGE_INTEGER libNewSize) /* [in] */
467 HGLOBALLockBytesImpl16* const This=(HGLOBALLockBytesImpl16*)iface;
468 HGLOBAL16 supportHandle;
470 TRACE("(%p,%ld)\n",This,libNewSize.u.LowPart);
472 * As documented.
474 if (libNewSize.u.HighPart != 0)
475 return STG_E_INVALIDFUNCTION;
477 if (This->byteArraySize.u.LowPart == libNewSize.u.LowPart)
478 return S_OK;
481 * Re allocate the HGlobal to fit the new size of the stream.
483 supportHandle = GlobalReAlloc16(This->supportHandle, libNewSize.u.LowPart, 0);
485 if (supportHandle == 0)
486 return STG_E_MEDIUMFULL;
488 This->supportHandle = supportHandle;
489 This->byteArraySize.u.LowPart = libNewSize.u.LowPart;
491 return S_OK;
494 /******************************************************************************
495 * This method is part of the ILockBytes interface.
497 * The global memory implementation of ILockBytes does not support locking.
499 * See the documentation of ILockBytes for more info.
501 HRESULT WINAPI HGLOBALLockBytesImpl16_LockRegion(
502 ILockBytes16* iface,
503 ULARGE_INTEGER libOffset, /* [in] */
504 ULARGE_INTEGER cb, /* [in] */
505 DWORD dwLockType) /* [in] */
507 return STG_E_INVALIDFUNCTION;
510 /******************************************************************************
511 * This method is part of the ILockBytes interface.
513 * The global memory implementation of ILockBytes does not support locking.
515 * See the documentation of ILockBytes for more info.
517 HRESULT WINAPI HGLOBALLockBytesImpl16_UnlockRegion(
518 ILockBytes16* iface,
519 ULARGE_INTEGER libOffset, /* [in] */
520 ULARGE_INTEGER cb, /* [in] */
521 DWORD dwLockType) /* [in] */
523 return STG_E_INVALIDFUNCTION;
526 /******************************************************************************
527 * This method is part of the ILockBytes interface.
529 * This method returns information about the current
530 * byte array object.
532 * See the documentation of ILockBytes for more info.
534 HRESULT WINAPI HGLOBALLockBytesImpl16_Stat(
535 ILockBytes16*iface,
536 STATSTG16* pstatstg, /* [out] */
537 DWORD grfStatFlag) /* [in] */
539 HGLOBALLockBytesImpl16* const This=(HGLOBALLockBytesImpl16*)iface;
541 memset(pstatstg, 0, sizeof(STATSTG16));
543 pstatstg->pwcsName = NULL;
544 pstatstg->type = STGTY_LOCKBYTES;
545 pstatstg->cbSize = This->byteArraySize;
547 return S_OK;
550 /******************************************************************************
551 * CreateILockBytesOnHGlobal [OLE2.54]
553 * Creates an ILockBytes interface for a HGLOBAL handle.
555 * Params:
556 * hGlobal the global handle (16bit)
557 * fDeleteOnRelease delete handle on release.
558 * ppLkbyt pointer to ILockBytes interface.
560 * Returns:
561 * Staddard OLE error return codes.
564 HRESULT WINAPI CreateILockBytesOnHGlobal16(
565 HGLOBAL16 hGlobal, /* [in] */
566 BOOL16 fDeleteOnRelease, /* [in] */
567 LPLOCKBYTES16 *ppLkbyt) /* [out] (ptr to SEGPTR!) */
569 HGLOBALLockBytesImpl16* newLockBytes; /* SEGPTR */
571 newLockBytes = HGLOBALLockBytesImpl16_Construct(hGlobal, fDeleteOnRelease);
573 if (newLockBytes != NULL)
574 return HGLOBALLockBytesImpl16_QueryInterface((ILockBytes16*)newLockBytes,
575 &IID_ILockBytes,
576 (void**)ppLkbyt);
577 return E_OUTOFMEMORY;