ReactOS 0.4.16-dev-2354-g16de117
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
 

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.

Typedef Documentation

◆ HGLOBALLockBytesImpl

Definition at line 66 of file memlockbytes.c.

Function Documentation

◆ CreateILockBytesOnHGlobal()

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

Definition at line 96 of file memlockbytes.c.

97{
98 HGLOBALLockBytesImpl* lockbytes;
99
100 lockbytes = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALLockBytesImpl));
101 if (!lockbytes) return E_OUTOFMEMORY;
102
103 lockbytes->ILockBytes_iface.lpVtbl = &HGLOBALLockBytesImpl_Vtbl;
104 lockbytes->ref = 1;
105
106 /*
107 * Initialize the support.
108 */
109 lockbytes->supportHandle = global;
110 lockbytes->deleteOnRelease = delete_on_release;
111
112 /*
113 * This method will allocate a handle if one is not supplied.
114 */
115 if (lockbytes->supportHandle == 0)
117
118 /*
119 * Initialize the size of the array to the size of the handle.
120 */
121 lockbytes->byteArraySize.HighPart = 0;
122 lockbytes->byteArraySize.LowPart = GlobalSize(lockbytes->supportHandle);
123
124 *ret = &lockbytes->ILockBytes_iface;
125
126 return S_OK;
127}
#define E_OUTOFMEMORY
Definition: ddrawi.h:100
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
return ret
Definition: mutex.c:146
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:73
ILockBytes ILockBytes_iface
Definition: memlockbytes.c:46
ULARGE_INTEGER byteArraySize
Definition: memlockbytes.c:63
$ULONG LowPart
Definition: ntbasedef.h:581
$ULONG HighPart
Definition: ntbasedef.h:582
#define GMEM_NODISCARD
Definition: winbase.h:326
#define GMEM_MOVEABLE
Definition: winbase.h:318

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 143 of file memlockbytes.c.

144{
146 STATSTG stbuf;
149 ULONG xread;
150
151 *phglobal = 0;
152 if (This->ILockBytes_iface.lpVtbl == &HGLOBALLockBytesImpl_Vtbl) {
153 *phglobal = This->supportHandle;
154 if (*phglobal == 0)
155 return E_INVALIDARG;
156 return S_OK;
157 }
158 /* It is not our lockbytes implementation, so use a more generic way */
159 hres = ILockBytes_Stat(iface,&stbuf,STATFLAG_NONAME);
160 if (hres != S_OK) {
161 ERR("Cannot ILockBytes_Stat, %lx\n",hres);
162 return hres;
163 }
164 TRACE("cbSize is %s\n", wine_dbgstr_longlong(stbuf.cbSize.QuadPart));
165 *phglobal = GlobalAlloc( GMEM_MOVEABLE|GMEM_SHARE, stbuf.cbSize.LowPart);
166 if (!*phglobal)
167 return E_INVALIDARG;
168 memset(&start,0,sizeof(start));
169 hres = ILockBytes_ReadAt(iface, start, GlobalLock(*phglobal), stbuf.cbSize.LowPart, &xread);
170 GlobalUnlock(*phglobal);
171 if (hres != S_OK) {
172 FIXME("%p->ReadAt failed with %lx\n",iface,hres);
173 return hres;
174 }
175 if (stbuf.cbSize.LowPart != xread) {
176 FIXME("Read size is not requested size %ld vs %ld?\n",stbuf.cbSize.LowPart, xread);
177 }
178 return S_OK;
179}
#define FIXME(fmt,...)
Definition: precomp.h:53
#define ERR(fmt,...)
Definition: precomp.h:57
#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
BOOL NTAPI GlobalUnlock(HGLOBAL hMem)
Definition: heapmem.c:1190
static ERESOURCE GlobalLock
Definition: sys_arch.c:8
static HGLOBALLockBytesImpl * impl_from_ILockBytes(ILockBytes *iface)
Definition: memlockbytes.c:68
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:329

Referenced by create_storage().

◆ HGLOBALLockBytesImpl_AddRef()

static ULONG WINAPI HGLOBALLockBytesImpl_AddRef ( ILockBytes iface)
static

Definition at line 220 of file memlockbytes.c.

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

◆ HGLOBALLockBytesImpl_Flush()

static HRESULT WINAPI HGLOBALLockBytesImpl_Flush ( ILockBytes iface)
static

Definition at line 393 of file memlockbytes.c.

394{
395 return S_OK;
396}

◆ HGLOBALLockBytesImpl_LockRegion()

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

Definition at line 442 of file memlockbytes.c.

447{
449}
#define STG_E_INVALIDFUNCTION
Definition: winerror.h:3659

◆ HGLOBALLockBytesImpl_QueryInterface()

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

Definition at line 191 of file memlockbytes.c.

195{
197
198 if (ppvObject==0)
199 return E_INVALIDARG;
200
201 *ppvObject = 0;
202
204 IsEqualIID(riid, &IID_ILockBytes))
205 {
206 *ppvObject = &This->ILockBytes_iface;
207 }
208 else
209 return E_NOINTERFACE;
210
211 ILockBytes_AddRef(iface);
212
213 return S_OK;
214}
const GUID IID_IUnknown
REFIID riid
Definition: atlbase.h:39
#define IsEqualIID(riid1, riid2)
Definition: guiddef.h:95
#define E_NOINTERFACE
Definition: winerror.h:3479

◆ HGLOBALLockBytesImpl_ReadAt()

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

Definition at line 257 of file memlockbytes.c.

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

◆ HGLOBALLockBytesImpl_Release()

static ULONG WINAPI HGLOBALLockBytesImpl_Release ( ILockBytes iface)
static

Definition at line 230 of file memlockbytes.c.

231{
233 ULONG ref;
234
236 if (!ref)
237 {
238 if (This->deleteOnRelease)
239 {
240 GlobalFree(This->supportHandle);
241 This->supportHandle = 0;
242 }
244 }
245
246 return ref;
247}
#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 405 of file memlockbytes.c.

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

◆ HGLOBALLockBytesImpl_Stat()

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

Definition at line 475 of file memlockbytes.c.

479{
481
482 memset(pstatstg, 0, sizeof(STATSTG));
483
484 pstatstg->pwcsName = NULL;
485 pstatstg->type = STGTY_LOCKBYTES;
486 pstatstg->cbSize = This->byteArraySize;
487
488 return S_OK;
489}
#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 458 of file memlockbytes.c.

463{
465}

◆ HGLOBALLockBytesImpl_WriteAt()

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

Definition at line 328 of file memlockbytes.c.

334{
336
337 void* supportBuffer;
338 ULARGE_INTEGER newSize;
340
341 /*
342 * If the caller is not interested in the number of bytes written,
343 * we use another buffer to avoid "if" statements in the code.
344 */
345 if (pcbWritten == 0)
346 pcbWritten = &bytesWritten;
347
348 if (cb == 0)
349 {
350 return S_OK;
351 }
352 else
353 {
354 newSize.HighPart = 0;
355 newSize.LowPart = ulOffset.LowPart + cb;
356 }
357
358 /*
359 * Verify if we need to grow the stream
360 */
361 if (newSize.LowPart > This->byteArraySize.LowPart)
362 {
363 /* grow stream */
364 if (ILockBytes_SetSize(iface, newSize) == STG_E_MEDIUMFULL)
365 return STG_E_MEDIUMFULL;
366 }
367
368 /*
369 * Lock the buffer in position and copy the data.
370 */
371 supportBuffer = GlobalLock(This->supportHandle);
372
373 memcpy((char *) supportBuffer + ulOffset.LowPart, pv, cb);
374
375 /*
376 * Return the number of bytes written.
377 */
378 *pcbWritten = cb;
379
380 /*
381 * Cleanup
382 */
383 GlobalUnlock(This->supportHandle);
384
385 return S_OK;
386}
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:220
static ULONG WINAPI HGLOBALLockBytesImpl_Release(ILockBytes *iface)
Definition: memlockbytes.c:230
static HRESULT WINAPI HGLOBALLockBytesImpl_WriteAt(ILockBytes *iface, ULARGE_INTEGER ulOffset, const void *pv, ULONG cb, ULONG *pcbWritten)
Definition: memlockbytes.c:328
static HRESULT WINAPI HGLOBALLockBytesImpl_Stat(ILockBytes *iface, STATSTG *pstatstg, DWORD grfStatFlag)
Definition: memlockbytes.c:475
static HRESULT WINAPI HGLOBALLockBytesImpl_ReadAt(ILockBytes *iface, ULARGE_INTEGER ulOffset, void *pv, ULONG cb, ULONG *pcbRead)
Definition: memlockbytes.c:257
static HRESULT WINAPI HGLOBALLockBytesImpl_LockRegion(ILockBytes *iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
Definition: memlockbytes.c:442
static HRESULT WINAPI HGLOBALLockBytesImpl_UnlockRegion(ILockBytes *iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
Definition: memlockbytes.c:458
static HRESULT WINAPI HGLOBALLockBytesImpl_QueryInterface(ILockBytes *iface, REFIID riid, void **ppvObject)
Definition: memlockbytes.c:191
static HRESULT WINAPI HGLOBALLockBytesImpl_Flush(ILockBytes *iface)
Definition: memlockbytes.c:393
static HRESULT WINAPI HGLOBALLockBytesImpl_SetSize(ILockBytes *iface, ULARGE_INTEGER libNewSize)
Definition: memlockbytes.c:405

Definition at line 73 of file memlockbytes.c.

Referenced by CreateILockBytesOnHGlobal(), and GetHGlobalFromILockBytes().