ReactOS 0.4.15-dev-7705-g2320c37
memlockbytes.c File Reference
#include <assert.h>
#include <stdarg.h>
#include <string.h>
#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "objbase.h"
#include "ole2.h"
#include "winerror.h"
#include "wine/debug.h"
Include dependency graph for memlockbytes.c:

Go to the source code of this file.

Classes

struct  HGLOBALLockBytesImpl
 

Macros

#define COBJMACROS
 
#define NONAMELESSUNION
 

Typedefs

typedef struct HGLOBALLockBytesImpl HGLOBALLockBytesImpl
 

Functions

 WINE_DEFAULT_DEBUG_CHANNEL (ole)
 
static HGLOBALLockBytesImplimpl_from_ILockBytes (ILockBytes *iface)
 
HRESULT WINAPI CreateILockBytesOnHGlobal (HGLOBAL global, BOOL delete_on_release, ILockBytes **ret)
 
HRESULT WINAPI GetHGlobalFromILockBytes (ILockBytes *iface, HGLOBAL *phglobal)
 
static HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface (ILockBytes *iface, REFIID riid, void **ppvObject)
 
static ULONG WINAPI HGLOBALLockBytesImpl_AddRef (ILockBytes *iface)
 
static ULONG WINAPI HGLOBALLockBytesImpl_Release (ILockBytes *iface)
 
static HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt (ILockBytes *iface, ULARGE_INTEGER ulOffset, void *pv, ULONG cb, ULONG *pcbRead)
 
static HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt (ILockBytes *iface, ULARGE_INTEGER ulOffset, const void *pv, ULONG cb, ULONG *pcbWritten)
 
static HRESULT WINAPI HGLOBALLockBytesImpl_Flush (ILockBytes *iface)
 
static HRESULT WINAPI HGLOBALLockBytesImpl_SetSize (ILockBytes *iface, ULARGE_INTEGER libNewSize)
 
static HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion (ILockBytes *iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
 
static HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion (ILockBytes *iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
 
static HRESULT WINAPI HGLOBALLockBytesImpl_Stat (ILockBytes *iface, STATSTG *pstatstg, DWORD grfStatFlag)
 

Variables

static const ILockBytesVtbl HGLOBALLockBytesImpl_Vtbl
 

Macro Definition Documentation

◆ COBJMACROS

#define COBJMACROS

Definition at line 26 of file memlockbytes.c.

◆ NONAMELESSUNION

#define NONAMELESSUNION

Definition at line 27 of file memlockbytes.c.

Typedef Documentation

◆ HGLOBALLockBytesImpl

Definition at line 68 of file memlockbytes.c.

Function Documentation

◆ CreateILockBytesOnHGlobal()

HRESULT WINAPI CreateILockBytesOnHGlobal ( HGLOBAL  global,
BOOL  delete_on_release,
ILockBytes **  ret 
)

Definition at line 98 of file memlockbytes.c.

99{
100 HGLOBALLockBytesImpl* lockbytes;
101
102 lockbytes = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALLockBytesImpl));
103 if (!lockbytes) return E_OUTOFMEMORY;
104
105 lockbytes->ILockBytes_iface.lpVtbl = &HGLOBALLockBytesImpl_Vtbl;
106 lockbytes->ref = 1;
107
108 /*
109 * Initialize the support.
110 */
111 lockbytes->supportHandle = global;
112 lockbytes->deleteOnRelease = delete_on_release;
113
114 /*
115 * This method will allocate a handle if one is not supplied.
116 */
117 if (lockbytes->supportHandle == 0)
119
120 /*
121 * Initialize the size of the array to the size of the handle.
122 */
123 lockbytes->byteArraySize.u.HighPart = 0;
124 lockbytes->byteArraySize.u.LowPart = GlobalSize(lockbytes->supportHandle);
125
126 *ret = &lockbytes->ILockBytes_iface;
127
128 return S_OK;
129}
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
int global
Definition: ehframes.cpp:22
HGLOBAL NTAPI GlobalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:368
SIZE_T NTAPI GlobalSize(HGLOBAL hMem)
Definition: heapmem.c:1090
#define S_OK
Definition: intsafe.h:52
static const ILockBytesVtbl HGLOBALLockBytesImpl_Vtbl
Definition: memlockbytes.c:75
ILockBytes ILockBytes_iface
Definition: memlockbytes.c:48
ULARGE_INTEGER byteArraySize
Definition: memlockbytes.c:65
struct _ULARGE_INTEGER::@4136 u
int ret
#define GMEM_NODISCARD
Definition: winbase.h:302
#define GMEM_MOVEABLE
Definition: winbase.h:294

Referenced by create_storage(), DataObjectImpl_CreateComplex(), get_data_from_storage(), get_stgmed_for_storage(), render_embed_source_hack(), stream_to_storage(), test_data_cache_save(), test_data_cache_save_data(), test_hglobal_storage_creation(), test_hglobal_storage_stat(), and test_OleCreateStaticFromData().

◆ GetHGlobalFromILockBytes()

HRESULT WINAPI GetHGlobalFromILockBytes ( ILockBytes iface,
HGLOBAL phglobal 
)

Definition at line 145 of file memlockbytes.c.

146{
148 STATSTG stbuf;
151 ULONG xread;
152
153 *phglobal = 0;
154 if (This->ILockBytes_iface.lpVtbl == &HGLOBALLockBytesImpl_Vtbl) {
155 *phglobal = This->supportHandle;
156 if (*phglobal == 0)
157 return E_INVALIDARG;
158 return S_OK;
159 }
160 /* It is not our lockbytes implementation, so use a more generic way */
161 hres = ILockBytes_Stat(iface,&stbuf,STATFLAG_NONAME);
162 if (hres != S_OK) {
163 ERR("Cannot ILockBytes_Stat, %x\n",hres);
164 return hres;
165 }
166 TRACE("cbSize is %s\n", wine_dbgstr_longlong(stbuf.cbSize.QuadPart));
167 *phglobal = GlobalAlloc( GMEM_MOVEABLE|GMEM_SHARE, stbuf.cbSize.u.LowPart);
168 if (!*phglobal)
169 return E_INVALIDARG;
170 memset(&start,0,sizeof(start));
171 hres = ILockBytes_ReadAt(iface, start, GlobalLock(*phglobal), stbuf.cbSize.u.LowPart, &xread);
172 GlobalUnlock(*phglobal);
173 if (hres != S_OK) {
174 FIXME("%p->ReadAt failed with %x\n",iface,hres);
175 return hres;
176 }
177 if (stbuf.cbSize.u.LowPart != xread) {
178 FIXME("Read size is not requested size %d vs %d?\n",stbuf.cbSize.u.LowPart, xread);
179 }
180 return S_OK;
181}
#define FIXME(fmt,...)
Definition: debug.h:111
#define ERR(fmt,...)
Definition: debug.h:110
#define E_INVALIDARG
Definition: ddrawi.h:101
static __inline const char * wine_dbgstr_longlong(ULONGLONG ll)
Definition: compat.h:49
GLuint start
Definition: gl.h:1545
LPVOID NTAPI GlobalLock(HGLOBAL hMem)
Definition: heapmem.c:755
BOOL NTAPI GlobalUnlock(HGLOBAL hMem)
Definition: heapmem.c:1190
static HGLOBALLockBytesImpl * impl_from_ILockBytes(ILockBytes *iface)
Definition: memlockbytes.c:70
HRESULT hres
Definition: protocol.c:465
#define memset(x, y, z)
Definition: compat.h:39
#define TRACE(s)
Definition: solgame.cpp:4
uint32_t ULONG
Definition: typedefs.h:59
#define GMEM_SHARE
Definition: winbase.h:305

Referenced by create_storage().

◆ HGLOBALLockBytesImpl_AddRef()

static ULONG WINAPI HGLOBALLockBytesImpl_AddRef ( ILockBytes iface)
static

Definition at line 222 of file memlockbytes.c.

223{
225 return InterlockedIncrement(&This->ref);
226}
#define InterlockedIncrement
Definition: armddk.h:53

◆ HGLOBALLockBytesImpl_Flush()

static HRESULT WINAPI HGLOBALLockBytesImpl_Flush ( ILockBytes iface)
static

Definition at line 396 of file memlockbytes.c.

397{
398 return S_OK;
399}

◆ HGLOBALLockBytesImpl_LockRegion()

static HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion ( ILockBytes iface,
ULARGE_INTEGER  libOffset,
ULARGE_INTEGER  cb,
DWORD  dwLockType 
)
static

Definition at line 445 of file memlockbytes.c.

450{
452}
#define STG_E_INVALIDFUNCTION
Definition: winerror.h:2564

◆ HGLOBALLockBytesImpl_QueryInterface()

static HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface ( ILockBytes iface,
REFIID  riid,
void **  ppvObject 
)
static

Definition at line 193 of file memlockbytes.c.

197{
199
200 if (ppvObject==0)
201 return E_INVALIDARG;
202
203 *ppvObject = 0;
204
206 IsEqualIID(riid, &IID_ILockBytes))
207 {
208 *ppvObject = &This->ILockBytes_iface;
209 }
210 else
211 return E_NOINTERFACE;
212
213 ILockBytes_AddRef(iface);
214
215 return S_OK;
216}
const GUID IID_IUnknown
REFIID riid
Definition: atlbase.h:39
#define IsEqualIID(riid1, riid2)
Definition: guiddef.h:95
_In_ void _In_ PCCERT_CONTEXT _In_opt_ LPFILETIME _In_ DWORD _In_ DWORD _Outptr_opt_ void ** ppvObject
Definition: wincrypt.h:6082
#define E_NOINTERFACE
Definition: winerror.h:2364

◆ HGLOBALLockBytesImpl_ReadAt()

static HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt ( ILockBytes iface,
ULARGE_INTEGER  ulOffset,
void pv,
ULONG  cb,
ULONG pcbRead 
)
static

Definition at line 259 of file memlockbytes.c.

265{
267
268 void* supportBuffer;
269 ULONG bytesReadBuffer = 0;
270 ULONG bytesToReadFromBuffer;
271
272 /*
273 * If the caller is not interested in the number of bytes read,
274 * we use another buffer to avoid "if" statements in the code.
275 */
276 if (pcbRead == 0)
277 pcbRead = &bytesReadBuffer;
278
279 /*
280 * Make sure the offset is valid.
281 */
282 if (ulOffset.u.LowPart > This->byteArraySize.u.LowPart)
283 return E_FAIL;
284
285 /*
286 * Using the known size of the array, calculate the number of bytes
287 * to read.
288 */
289 bytesToReadFromBuffer = min(This->byteArraySize.u.LowPart -
290 ulOffset.u.LowPart, cb);
291
292 /*
293 * Lock the buffer in position and copy the data.
294 */
295 supportBuffer = GlobalLock(This->supportHandle);
296
297 memcpy(pv,
298 (char *) supportBuffer + ulOffset.u.LowPart,
299 bytesToReadFromBuffer);
300
301 /*
302 * Return the number of bytes read.
303 */
304 *pcbRead = bytesToReadFromBuffer;
305
306 /*
307 * Cleanup
308 */
309 GlobalUnlock(This->supportHandle);
310
311 /*
312 * The function returns S_OK if the specified number of bytes were read
313 * or the end of the array was reached.
314 * It returns STG_E_READFAULT if the number of bytes to read does not equal
315 * the number of bytes actually read.
316 */
317 if(*pcbRead == cb)
318 return S_OK;
319
320 return STG_E_READFAULT;
321}
#define E_FAIL
Definition: ddrawi.h:102
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
static HMODULE MODULEINFO DWORD cb
Definition: module.c:33
#define min(a, b)
Definition: monoChain.cc:55
#define STG_E_READFAULT
Definition: winerror.h:2576

◆ HGLOBALLockBytesImpl_Release()

static ULONG WINAPI HGLOBALLockBytesImpl_Release ( ILockBytes iface)
static

Definition at line 232 of file memlockbytes.c.

233{
235 ULONG ref;
236
238 if (!ref)
239 {
240 if (This->deleteOnRelease)
241 {
242 GlobalFree(This->supportHandle);
243 This->supportHandle = 0;
244 }
246 }
247
248 return ref;
249}
#define InterlockedDecrement
Definition: armddk.h:52
#define HeapFree(x, y, z)
Definition: compat.h:735
HGLOBAL NTAPI GlobalFree(HGLOBAL hMem)
Definition: heapmem.c:611
Definition: send.c:48

◆ HGLOBALLockBytesImpl_SetSize()

static HRESULT WINAPI HGLOBALLockBytesImpl_SetSize ( ILockBytes iface,
ULARGE_INTEGER  libNewSize 
)
static

Definition at line 408 of file memlockbytes.c.

411{
413 HGLOBAL supportHandle;
414
415 /*
416 * As documented.
417 */
418 if (libNewSize.u.HighPart != 0)
420
421 if (This->byteArraySize.u.LowPart == libNewSize.u.LowPart)
422 return S_OK;
423
424 /*
425 * Re allocate the HGlobal to fit the new size of the stream.
426 */
427 supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
428
429 if (supportHandle == 0)
430 return STG_E_MEDIUMFULL;
431
432 This->supportHandle = supportHandle;
433 This->byteArraySize.u.LowPart = libNewSize.u.LowPart;
434
435 return S_OK;
436}
HGLOBAL NTAPI GlobalReAlloc(HGLOBAL hMem, SIZE_T dwBytes, UINT uFlags)
Definition: heapmem.c:825
#define STG_E_MEDIUMFULL
Definition: winerror.h:2581

◆ HGLOBALLockBytesImpl_Stat()

static HRESULT WINAPI HGLOBALLockBytesImpl_Stat ( ILockBytes iface,
STATSTG *  pstatstg,
DWORD  grfStatFlag 
)
static

Definition at line 478 of file memlockbytes.c.

482{
484
485 memset(pstatstg, 0, sizeof(STATSTG));
486
487 pstatstg->pwcsName = NULL;
488 pstatstg->type = STGTY_LOCKBYTES;
489 pstatstg->cbSize = This->byteArraySize;
490
491 return S_OK;
492}
#define NULL
Definition: types.h:112

◆ HGLOBALLockBytesImpl_UnlockRegion()

static HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion ( ILockBytes iface,
ULARGE_INTEGER  libOffset,
ULARGE_INTEGER  cb,
DWORD  dwLockType 
)
static

Definition at line 461 of file memlockbytes.c.

466{
468}

◆ HGLOBALLockBytesImpl_WriteAt()

static HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt ( ILockBytes iface,
ULARGE_INTEGER  ulOffset,
const void pv,
ULONG  cb,
ULONG pcbWritten 
)
static

Definition at line 331 of file memlockbytes.c.

337{
339
340 void* supportBuffer;
341 ULARGE_INTEGER newSize;
343
344 /*
345 * If the caller is not interested in the number of bytes written,
346 * we use another buffer to avoid "if" statements in the code.
347 */
348 if (pcbWritten == 0)
349 pcbWritten = &bytesWritten;
350
351 if (cb == 0)
352 {
353 return S_OK;
354 }
355 else
356 {
357 newSize.u.HighPart = 0;
358 newSize.u.LowPart = ulOffset.u.LowPart + cb;
359 }
360
361 /*
362 * Verify if we need to grow the stream
363 */
364 if (newSize.u.LowPart > This->byteArraySize.u.LowPart)
365 {
366 /* grow stream */
367 if (ILockBytes_SetSize(iface, newSize) == STG_E_MEDIUMFULL)
368 return STG_E_MEDIUMFULL;
369 }
370
371 /*
372 * Lock the buffer in position and copy the data.
373 */
374 supportBuffer = GlobalLock(This->supportHandle);
375
376 memcpy((char *) supportBuffer + ulOffset.u.LowPart, pv, cb);
377
378 /*
379 * Return the number of bytes written.
380 */
381 *pcbWritten = cb;
382
383 /*
384 * Cleanup
385 */
386 GlobalUnlock(This->supportHandle);
387
388 return S_OK;
389}
GLenum GLsizei GLuint GLint * bytesWritten
Definition: glext.h:11123

◆ impl_from_ILockBytes()

◆ WINE_DEFAULT_DEBUG_CHANNEL()

WINE_DEFAULT_DEBUG_CHANNEL ( ole  )

Variable Documentation

◆ HGLOBALLockBytesImpl_Vtbl

static const ILockBytesVtbl HGLOBALLockBytesImpl_Vtbl
static
Initial value:
=
{
}
static ULONG WINAPI HGLOBALLockBytesImpl_AddRef(ILockBytes *iface)
Definition: memlockbytes.c:222
static ULONG WINAPI HGLOBALLockBytesImpl_Release(ILockBytes *iface)
Definition: memlockbytes.c:232
static HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt(ILockBytes *iface, ULARGE_INTEGER ulOffset, const void *pv, ULONG cb, ULONG *pcbWritten)
Definition: memlockbytes.c:331
static HRESULT WINAPI HGLOBALLockBytesImpl_Stat(ILockBytes *iface, STATSTG *pstatstg, DWORD grfStatFlag)
Definition: memlockbytes.c:478
static HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(ILockBytes *iface, ULARGE_INTEGER ulOffset, void *pv, ULONG cb, ULONG *pcbRead)
Definition: memlockbytes.c:259
static HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion(ILockBytes *iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
Definition: memlockbytes.c:445
static HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion(ILockBytes *iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
Definition: memlockbytes.c:461
static HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(ILockBytes *iface, REFIID riid, void **ppvObject)
Definition: memlockbytes.c:193
static HRESULT WINAPI HGLOBALLockBytesImpl_Flush(ILockBytes *iface)
Definition: memlockbytes.c:396
static HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(ILockBytes *iface, ULARGE_INTEGER libNewSize)
Definition: memlockbytes.c:408

Definition at line 75 of file memlockbytes.c.

Referenced by CreateILockBytesOnHGlobal(), and GetHGlobalFromILockBytes().