1999-01-24 19:52:55 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* OLE Automation
|
|
|
|
* SafeArray Implementation
|
|
|
|
*
|
|
|
|
* This file contains the implementation of the SafeArray interface.
|
|
|
|
*
|
|
|
|
* Copyright 1999 Sylvain St-Germain
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
1999-01-24 19:52:55 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
1999-03-25 16:52:09 +01:00
|
|
|
#include <string.h>
|
1999-03-14 17:35:05 +01:00
|
|
|
#include "windef.h"
|
1999-02-28 20:14:33 +01:00
|
|
|
#include "winerror.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "oleauto.h"
|
2002-03-10 00:29:33 +01:00
|
|
|
#include "wine/debug.h"
|
1999-01-24 19:52:55 +01:00
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(ole);
|
1999-04-19 16:56:29 +02:00
|
|
|
|
2001-09-21 23:01:31 +02:00
|
|
|
#define SYSDUPSTRING(str) SysAllocStringLen((str), SysStringLen(str))
|
|
|
|
|
2002-01-29 03:44:44 +01:00
|
|
|
/* Locally used methods */
|
2002-06-01 01:06:46 +02:00
|
|
|
static INT
|
1999-01-24 19:52:55 +01:00
|
|
|
endOfDim(LONG *coor, SAFEARRAYBOUND *mat, LONG dim, LONG realDim);
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
static ULONG
|
1999-01-24 19:52:55 +01:00
|
|
|
calcDisplacement(LONG *coor, SAFEARRAYBOUND *mat, LONG dim);
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
static BOOL
|
1999-01-24 19:52:55 +01:00
|
|
|
isPointer(USHORT feature);
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
static INT
|
1999-01-24 19:52:55 +01:00
|
|
|
getFeatures(VARTYPE vt);
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
static BOOL
|
1999-01-24 19:52:55 +01:00
|
|
|
validCoordinate(LONG *coor, SAFEARRAY *psa);
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
static BOOL
|
1999-01-24 19:52:55 +01:00
|
|
|
resizeSafeArray(SAFEARRAY *psa, LONG lDelta);
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
static BOOL
|
1999-01-24 19:52:55 +01:00
|
|
|
validArg(SAFEARRAY *psa);
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
static ULONG
|
1999-01-24 19:52:55 +01:00
|
|
|
getArraySize(SAFEARRAY *psa);
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
static HRESULT
|
1999-01-24 19:52:55 +01:00
|
|
|
duplicateData(SAFEARRAY *psa, SAFEARRAY **ppsaOut);
|
|
|
|
|
|
|
|
/* Association between VARTYPE and their size.
|
|
|
|
A size of zero is defined for the unsupported types. */
|
|
|
|
|
|
|
|
#define VARTYPE_NOT_SUPPORTED 0
|
2000-10-29 19:06:04 +01:00
|
|
|
static const ULONG VARTYPE_SIZE[] =
|
1999-01-24 19:52:55 +01:00
|
|
|
{
|
|
|
|
/* this is taken from wtypes.h. Only [S]es are supported by the SafeArray */
|
2001-09-21 23:01:31 +02:00
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_EMPTY [V] [P] nothing */
|
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_NULL [V] [P] SQL style Nul */
|
|
|
|
2, /* VT_I2 [V][T][P][S] 2 byte signed int */
|
|
|
|
4, /* VT_I4 [V][T][P][S] 4 byte signed int */
|
|
|
|
4, /* VT_R4 [V][T][P][S] 4 byte real */
|
|
|
|
8, /* VT_R8 [V][T][P][S] 8 byte real */
|
1999-01-24 19:52:55 +01:00
|
|
|
8, /* VT_CY [V][T][P][S] currency */
|
2001-09-21 23:01:31 +02:00
|
|
|
8, /* VT_DATE [V][T][P][S] date */
|
|
|
|
sizeof(BSTR), /* VT_BSTR [V][T][P][S] OLE Automation string*/
|
|
|
|
sizeof(LPDISPATCH), /* VT_DISPATCH [V][T][P][S] IDispatch * */
|
1999-01-24 19:52:55 +01:00
|
|
|
4, /* VT_ERROR [V][T] [S] SCODE */
|
2002-12-23 03:02:49 +01:00
|
|
|
2, /* VT_BOOL [V][T][P][S] True=-1, False=0*/
|
2001-09-21 23:01:31 +02:00
|
|
|
sizeof(VARIANT), /* VT_VARIANT [V][T][P][S] VARIANT * */
|
|
|
|
sizeof(LPUNKNOWN), /* VT_UNKNOWN [V][T] [S] IUnknown * */
|
|
|
|
sizeof(DECIMAL), /* VT_DECIMAL [V][T] [S] 16 byte fixed point */
|
2002-12-23 03:02:49 +01:00
|
|
|
VARTYPE_NOT_SUPPORTED, /* no VARTYPE here..... */
|
|
|
|
1, /* VT_I1 [T] [S] signed char */
|
|
|
|
1, /* VT_UI1 [V][T][P][S] unsigned char */
|
|
|
|
2, /* VT_UI2 [T][P][S] unsigned short */
|
|
|
|
4, /* VT_UI4 [T][P][S] unsigned int */
|
1999-01-24 19:52:55 +01:00
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_I8 [T][P] signed 64-bit int */
|
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_UI8 [T][P] unsigned 64-bit int */
|
2002-12-23 03:02:49 +01:00
|
|
|
sizeof(INT), /* VT_INT [T] signed machine int */
|
|
|
|
sizeof(UINT), /* VT_UINT [T] unsigned machine int */
|
1999-01-24 19:52:55 +01:00
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_VOID [T] C style void */
|
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_HRESULT [T] Standard return type */
|
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_PTR [T] pointer type */
|
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_SAFEARRAY [T] (use VT_ARRAY in VARIANT)*/
|
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_CARRAY [T] C style array */
|
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_USERDEFINED [T] user defined type */
|
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_LPSTR [T][P] null terminated string */
|
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_LPWSTR [T][P] wide null term string */
|
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_FILETIME [P] FILETIME */
|
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_BLOB [P] Length prefixed bytes */
|
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_STREAM [P] Name of stream follows */
|
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_STORAGE [P] Name of storage follows */
|
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_STREAMED_OBJECT[P] Stream contains an object*/
|
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_STORED_OBJECT [P] Storage contains object*/
|
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_BLOB_OBJECT [P] Blob contains an object*/
|
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_CF [P] Clipboard format */
|
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_CLSID [P] A Class ID */
|
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_VECTOR [P] simple counted array */
|
|
|
|
VARTYPE_NOT_SUPPORTED, /* VT_ARRAY [V] SAFEARRAY* */
|
|
|
|
VARTYPE_NOT_SUPPORTED /* VT_BYREF [V] void* for local use */
|
|
|
|
};
|
|
|
|
|
2002-01-29 03:44:44 +01:00
|
|
|
static const int LAST_VARTYPE = sizeof(VARTYPE_SIZE)/sizeof(VARTYPE_SIZE[0]);
|
1999-03-10 14:25:27 +01:00
|
|
|
|
|
|
|
|
1999-01-24 19:52:55 +01:00
|
|
|
/*************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* SafeArrayAllocDescriptor (OLEAUT32.36)
|
1999-01-24 19:52:55 +01:00
|
|
|
* Allocate the appropriate amount of memory for the SafeArray descriptor
|
|
|
|
*/
|
2002-06-01 01:06:46 +02:00
|
|
|
HRESULT WINAPI SafeArrayAllocDescriptor(
|
|
|
|
UINT cDims,
|
|
|
|
SAFEARRAY **ppsaOut)
|
1999-01-24 19:52:55 +01:00
|
|
|
{
|
|
|
|
SAFEARRAYBOUND *sab;
|
|
|
|
LONG allocSize = 0;
|
|
|
|
|
2002-12-23 03:02:49 +01:00
|
|
|
if (!cDims || cDims >= 0x10000) /* 65536 appears to be the limit */
|
|
|
|
return E_INVALIDARG;
|
|
|
|
if (!ppsaOut)
|
|
|
|
return E_POINTER;
|
|
|
|
|
|
|
|
|
1999-01-24 19:52:55 +01:00
|
|
|
/* SAFEARRAY + SAFEARRAYBOUND * (cDims -1) ( -1 because there is already one
|
|
|
|
( in SAFEARRAY struct */
|
|
|
|
allocSize = sizeof(**ppsaOut) + (sizeof(*sab) * (cDims-1));
|
|
|
|
|
|
|
|
/* Allocate memory for SAFEARRAY struc */
|
2002-06-01 01:06:46 +02:00
|
|
|
if(( (*ppsaOut)=HeapAlloc(
|
1999-01-24 19:52:55 +01:00
|
|
|
GetProcessHeap(), HEAP_ZERO_MEMORY, allocSize)) == NULL){
|
|
|
|
return(E_UNEXPECTED);
|
|
|
|
}
|
2002-12-23 03:02:49 +01:00
|
|
|
(*ppsaOut)->cDims = cDims;
|
|
|
|
TRACE("(%d): %lu bytes allocated for descriptor.\n", cDims, allocSize);
|
1999-01-24 19:52:55 +01:00
|
|
|
|
|
|
|
return(S_OK);
|
|
|
|
}
|
|
|
|
|
2002-01-29 03:44:44 +01:00
|
|
|
/*************************************************************************
|
2002-07-19 02:30:16 +02:00
|
|
|
* SafeArrayAllocDescriptorEx (OLEAUT32.41)
|
2002-01-29 03:44:44 +01:00
|
|
|
* Allocate the appropriate amount of memory for the SafeArray descriptor
|
|
|
|
*
|
|
|
|
* This is a minimal implementation just to get things moving.
|
|
|
|
*
|
|
|
|
* The MSDN documentation on this doesn't tell us much.
|
|
|
|
*/
|
2002-06-01 01:06:46 +02:00
|
|
|
HRESULT WINAPI SafeArrayAllocDescriptorEx(
|
2002-01-29 03:44:44 +01:00
|
|
|
VARTYPE vt,
|
2002-06-01 01:06:46 +02:00
|
|
|
UINT cDims,
|
|
|
|
SAFEARRAY **ppsaOut)
|
2002-01-29 03:44:44 +01:00
|
|
|
{
|
|
|
|
if ( (vt >= LAST_VARTYPE) ||
|
|
|
|
( VARTYPE_SIZE[vt] == VARTYPE_NOT_SUPPORTED ) )
|
|
|
|
return E_UNEXPECTED;
|
|
|
|
|
|
|
|
return SafeArrayAllocDescriptor (cDims, ppsaOut);
|
|
|
|
}
|
|
|
|
|
1999-01-24 19:52:55 +01:00
|
|
|
/*************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* SafeArrayAllocData (OLEAUT32.37)
|
1999-01-24 19:52:55 +01:00
|
|
|
* Allocate the appropriate amount of data for the SafeArray data
|
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
HRESULT WINAPI SafeArrayAllocData(
|
2002-06-01 01:06:46 +02:00
|
|
|
SAFEARRAY *psa)
|
1999-01-24 19:52:55 +01:00
|
|
|
{
|
|
|
|
ULONG ulWholeArraySize; /* to store the size of the whole thing */
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if(! validArg(psa))
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
|
|
|
ulWholeArraySize = getArraySize(psa);
|
|
|
|
|
|
|
|
/* Allocate memory for the data itself */
|
2002-06-01 01:06:46 +02:00
|
|
|
if((psa->pvData = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
|
1999-01-24 19:52:55 +01:00
|
|
|
psa->cbElements*ulWholeArraySize)) == NULL)
|
|
|
|
return(E_UNEXPECTED);
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
TRACE("SafeArray: %lu bytes allocated for data at %p (%lu objects).\n",
|
1999-01-24 19:52:55 +01:00
|
|
|
psa->cbElements*ulWholeArraySize, psa->pvData, ulWholeArraySize);
|
|
|
|
|
|
|
|
return(S_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* SafeArrayCreate (OLEAUT32.15)
|
2002-06-01 01:06:46 +02:00
|
|
|
* Create a SafeArray object by encapsulating AllocDescriptor and AllocData
|
1999-01-24 19:52:55 +01:00
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
SAFEARRAY* WINAPI SafeArrayCreate(
|
2002-06-01 01:06:46 +02:00
|
|
|
VARTYPE vt,
|
|
|
|
UINT cDims,
|
1999-01-24 19:52:55 +01:00
|
|
|
SAFEARRAYBOUND *rgsabound)
|
|
|
|
{
|
|
|
|
SAFEARRAY *psa;
|
|
|
|
HRESULT hRes;
|
|
|
|
USHORT cDim;
|
|
|
|
|
|
|
|
/* Validate supported VARTYPE */
|
1999-03-10 14:25:27 +01:00
|
|
|
if ( (vt >= LAST_VARTYPE) ||
|
|
|
|
( VARTYPE_SIZE[vt] == VARTYPE_NOT_SUPPORTED ) )
|
1999-01-24 19:52:55 +01:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Allocate memory for the array descriptor */
|
1999-02-26 12:11:13 +01:00
|
|
|
if( FAILED( hRes = SafeArrayAllocDescriptor(cDims, &psa)))
|
1999-01-24 19:52:55 +01:00
|
|
|
return NULL;
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/* setup data members... */
|
1999-01-24 19:52:55 +01:00
|
|
|
psa->cDims = cDims;
|
|
|
|
psa->fFeatures = getFeatures(vt);
|
|
|
|
psa->cLocks = 0;
|
|
|
|
psa->pvData = NULL;
|
|
|
|
psa->cbElements= VARTYPE_SIZE[vt];
|
|
|
|
|
|
|
|
/* Invert the bounds ... */
|
|
|
|
for(cDim=0; cDim < psa->cDims; cDim++) {
|
|
|
|
psa->rgsabound[cDim].cElements = rgsabound[psa->cDims-cDim-1].cElements;
|
|
|
|
psa->rgsabound[cDim].lLbound = rgsabound[psa->cDims-cDim-1].lLbound;
|
|
|
|
}
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/* allocate memory for the data... */
|
1999-02-26 12:11:13 +01:00
|
|
|
if( FAILED( hRes = SafeArrayAllocData(psa))) {
|
2002-06-01 01:06:46 +02:00
|
|
|
SafeArrayDestroyDescriptor(psa);
|
1999-07-04 18:02:24 +02:00
|
|
|
ERR("() : Failed to allocate the Safe Array data\n");
|
1999-01-24 19:52:55 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
return(psa);
|
1999-01-24 19:52:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* SafeArrayDestroyDescriptor (OLEAUT32.38)
|
1999-01-24 19:52:55 +01:00
|
|
|
* Frees the memory associated with the descriptor.
|
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
HRESULT WINAPI SafeArrayDestroyDescriptor(
|
1999-01-24 19:52:55 +01:00
|
|
|
SAFEARRAY *psa)
|
|
|
|
{
|
|
|
|
/* Check for lockness before to free... */
|
2002-06-01 01:06:46 +02:00
|
|
|
if(psa->cLocks > 0)
|
1999-01-24 19:52:55 +01:00
|
|
|
return DISP_E_ARRAYISLOCKED;
|
|
|
|
|
|
|
|
/* The array is unlocked, then, deallocate memory */
|
2002-06-01 01:06:46 +02:00
|
|
|
if(HeapFree( GetProcessHeap(), 0, psa) == FALSE)
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_UNEXPECTED;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-01-24 19:52:55 +01:00
|
|
|
return(S_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* SafeArrayLock (OLEAUT32.21)
|
1999-01-24 19:52:55 +01:00
|
|
|
* Increment the lock counter
|
|
|
|
*
|
|
|
|
* Doc says (MSDN Library ) that psa->pvData should be made available (!= NULL)
|
2002-06-01 01:06:46 +02:00
|
|
|
* only when psa->cLocks is > 0... I don't get it since pvData is allocated
|
|
|
|
* before the array is locked, therefore
|
1999-01-24 19:52:55 +01:00
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
HRESULT WINAPI SafeArrayLock(
|
1999-01-24 19:52:55 +01:00
|
|
|
SAFEARRAY *psa)
|
|
|
|
{
|
2002-06-01 01:06:46 +02:00
|
|
|
if(! validArg(psa))
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
|
|
|
psa->cLocks++;
|
|
|
|
|
|
|
|
return(S_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* SafeArrayUnlock (OLEAUT32.22)
|
1999-01-24 19:52:55 +01:00
|
|
|
* Decrement the lock counter
|
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
HRESULT WINAPI SafeArrayUnlock(
|
1999-01-24 19:52:55 +01:00
|
|
|
SAFEARRAY *psa)
|
|
|
|
{
|
2002-06-01 01:06:46 +02:00
|
|
|
if(! validArg(psa))
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if (psa->cLocks > 0)
|
1999-01-24 19:52:55 +01:00
|
|
|
psa->cLocks--;
|
|
|
|
|
|
|
|
return(S_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* SafeArrayPutElement (OLEAUT32.26)
|
1999-01-24 19:52:55 +01:00
|
|
|
* Set the data at the given coordinate
|
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
HRESULT WINAPI SafeArrayPutElement(
|
2002-06-01 01:06:46 +02:00
|
|
|
SAFEARRAY *psa,
|
|
|
|
LONG *rgIndices,
|
1999-01-24 19:52:55 +01:00
|
|
|
void *pv)
|
|
|
|
{
|
2002-06-01 01:06:46 +02:00
|
|
|
ULONG stepCountInSAData = 0; /* Number of array item to skip to get to
|
1999-01-24 19:52:55 +01:00
|
|
|
the desired one... */
|
|
|
|
PVOID elementStorageAddress = NULL; /* Adress to store the data */
|
|
|
|
|
|
|
|
/* Validate the index given */
|
2002-06-01 01:06:46 +02:00
|
|
|
if(! validCoordinate(rgIndices, psa))
|
1999-01-24 19:52:55 +01:00
|
|
|
return DISP_E_BADINDEX;
|
|
|
|
if(! validArg(psa))
|
|
|
|
return E_INVALIDARG;
|
|
|
|
|
1999-02-26 12:11:13 +01:00
|
|
|
if( SafeArrayLock(psa) == S_OK) {
|
1999-01-24 19:52:55 +01:00
|
|
|
|
|
|
|
/* Figure out the number of items to skip */
|
|
|
|
stepCountInSAData = calcDisplacement(rgIndices, psa->rgsabound, psa->cDims);
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-01-24 19:52:55 +01:00
|
|
|
/* Figure out the number of byte to skip ... */
|
1999-04-25 21:01:52 +02:00
|
|
|
elementStorageAddress = (char *) psa->pvData+(stepCountInSAData*psa->cbElements);
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-01-24 19:52:55 +01:00
|
|
|
if(isPointer(psa->fFeatures)) { /* increment ref count for this pointer */
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
*((PVOID*)elementStorageAddress) = *(PVOID*)pv;
|
1999-01-24 19:52:55 +01:00
|
|
|
IUnknown_AddRef( *(IUnknown**)pv);
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
} else {
|
1999-01-24 19:52:55 +01:00
|
|
|
|
|
|
|
if(psa->fFeatures == FADF_BSTR) { /* Create a new object */
|
2001-09-21 23:01:31 +02:00
|
|
|
BSTR pbstrReAllocStr = NULL;
|
|
|
|
if(pv &&
|
|
|
|
((pbstrReAllocStr = SYSDUPSTRING( (OLECHAR*)pv )) == NULL)) {
|
2002-06-01 01:06:46 +02:00
|
|
|
SafeArrayUnlock(psa);
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_OUTOFMEMORY;
|
2002-06-01 01:06:46 +02:00
|
|
|
} else
|
1999-02-26 12:11:13 +01:00
|
|
|
*((BSTR*)elementStorageAddress) = pbstrReAllocStr;
|
2001-09-21 23:01:31 +02:00
|
|
|
}
|
|
|
|
else if(psa->fFeatures == FADF_VARIANT) {
|
|
|
|
HRESULT hr = VariantCopy(elementStorageAddress, pv);
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
SafeArrayUnlock(psa);
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* duplicate the memory */
|
1999-02-26 12:11:13 +01:00
|
|
|
memcpy(elementStorageAddress, pv, SafeArrayGetElemsize(psa) );
|
1999-01-24 19:52:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
1999-07-04 18:02:24 +02:00
|
|
|
ERR("SafeArray: Cannot lock array....\n");
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_UNEXPECTED; /* UNDOC error condition */
|
|
|
|
}
|
|
|
|
|
1999-07-04 18:02:24 +02:00
|
|
|
TRACE("SafeArray: item put at adress %p.\n",elementStorageAddress);
|
2002-06-01 01:06:46 +02:00
|
|
|
return SafeArrayUnlock(psa);
|
1999-01-24 19:52:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* SafeArrayGetElement (OLEAUT32.25)
|
1999-01-24 19:52:55 +01:00
|
|
|
* Return the data element corresponding the the given coordinate
|
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
HRESULT WINAPI SafeArrayGetElement(
|
2002-06-01 01:06:46 +02:00
|
|
|
SAFEARRAY *psa,
|
|
|
|
LONG *rgIndices,
|
1999-01-24 19:52:55 +01:00
|
|
|
void *pv)
|
|
|
|
{
|
2002-06-01 01:06:46 +02:00
|
|
|
ULONG stepCountInSAData = 0; /* Number of array item to skip to get to
|
1999-01-24 19:52:55 +01:00
|
|
|
the desired one... */
|
|
|
|
PVOID elementStorageAddress = NULL; /* Adress to store the data */
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if(! validArg(psa))
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_INVALIDARG;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-01-24 19:52:55 +01:00
|
|
|
if(! validCoordinate(rgIndices, psa)) /* Validate the index given */
|
|
|
|
return(DISP_E_BADINDEX);
|
|
|
|
|
1999-02-26 12:11:13 +01:00
|
|
|
if( SafeArrayLock(psa) == S_OK) {
|
1999-01-24 19:52:55 +01:00
|
|
|
|
|
|
|
/* Figure out the number of items to skip */
|
|
|
|
stepCountInSAData = calcDisplacement(rgIndices, psa->rgsabound, psa->cDims);
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-01-24 19:52:55 +01:00
|
|
|
/* Figure out the number of byte to skip ... */
|
1999-04-25 21:01:52 +02:00
|
|
|
elementStorageAddress = (char *) psa->pvData+(stepCountInSAData*psa->cbElements);
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-01-24 19:52:55 +01:00
|
|
|
if( psa->fFeatures == FADF_BSTR) { /* reallocate the obj */
|
2001-09-21 23:01:31 +02:00
|
|
|
BSTR pbstrStoredStr = *(OLECHAR**)elementStorageAddress;
|
|
|
|
BSTR pbstrReturnedStr = NULL;
|
|
|
|
if( pbstrStoredStr &&
|
|
|
|
((pbstrReturnedStr = SYSDUPSTRING( pbstrStoredStr )) == NULL) ) {
|
1999-02-26 12:11:13 +01:00
|
|
|
SafeArrayUnlock(psa);
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_OUTOFMEMORY;
|
2002-06-01 01:06:46 +02:00
|
|
|
} else
|
|
|
|
*((BSTR*)pv) = pbstrReturnedStr;
|
2001-09-21 23:01:31 +02:00
|
|
|
}
|
|
|
|
else if( psa->fFeatures == FADF_VARIANT) {
|
2002-01-02 22:44:49 +01:00
|
|
|
HRESULT hr;
|
|
|
|
VariantInit(pv);
|
|
|
|
hr = VariantCopy(pv, elementStorageAddress);
|
2001-09-21 23:01:31 +02:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
SafeArrayUnlock(psa);
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( isPointer(psa->fFeatures) ) /* simply copy the pointer */
|
2002-06-01 01:06:46 +02:00
|
|
|
*(PVOID*)pv = *((PVOID*)elementStorageAddress);
|
1999-01-24 19:52:55 +01:00
|
|
|
else /* copy the bytes */
|
2001-09-21 23:01:31 +02:00
|
|
|
memcpy(pv, elementStorageAddress, psa->cbElements );
|
1999-01-24 19:52:55 +01:00
|
|
|
|
|
|
|
} else {
|
1999-07-04 18:02:24 +02:00
|
|
|
ERR("SafeArray: Cannot lock array....\n");
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_UNEXPECTED; /* UNDOC error condition */
|
|
|
|
}
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
return( SafeArrayUnlock(psa) );
|
1999-01-24 19:52:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* SafeArrayGetUBound (OLEAUT32.19)
|
1999-01-24 19:52:55 +01:00
|
|
|
* return the UP bound for a given array dimension
|
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
HRESULT WINAPI SafeArrayGetUBound(
|
2002-06-01 01:06:46 +02:00
|
|
|
SAFEARRAY *psa,
|
1999-02-26 12:11:13 +01:00
|
|
|
UINT nDim,
|
1999-01-24 19:52:55 +01:00
|
|
|
LONG *plUbound)
|
|
|
|
{
|
2002-06-01 01:06:46 +02:00
|
|
|
if(! validArg(psa))
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if(nDim > psa->cDims)
|
1999-01-24 19:52:55 +01:00
|
|
|
return DISP_E_BADINDEX;
|
|
|
|
|
2000-12-21 21:18:18 +01:00
|
|
|
if(0 == nDim)
|
|
|
|
return DISP_E_BADINDEX;
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
*plUbound = psa->rgsabound[nDim-1].lLbound +
|
1999-03-10 14:25:27 +01:00
|
|
|
psa->rgsabound[nDim-1].cElements - 1;
|
1999-01-24 19:52:55 +01:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* SafeArrayGetLBound (OLEAUT32.20)
|
2002-06-01 01:06:46 +02:00
|
|
|
* Return the LO bound for a given array dimension
|
1999-01-24 19:52:55 +01:00
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
HRESULT WINAPI SafeArrayGetLBound(
|
1999-01-24 19:52:55 +01:00
|
|
|
SAFEARRAY *psa,
|
2002-06-01 01:06:46 +02:00
|
|
|
UINT nDim,
|
1999-01-24 19:52:55 +01:00
|
|
|
LONG *plLbound)
|
|
|
|
{
|
2002-06-01 01:06:46 +02:00
|
|
|
if(! validArg(psa))
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if(nDim > psa->cDims)
|
1999-01-24 19:52:55 +01:00
|
|
|
return DISP_E_BADINDEX;
|
|
|
|
|
2000-12-21 21:18:18 +01:00
|
|
|
if(0 == nDim)
|
|
|
|
return DISP_E_BADINDEX;
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-03-10 14:25:27 +01:00
|
|
|
*plLbound = psa->rgsabound[nDim-1].lLbound;
|
1999-01-24 19:52:55 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* SafeArrayGetDim (OLEAUT32.17)
|
1999-01-24 19:52:55 +01:00
|
|
|
* returns the number of dimension in the array
|
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
UINT WINAPI SafeArrayGetDim(
|
1999-01-24 19:52:55 +01:00
|
|
|
SAFEARRAY * psa)
|
2002-06-01 01:06:46 +02:00
|
|
|
{
|
1999-02-28 11:07:12 +01:00
|
|
|
/*
|
|
|
|
* A quick test in Windows shows that the behavior here for an invalid
|
|
|
|
* pointer is to return 0.
|
|
|
|
*/
|
2002-06-01 01:06:46 +02:00
|
|
|
if(! validArg(psa))
|
1999-02-28 11:07:12 +01:00
|
|
|
return 0;
|
|
|
|
|
1999-01-24 19:52:55 +01:00
|
|
|
return psa->cDims;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* SafeArrayGetElemsize (OLEAUT32.18)
|
1999-01-24 19:52:55 +01:00
|
|
|
* Return the size of the element in the array
|
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
UINT WINAPI SafeArrayGetElemsize(
|
1999-01-24 19:52:55 +01:00
|
|
|
SAFEARRAY * psa)
|
2002-06-01 01:06:46 +02:00
|
|
|
{
|
1999-02-28 11:07:12 +01:00
|
|
|
/*
|
|
|
|
* A quick test in Windows shows that the behavior here for an invalid
|
|
|
|
* pointer is to return 0.
|
|
|
|
*/
|
2002-06-01 01:06:46 +02:00
|
|
|
if(! validArg(psa))
|
1999-02-28 11:07:12 +01:00
|
|
|
return 0;
|
|
|
|
|
1999-01-24 19:52:55 +01:00
|
|
|
return psa->cbElements;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* SafeArrayAccessData (OLEAUT32.23)
|
2002-06-01 01:06:46 +02:00
|
|
|
* increment the access count and return the data
|
1999-01-24 19:52:55 +01:00
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
HRESULT WINAPI SafeArrayAccessData(
|
2002-06-01 01:06:46 +02:00
|
|
|
SAFEARRAY *psa,
|
1999-01-24 19:52:55 +01:00
|
|
|
void **ppvData)
|
2002-06-01 01:06:46 +02:00
|
|
|
{
|
1999-01-24 19:52:55 +01:00
|
|
|
HRESULT hRes;
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if(! validArg(psa))
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
1999-02-26 12:11:13 +01:00
|
|
|
hRes = SafeArrayLock(psa);
|
1999-01-24 19:52:55 +01:00
|
|
|
|
|
|
|
switch (hRes) {
|
2002-06-01 01:06:46 +02:00
|
|
|
case S_OK:
|
1999-01-24 19:52:55 +01:00
|
|
|
(*ppvData) = psa->pvData;
|
|
|
|
break;
|
|
|
|
case E_INVALIDARG:
|
|
|
|
(*ppvData) = NULL;
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-01-24 19:52:55 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* SafeArrayUnaccessData (OLEAUT32.24)
|
1999-01-24 19:52:55 +01:00
|
|
|
* Decrement the access count
|
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
HRESULT WINAPI SafeArrayUnaccessData(
|
1999-01-24 19:52:55 +01:00
|
|
|
SAFEARRAY * psa)
|
2002-06-01 01:06:46 +02:00
|
|
|
{
|
|
|
|
if(! validArg(psa))
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
1999-02-26 12:11:13 +01:00
|
|
|
return(SafeArrayUnlock(psa));
|
1999-01-24 19:52:55 +01:00
|
|
|
}
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* SafeArrayPtrOfIndex (OLEAUT32.148)
|
1999-01-24 19:52:55 +01:00
|
|
|
* Return a pointer to the element at rgIndices
|
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
HRESULT WINAPI SafeArrayPtrOfIndex(
|
2002-06-01 01:06:46 +02:00
|
|
|
SAFEARRAY *psa,
|
|
|
|
LONG *rgIndices,
|
1999-01-24 19:52:55 +01:00
|
|
|
void **ppvData)
|
2002-06-01 01:06:46 +02:00
|
|
|
{
|
|
|
|
ULONG stepCountInSAData = 0; /* Number of array item to skip to get to
|
1999-01-24 19:52:55 +01:00
|
|
|
the desired one... */
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if(! validArg(psa))
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if(! validCoordinate(rgIndices, psa))
|
1999-01-24 19:52:55 +01:00
|
|
|
return DISP_E_BADINDEX;
|
|
|
|
|
2002-01-29 03:44:44 +01:00
|
|
|
/* Although it is dangerous to do this without having a lock, it is not
|
|
|
|
* illegal. Microsoft do warn of the danger.
|
|
|
|
*/
|
|
|
|
|
1999-01-24 19:52:55 +01:00
|
|
|
/* Figure out the number of items to skip */
|
|
|
|
stepCountInSAData = calcDisplacement(rgIndices, psa->rgsabound, psa->cDims);
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-04-25 21:01:52 +02:00
|
|
|
*ppvData = (char *) psa->pvData+(stepCountInSAData*psa->cbElements);
|
1999-01-24 19:52:55 +01:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* SafeArrayDestroyData (OLEAUT32.39)
|
1999-01-24 19:52:55 +01:00
|
|
|
* Frees the memory data bloc
|
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
HRESULT WINAPI SafeArrayDestroyData(
|
1999-01-24 19:52:55 +01:00
|
|
|
SAFEARRAY *psa)
|
2002-06-01 01:06:46 +02:00
|
|
|
{
|
1999-01-24 19:52:55 +01:00
|
|
|
HRESULT hRes;
|
|
|
|
ULONG ulWholeArraySize; /* count spot in array */
|
|
|
|
ULONG ulDataIter; /* to iterate the data space */
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if(! validArg(psa))
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if(psa->cLocks > 0)
|
1999-01-24 19:52:55 +01:00
|
|
|
return DISP_E_ARRAYISLOCKED;
|
|
|
|
|
2002-12-12 23:59:07 +01:00
|
|
|
if(psa->pvData==NULL)
|
|
|
|
return S_OK;
|
|
|
|
|
1999-01-24 19:52:55 +01:00
|
|
|
ulWholeArraySize = getArraySize(psa);
|
|
|
|
|
|
|
|
if(isPointer(psa->fFeatures)) { /* release the pointers */
|
2001-09-21 23:01:31 +02:00
|
|
|
IUnknown *punk;
|
1999-01-24 19:52:55 +01:00
|
|
|
|
|
|
|
for(ulDataIter=0; ulDataIter < ulWholeArraySize; ulDataIter++) {
|
2002-06-01 01:06:46 +02:00
|
|
|
punk = *(IUnknown**)((char *) psa->pvData+(ulDataIter*(psa->cbElements)));
|
1999-01-24 19:52:55 +01:00
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if( punk != NULL)
|
1999-01-24 19:52:55 +01:00
|
|
|
IUnknown_Release(punk);
|
|
|
|
}
|
|
|
|
|
2001-09-21 23:01:31 +02:00
|
|
|
}
|
|
|
|
else if(psa->fFeatures & FADF_BSTR) { /* deallocate the obj */
|
|
|
|
BSTR bstr;
|
1999-01-24 19:52:55 +01:00
|
|
|
|
|
|
|
for(ulDataIter=0; ulDataIter < ulWholeArraySize; ulDataIter++) {
|
1999-04-25 21:01:52 +02:00
|
|
|
bstr = *(BSTR*)((char *) psa->pvData+(ulDataIter*(psa->cbElements)));
|
1999-01-24 19:52:55 +01:00
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if( bstr != NULL)
|
1999-02-26 12:11:13 +01:00
|
|
|
SysFreeString( bstr );
|
1999-01-24 19:52:55 +01:00
|
|
|
}
|
|
|
|
}
|
2001-09-21 23:01:31 +02:00
|
|
|
else if(psa->fFeatures & FADF_VARIANT) { /* deallocate the obj */
|
|
|
|
|
|
|
|
for(ulDataIter=0; ulDataIter < ulWholeArraySize; ulDataIter++) {
|
|
|
|
VariantClear((VARIANT*)((char *) psa->pvData+(ulDataIter*(psa->cbElements))));
|
|
|
|
}
|
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
|
|
|
|
/* check if this array is a Vector, in which case do not free the data
|
1999-01-24 19:52:55 +01:00
|
|
|
block since it has been allocated by AllocDescriptor and therefore
|
|
|
|
deserve to be freed by DestroyDescriptor */
|
2000-06-18 21:29:40 +02:00
|
|
|
if(!(psa->fFeatures & FADF_CREATEVECTOR)) { /* Set when we do CreateVector */
|
1999-01-24 19:52:55 +01:00
|
|
|
|
|
|
|
/* free the whole chunk */
|
|
|
|
if((hRes = HeapFree( GetProcessHeap(), 0, psa->pvData)) == 0) /*falied*/
|
|
|
|
return E_UNEXPECTED; /* UNDOC error condition */
|
|
|
|
|
|
|
|
psa->pvData = NULL;
|
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-01-24 19:52:55 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* SafeArrayCopyData (OLEAUT32.412)
|
1999-01-24 19:52:55 +01:00
|
|
|
* Copy the psaSource's data block into psaTarget if dimension and size
|
|
|
|
* permits it.
|
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
HRESULT WINAPI SafeArrayCopyData(
|
1999-01-24 19:52:55 +01:00
|
|
|
SAFEARRAY *psaSource,
|
|
|
|
SAFEARRAY **psaTarget)
|
2002-06-01 01:06:46 +02:00
|
|
|
{
|
1999-01-24 19:52:55 +01:00
|
|
|
USHORT cDimCount; /* looper */
|
|
|
|
LONG lDelta; /* looper */
|
2002-06-01 01:06:46 +02:00
|
|
|
IUnknown *punk;
|
1999-01-24 19:52:55 +01:00
|
|
|
ULONG ulWholeArraySize; /* Number of item in SA */
|
1999-02-26 12:11:13 +01:00
|
|
|
BSTR bstr;
|
1999-01-24 19:52:55 +01:00
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if(! (validArg(psaSource) && validArg(*psaTarget)) )
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
1999-02-26 12:11:13 +01:00
|
|
|
if(SafeArrayGetDim(psaSource) != SafeArrayGetDim(*psaTarget))
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
ulWholeArraySize = getArraySize(psaSource);
|
1999-01-24 19:52:55 +01:00
|
|
|
|
|
|
|
/* The two arrays boundaries must be of same lenght */
|
|
|
|
for(cDimCount=0;cDimCount < psaSource->cDims; cDimCount++)
|
2002-06-01 01:06:46 +02:00
|
|
|
if( psaSource->rgsabound[cDimCount].cElements !=
|
1999-01-24 19:52:55 +01:00
|
|
|
(*psaTarget)->rgsabound[cDimCount].cElements)
|
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if( isPointer((*psaTarget)->fFeatures) ) { /* the target contains ptr
|
1999-01-24 19:52:55 +01:00
|
|
|
that must be released */
|
|
|
|
for(lDelta=0;lDelta < ulWholeArraySize; lDelta++) {
|
|
|
|
punk = *(IUnknown**)
|
1999-04-25 21:01:52 +02:00
|
|
|
((char *) (*psaTarget)->pvData + (lDelta * (*psaTarget)->cbElements));
|
1999-01-24 19:52:55 +01:00
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if( punk != NULL)
|
1999-01-24 19:52:55 +01:00
|
|
|
IUnknown_Release(punk);
|
|
|
|
}
|
|
|
|
|
2001-09-21 23:01:31 +02:00
|
|
|
}
|
|
|
|
else if( (*psaTarget)->fFeatures & FADF_BSTR) { /* the target contain BSTR
|
2002-06-01 01:06:46 +02:00
|
|
|
that must be freed */
|
1999-01-24 19:52:55 +01:00
|
|
|
for(lDelta=0;lDelta < ulWholeArraySize; lDelta++) {
|
2002-06-01 01:06:46 +02:00
|
|
|
bstr =
|
1999-04-25 21:01:52 +02:00
|
|
|
*(BSTR*)((char *) (*psaTarget)->pvData + (lDelta * (*psaTarget)->cbElements));
|
1999-01-24 19:52:55 +01:00
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if( bstr != NULL)
|
1999-02-26 12:11:13 +01:00
|
|
|
SysFreeString( bstr );
|
1999-01-24 19:52:55 +01:00
|
|
|
}
|
|
|
|
}
|
2001-09-21 23:01:31 +02:00
|
|
|
else if( (*psaTarget)->fFeatures & FADF_VARIANT) {
|
|
|
|
|
|
|
|
for(lDelta=0;lDelta < ulWholeArraySize; lDelta++) {
|
|
|
|
VariantClear((VARIANT*)((char *) (*psaTarget)->pvData + (lDelta * (*psaTarget)->cbElements)));
|
|
|
|
}
|
|
|
|
}
|
1999-01-24 19:52:55 +01:00
|
|
|
|
|
|
|
return duplicateData(psaSource, psaTarget);
|
|
|
|
}
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* SafeArrayDestroy (OLEAUT32.16)
|
1999-01-24 19:52:55 +01:00
|
|
|
* Deallocates all memory reserved for the SafeArray
|
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
HRESULT WINAPI SafeArrayDestroy(
|
1999-01-24 19:52:55 +01:00
|
|
|
SAFEARRAY * psa)
|
2002-06-01 01:06:46 +02:00
|
|
|
{
|
1999-01-24 19:52:55 +01:00
|
|
|
HRESULT hRes;
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if(! validArg(psa))
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if(psa->cLocks > 0)
|
1999-01-24 19:52:55 +01:00
|
|
|
return DISP_E_ARRAYISLOCKED;
|
|
|
|
|
1999-02-26 12:11:13 +01:00
|
|
|
if((hRes = SafeArrayDestroyData( psa )) == S_OK)
|
|
|
|
if((hRes = SafeArrayDestroyDescriptor( psa )) == S_OK)
|
1999-01-24 19:52:55 +01:00
|
|
|
return S_OK;
|
|
|
|
|
|
|
|
return E_UNEXPECTED; /* UNDOC error condition */
|
|
|
|
}
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* SafeArrayCopy (OLEAUT32.27)
|
1999-01-24 19:52:55 +01:00
|
|
|
* Make a dupplicate of a SafeArray
|
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
HRESULT WINAPI SafeArrayCopy(
|
2002-06-01 01:06:46 +02:00
|
|
|
SAFEARRAY *psa,
|
1999-01-24 19:52:55 +01:00
|
|
|
SAFEARRAY **ppsaOut)
|
2002-06-01 01:06:46 +02:00
|
|
|
{
|
1999-01-24 19:52:55 +01:00
|
|
|
HRESULT hRes;
|
|
|
|
DWORD dAllocSize;
|
2000-06-18 21:29:40 +02:00
|
|
|
ULONG ulWholeArraySize; /* size of the thing */
|
1999-01-24 19:52:55 +01:00
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if(! validArg(psa))
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
1999-02-26 12:11:13 +01:00
|
|
|
if((hRes=SafeArrayAllocDescriptor(psa->cDims, ppsaOut)) == S_OK){
|
1999-01-24 19:52:55 +01:00
|
|
|
|
|
|
|
/* Duplicate the SAFEARRAY struc */
|
2002-06-01 01:06:46 +02:00
|
|
|
memcpy(*ppsaOut, psa,
|
1999-01-24 19:52:55 +01:00
|
|
|
sizeof(*psa)+(sizeof(*(psa->rgsabound))*(psa->cDims-1)));
|
|
|
|
|
|
|
|
(*ppsaOut)->pvData = NULL; /* do not point to the same data area */
|
2000-06-18 21:29:40 +02:00
|
|
|
|
|
|
|
/* make sure the new safe array doesn't have the FADF_CREATEVECTOR flag,
|
|
|
|
because the data has not been allocated with the descriptor. */
|
2002-06-01 01:06:46 +02:00
|
|
|
(*ppsaOut)->fFeatures &= ~FADF_CREATEVECTOR;
|
|
|
|
|
|
|
|
/* Get the allocated memory size for source and allocate it for target */
|
2000-06-18 21:29:40 +02:00
|
|
|
ulWholeArraySize = getArraySize(psa); /* Number of item in SA */
|
|
|
|
dAllocSize = ulWholeArraySize*psa->cbElements;
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
(*ppsaOut)->pvData =
|
1999-01-24 19:52:55 +01:00
|
|
|
HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dAllocSize);
|
|
|
|
if( (*ppsaOut)->pvData != NULL) { /* HeapAlloc succeed */
|
|
|
|
|
|
|
|
if( (hRes=duplicateData(psa, ppsaOut)) != S_OK) { /* E_OUTOFMEMORY */
|
|
|
|
HeapFree(GetProcessHeap(), 0, (*ppsaOut)->pvData);
|
|
|
|
(*ppsaOut)->pvData = NULL;
|
1999-02-26 12:11:13 +01:00
|
|
|
SafeArrayDestroyDescriptor(*ppsaOut);
|
1999-01-24 19:52:55 +01:00
|
|
|
return hRes;
|
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-01-24 19:52:55 +01:00
|
|
|
} else { /* failed to allocate or dupplicate... */
|
1999-02-26 12:11:13 +01:00
|
|
|
SafeArrayDestroyDescriptor(*ppsaOut);
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_UNEXPECTED; /* UNDOC error condition */
|
|
|
|
}
|
|
|
|
} else { /* failed to allocate mem for descriptor */
|
|
|
|
return E_OUTOFMEMORY; /* UNDOC error condiftion */
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* SafeArrayCreateVector (OLEAUT32.411)
|
2002-06-01 01:06:46 +02:00
|
|
|
* Creates a one dimension safearray where the data is next to the
|
1999-01-24 19:52:55 +01:00
|
|
|
* SAFEARRAY structure.
|
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
SAFEARRAY* WINAPI SafeArrayCreateVector(
|
2002-06-01 01:06:46 +02:00
|
|
|
VARTYPE vt,
|
|
|
|
LONG lLbound,
|
|
|
|
ULONG cElements)
|
|
|
|
{
|
1999-01-24 19:52:55 +01:00
|
|
|
SAFEARRAY *psa;
|
|
|
|
|
|
|
|
/* Validate supported VARTYPE */
|
1999-03-10 14:25:27 +01:00
|
|
|
if ( (vt >= LAST_VARTYPE) ||
|
|
|
|
( VARTYPE_SIZE[vt] == VARTYPE_NOT_SUPPORTED ) )
|
1999-01-24 19:52:55 +01:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Allocate memory for the array descriptor and data contiguously */
|
2002-06-01 01:06:46 +02:00
|
|
|
if( FAILED( psa = HeapAlloc( GetProcessHeap(),
|
|
|
|
HEAP_ZERO_MEMORY,
|
1999-01-24 19:52:55 +01:00
|
|
|
(sizeof(*psa) + (VARTYPE_SIZE[vt] * cElements))))) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
|
|
|
|
/* setup data members... */
|
1999-01-24 19:52:55 +01:00
|
|
|
psa->cDims = 1; /* always and forever */
|
2000-06-18 21:29:40 +02:00
|
|
|
psa->fFeatures = getFeatures(vt) | FADF_CREATEVECTOR; /* undocumented flag used by Microsoft */
|
1999-01-24 19:52:55 +01:00
|
|
|
psa->cLocks = 0;
|
2000-06-18 21:29:40 +02:00
|
|
|
psa->pvData = (BYTE*)psa + sizeof(*psa);
|
1999-01-24 19:52:55 +01:00
|
|
|
psa->cbElements = VARTYPE_SIZE[vt];
|
|
|
|
|
|
|
|
psa->rgsabound[0].cElements = cElements;
|
|
|
|
psa->rgsabound[0].lLbound = lLbound;
|
2000-06-18 21:29:40 +02:00
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
return(psa);
|
|
|
|
}
|
1999-01-24 19:52:55 +01:00
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/************************************************************************
|
2001-06-19 20:20:47 +02:00
|
|
|
* SafeArrayRedim (OLEAUT32.40)
|
1999-01-24 19:52:55 +01:00
|
|
|
* Changes the caracteristics of the last dimension of the SafeArray
|
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
HRESULT WINAPI SafeArrayRedim(
|
2002-06-01 01:06:46 +02:00
|
|
|
SAFEARRAY *psa,
|
1999-01-24 19:52:55 +01:00
|
|
|
SAFEARRAYBOUND *psaboundNew)
|
2002-06-01 01:06:46 +02:00
|
|
|
{
|
1999-01-24 19:52:55 +01:00
|
|
|
LONG lDelta; /* hold difference in size */
|
|
|
|
USHORT cDims=1; /* dims counter */
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if( !validArg(psa) )
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if( psa->cLocks > 0 )
|
1999-01-24 19:52:55 +01:00
|
|
|
return DISP_E_ARRAYISLOCKED;
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if( psa->fFeatures & FADF_FIXEDSIZE )
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_INVALIDARG;
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if( SafeArrayLock(psa)==E_UNEXPECTED )
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_UNEXPECTED;/* UNDOC error condition */
|
|
|
|
|
|
|
|
/* find the delta in number of array spot to apply to the new array */
|
|
|
|
lDelta = psaboundNew->cElements - psa->rgsabound[0].cElements;
|
|
|
|
for(; cDims < psa->cDims; cDims++)
|
|
|
|
/* delta in number of spot implied by modifying the last dimension */
|
|
|
|
lDelta *= psa->rgsabound[cDims].cElements;
|
|
|
|
|
2001-10-14 18:07:36 +02:00
|
|
|
TRACE("elements=%ld, Lbound=%ld (delta=%ld)\n", psaboundNew->cElements, psaboundNew->lLbound, lDelta);
|
|
|
|
|
1999-01-24 19:52:55 +01:00
|
|
|
if (lDelta == 0) { ;/* same size, maybe a change of lLbound, just set it */
|
|
|
|
|
|
|
|
} else /* need to enlarge (lDelta +) reduce (lDelta -) */
|
2002-06-01 01:06:46 +02:00
|
|
|
if(! resizeSafeArray(psa, lDelta))
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_UNEXPECTED; /* UNDOC error condition */
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/* the only modifyable dimension sits in [0] as the dimensions were reversed
|
1999-01-24 19:52:55 +01:00
|
|
|
at array creation time... */
|
|
|
|
psa->rgsabound[0].cElements = psaboundNew->cElements;
|
|
|
|
psa->rgsabound[0].lLbound = psaboundNew->lLbound;
|
|
|
|
|
1999-02-26 12:11:13 +01:00
|
|
|
return SafeArrayUnlock(psa);
|
1999-01-24 19:52:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************
|
|
|
|
* NOT WINDOWS API - SafeArray* Utility functions
|
|
|
|
************************************************************************/
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/************************************************************************
|
1999-01-24 19:52:55 +01:00
|
|
|
* Used to validate the SAFEARRAY type of arg
|
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
static BOOL validArg(
|
2002-06-01 01:06:46 +02:00
|
|
|
SAFEARRAY *psa)
|
1999-01-24 19:52:55 +01:00
|
|
|
{
|
|
|
|
SAFEARRAYBOUND *sab;
|
|
|
|
LONG psaSize = 0;
|
|
|
|
LONG descSize = 0;
|
|
|
|
LONG fullSize = 0;
|
|
|
|
|
1999-02-28 11:07:12 +01:00
|
|
|
/*
|
|
|
|
* Let's check for the null pointer just in case.
|
|
|
|
*/
|
|
|
|
if (psa == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
2000-12-09 04:08:02 +01:00
|
|
|
/* Check whether the size of the chunk makes sense... That's the only thing
|
1999-01-24 19:52:55 +01:00
|
|
|
I can think of now... */
|
|
|
|
|
2000-12-09 04:08:02 +01:00
|
|
|
psaSize = HeapSize(GetProcessHeap(), 0, psa);
|
|
|
|
if (psaSize == -1)
|
|
|
|
/* uh, foreign heap. Better don't mess with it ! */
|
|
|
|
return TRUE;
|
1999-01-24 19:52:55 +01:00
|
|
|
|
|
|
|
/* size of the descriptor when the SA is not created with CreateVector */
|
|
|
|
descSize = sizeof(*psa) + (sizeof(*sab) * (psa->cDims-1));
|
|
|
|
|
|
|
|
/* size of the descriptor + data when created with CreateVector */
|
|
|
|
fullSize = sizeof(*psa) + (psa->cbElements * psa->rgsabound[0].cElements);
|
|
|
|
|
1999-03-10 14:25:27 +01:00
|
|
|
return((psaSize >= descSize) || (psaSize >= fullSize));
|
1999-01-24 19:52:55 +01:00
|
|
|
}
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/************************************************************************
|
1999-01-24 19:52:55 +01:00
|
|
|
* Used to reallocate memory
|
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
static BOOL resizeSafeArray(
|
2002-06-01 01:06:46 +02:00
|
|
|
SAFEARRAY *psa,
|
1999-01-24 19:52:55 +01:00
|
|
|
LONG lDelta)
|
|
|
|
{
|
|
|
|
ULONG ulWholeArraySize; /* use as multiplicator */
|
2002-06-01 01:06:46 +02:00
|
|
|
PVOID pvNewBlock = NULL;
|
1999-01-24 19:52:55 +01:00
|
|
|
IUnknown *punk;
|
1999-02-26 12:11:13 +01:00
|
|
|
BSTR bstr;
|
1999-01-24 19:52:55 +01:00
|
|
|
|
|
|
|
ulWholeArraySize = getArraySize(psa);
|
|
|
|
|
|
|
|
if(lDelta < 0) { /* array needs to be shorthen */
|
|
|
|
if( isPointer(psa->fFeatures)) /* ptr that need to be released */
|
2001-09-21 23:01:31 +02:00
|
|
|
for(;lDelta < 0; lDelta++) {
|
1999-01-24 19:52:55 +01:00
|
|
|
punk = *(IUnknown**)
|
1999-04-25 21:01:52 +02:00
|
|
|
((char *) psa->pvData+((ulWholeArraySize+lDelta)*psa->cbElements));
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-01-24 19:52:55 +01:00
|
|
|
if( punk != NULL )
|
|
|
|
IUnknown_Release(punk);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if(psa->fFeatures & FADF_BSTR) /* BSTR that need to be freed */
|
2001-09-21 23:01:31 +02:00
|
|
|
for(;lDelta < 0; lDelta++) {
|
1999-02-26 12:11:13 +01:00
|
|
|
bstr = *(BSTR*)
|
1999-04-25 21:01:52 +02:00
|
|
|
((char *) psa->pvData+((ulWholeArraySize+lDelta)*psa->cbElements));
|
1999-01-24 19:52:55 +01:00
|
|
|
|
|
|
|
if( bstr != NULL )
|
1999-02-26 12:11:13 +01:00
|
|
|
SysFreeString( bstr );
|
1999-01-24 19:52:55 +01:00
|
|
|
}
|
2001-09-21 23:01:31 +02:00
|
|
|
else if(psa->fFeatures & FADF_VARIANT)
|
|
|
|
for(;lDelta < 0; lDelta++) {
|
|
|
|
VariantClear((VARIANT*)((char *) psa->pvData+((ulWholeArraySize+lDelta)*psa->cbElements)));
|
|
|
|
}
|
1999-01-24 19:52:55 +01:00
|
|
|
}
|
|
|
|
|
2000-06-18 21:29:40 +02:00
|
|
|
if (!(psa->fFeatures & FADF_CREATEVECTOR))
|
|
|
|
{
|
2002-06-01 01:06:46 +02:00
|
|
|
/* Ok now, if we are enlarging the array, we *MUST* move the whole block
|
2000-06-18 21:29:40 +02:00
|
|
|
pointed to by pvData. If we are shorthening the array, this move is
|
2002-06-01 01:06:46 +02:00
|
|
|
optional but we do it anyway becuase the benefit is that we are
|
2000-06-18 21:29:40 +02:00
|
|
|
releasing to the system the unused memory */
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if((pvNewBlock = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, psa->pvData,
|
|
|
|
(ulWholeArraySize + lDelta) * psa->cbElements)) == NULL)
|
2000-06-18 21:29:40 +02:00
|
|
|
return FALSE; /* TODO If we get here it means:
|
|
|
|
SHRINK situation : we've deleted the undesired
|
|
|
|
data and did not release the memory
|
|
|
|
GROWING situation: we've been unable to grow the array
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-06-01 01:06:46 +02:00
|
|
|
/* Allocate a new block, because the previous data has been allocated with
|
2000-06-18 21:29:40 +02:00
|
|
|
the descriptor in SafeArrayCreateVector function. */
|
|
|
|
|
2001-10-14 18:07:36 +02:00
|
|
|
if((pvNewBlock = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
|
2002-06-01 01:06:46 +02:00
|
|
|
ulWholeArraySize * psa->cbElements)) == NULL)
|
2000-06-18 21:29:40 +02:00
|
|
|
return FALSE;
|
1999-01-24 19:52:55 +01:00
|
|
|
|
2000-06-18 21:29:40 +02:00
|
|
|
psa->fFeatures &= ~FADF_CREATEVECTOR;
|
|
|
|
}
|
1999-01-24 19:52:55 +01:00
|
|
|
/* reassign to the new block of data */
|
|
|
|
psa->pvData = pvNewBlock;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/************************************************************************
|
|
|
|
* Used to set the fFeatures data member of the SAFEARRAY structure.
|
1999-01-24 19:52:55 +01:00
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
static INT getFeatures(
|
2002-06-01 01:06:46 +02:00
|
|
|
VARTYPE vt)
|
1999-01-24 19:52:55 +01:00
|
|
|
{
|
|
|
|
switch(vt) {
|
2001-09-21 23:01:31 +02:00
|
|
|
case VT_BSTR: return FADF_BSTR;
|
1999-01-24 19:52:55 +01:00
|
|
|
case VT_UNKNOWN: return FADF_UNKNOWN;
|
|
|
|
case VT_DISPATCH: return FADF_DISPATCH;
|
2001-09-21 23:01:31 +02:00
|
|
|
case VT_VARIANT: return FADF_VARIANT;
|
1999-01-24 19:52:55 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/************************************************************************
|
|
|
|
* Used to figure out if the fFeatures data member of the SAFEARRAY
|
|
|
|
* structure contain any information about the type of data stored...
|
1999-01-24 19:52:55 +01:00
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
static BOOL isPointer(
|
2002-06-01 01:06:46 +02:00
|
|
|
USHORT feature)
|
1999-01-24 19:52:55 +01:00
|
|
|
{
|
|
|
|
switch(feature) {
|
|
|
|
case FADF_UNKNOWN: return TRUE; /* those are pointers */
|
|
|
|
case FADF_DISPATCH: return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/************************************************************************
|
|
|
|
* Used to calculate the displacement when accessing or modifying
|
1999-01-24 19:52:55 +01:00
|
|
|
* safearray data set.
|
|
|
|
*
|
|
|
|
* Parameters: - LONG *coor is the desired location in the multidimension
|
|
|
|
* table. Ex for a 3 dim table: coor[] = {1,2,3};
|
|
|
|
* - ULONG *mat is the format of the table. Ex for a 3 dim
|
|
|
|
* table mat[] = {4,4,4};
|
|
|
|
* - USHORT dim is the number of dimension of the SafeArray
|
|
|
|
*/
|
|
|
|
static ULONG calcDisplacement(
|
2002-06-01 01:06:46 +02:00
|
|
|
LONG *coor,
|
|
|
|
SAFEARRAYBOUND *mat,
|
|
|
|
LONG dim)
|
1999-01-24 19:52:55 +01:00
|
|
|
{
|
|
|
|
ULONG res = 0;
|
|
|
|
LONG iterDim;
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
for(iterDim=0; iterDim<dim; iterDim++)
|
1999-01-24 19:52:55 +01:00
|
|
|
/* the -mat[dim] bring coor[dim] relative to 0 for calculation */
|
2002-06-01 01:06:46 +02:00
|
|
|
res += ((coor[iterDim]-mat[iterDim].lLbound) *
|
1999-01-24 19:52:55 +01:00
|
|
|
endOfDim(coor, mat, iterDim+1, dim));
|
|
|
|
|
1999-07-04 18:02:24 +02:00
|
|
|
TRACE("SafeArray: calculated displacement is %lu.\n", res);
|
1999-01-24 19:52:55 +01:00
|
|
|
return(res);
|
|
|
|
}
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/************************************************************************
|
|
|
|
* Recursivity agent for calcDisplacement method. Used within Put and
|
1999-01-24 19:52:55 +01:00
|
|
|
* Get methods.
|
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
static INT endOfDim(
|
2002-06-01 01:06:46 +02:00
|
|
|
LONG *coor,
|
|
|
|
SAFEARRAYBOUND *mat,
|
|
|
|
LONG dim,
|
|
|
|
LONG realDim)
|
1999-01-24 19:52:55 +01:00
|
|
|
{
|
2002-06-01 01:06:46 +02:00
|
|
|
if(dim==realDim)
|
1999-01-24 19:52:55 +01:00
|
|
|
return 1;
|
2002-06-01 01:06:46 +02:00
|
|
|
else
|
1999-01-24 19:52:55 +01:00
|
|
|
return (endOfDim(coor, mat, dim+1, realDim) * mat[dim].cElements);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/************************************************************************
|
|
|
|
* Method used to validate the coordinate received in Put and Get
|
1999-01-24 19:52:55 +01:00
|
|
|
* methods.
|
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
static BOOL validCoordinate(
|
2002-06-01 01:06:46 +02:00
|
|
|
LONG *coor,
|
|
|
|
SAFEARRAY *psa)
|
1999-01-24 19:52:55 +01:00
|
|
|
{
|
1999-02-26 12:11:13 +01:00
|
|
|
INT iter=0;
|
1999-01-24 19:52:55 +01:00
|
|
|
LONG lUBound;
|
|
|
|
LONG lLBound;
|
|
|
|
HRESULT hRes;
|
|
|
|
|
2002-12-23 03:02:49 +01:00
|
|
|
if (!psa->cDims) { FIXME("no dims?\n");return FALSE; }
|
1999-01-24 19:52:55 +01:00
|
|
|
for(; iter<psa->cDims; iter++) {
|
2001-10-14 18:07:36 +02:00
|
|
|
TRACE("coor[%d]=%ld\n", iter, coor[iter]);
|
2002-12-23 03:02:49 +01:00
|
|
|
if((hRes = SafeArrayGetLBound(psa, (iter+1), &lLBound)) != S_OK) {
|
|
|
|
FIXME("No lbound?\n");
|
1999-01-24 19:52:55 +01:00
|
|
|
return FALSE;
|
2002-12-23 03:02:49 +01:00
|
|
|
}
|
|
|
|
if((hRes = SafeArrayGetUBound(psa, (iter+1), &lUBound)) != S_OK) {
|
|
|
|
FIXME("No ubound?\n");
|
1999-01-24 19:52:55 +01:00
|
|
|
return FALSE;
|
2002-12-23 03:02:49 +01:00
|
|
|
}
|
|
|
|
if(lLBound > lUBound) {
|
|
|
|
FIXME("lbound larger than ubound?\n");
|
2002-06-01 01:06:46 +02:00
|
|
|
return FALSE;
|
2002-12-23 03:02:49 +01:00
|
|
|
}
|
2001-10-14 18:07:36 +02:00
|
|
|
|
2002-12-23 03:02:49 +01:00
|
|
|
if((coor[iter] < lLBound) || (coor[iter] > lUBound)) {
|
|
|
|
FIXME("coordinate %ld not within %ld - %ld\n",coor[iter], lLBound, lUBound);
|
1999-01-24 19:52:55 +01:00
|
|
|
return FALSE;
|
2002-12-23 03:02:49 +01:00
|
|
|
}
|
1999-01-24 19:52:55 +01:00
|
|
|
}
|
2001-10-14 18:07:36 +02:00
|
|
|
return TRUE;
|
2002-06-01 01:06:46 +02:00
|
|
|
}
|
1999-01-24 19:52:55 +01:00
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/************************************************************************
|
1999-01-24 19:52:55 +01:00
|
|
|
* Method used to calculate the number of cells of the SA
|
|
|
|
*/
|
|
|
|
static ULONG getArraySize(
|
2002-06-01 01:06:46 +02:00
|
|
|
SAFEARRAY *psa)
|
1999-01-24 19:52:55 +01:00
|
|
|
{
|
2002-06-01 01:06:46 +02:00
|
|
|
USHORT cCount;
|
1999-01-24 19:52:55 +01:00
|
|
|
ULONG ulWholeArraySize = 1;
|
|
|
|
|
|
|
|
for(cCount=0; cCount < psa->cDims; cCount++) /* foreach dimensions... */
|
|
|
|
ulWholeArraySize *= psa->rgsabound[cCount].cElements;
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
return ulWholeArraySize;
|
1999-01-24 19:52:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/************************************************************************
|
1999-01-24 19:52:55 +01:00
|
|
|
* Method used to handle data space dupplication for Copy32 and CopyData32
|
|
|
|
*/
|
|
|
|
static HRESULT duplicateData(
|
2002-06-01 01:06:46 +02:00
|
|
|
SAFEARRAY *psa,
|
|
|
|
SAFEARRAY **ppsaOut)
|
1999-01-24 19:52:55 +01:00
|
|
|
{
|
|
|
|
ULONG ulWholeArraySize; /* size of the thing */
|
|
|
|
LONG lDelta;
|
|
|
|
|
|
|
|
ulWholeArraySize = getArraySize(psa); /* Number of item in SA */
|
2002-06-01 01:06:46 +02:00
|
|
|
|
1999-02-26 12:11:13 +01:00
|
|
|
SafeArrayLock(*ppsaOut);
|
1999-01-24 19:52:55 +01:00
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
if( isPointer(psa->fFeatures) ) { /* If datatype is object increment
|
1999-01-24 19:52:55 +01:00
|
|
|
object's reference count */
|
2001-09-21 23:01:31 +02:00
|
|
|
IUnknown *punk;
|
1999-01-24 19:52:55 +01:00
|
|
|
|
|
|
|
for(lDelta=0; lDelta < ulWholeArraySize; lDelta++) {
|
1999-04-25 21:01:52 +02:00
|
|
|
punk = *(IUnknown**)((char *) psa->pvData+(lDelta * psa->cbElements));
|
1999-01-24 19:52:55 +01:00
|
|
|
|
|
|
|
if( punk != NULL)
|
|
|
|
IUnknown_AddRef(punk);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the source array data into target array */
|
2002-06-01 01:06:46 +02:00
|
|
|
memcpy((*ppsaOut)->pvData, psa->pvData,
|
1999-01-24 19:52:55 +01:00
|
|
|
ulWholeArraySize*psa->cbElements);
|
|
|
|
|
2001-09-21 23:01:31 +02:00
|
|
|
}
|
2002-06-01 01:06:46 +02:00
|
|
|
else if( psa->fFeatures & FADF_BSTR ) { /* if datatype is BSTR allocate
|
2001-09-21 23:01:31 +02:00
|
|
|
the BSTR in the new array */
|
|
|
|
BSTR pbstrReAllocStr = NULL;
|
1999-01-24 19:52:55 +01:00
|
|
|
|
|
|
|
for(lDelta=0; lDelta < ulWholeArraySize; lDelta++) {
|
2001-09-21 23:01:31 +02:00
|
|
|
if(( pbstrReAllocStr = SYSDUPSTRING(
|
1999-04-25 21:01:52 +02:00
|
|
|
*(BSTR*)((char *) psa->pvData+(lDelta * psa->cbElements)))) == NULL) {
|
1999-01-24 19:52:55 +01:00
|
|
|
|
1999-02-26 12:11:13 +01:00
|
|
|
SafeArrayUnlock(*ppsaOut);
|
1999-01-24 19:52:55 +01:00
|
|
|
return E_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
*((BSTR*)((char *) (*ppsaOut)->pvData+(lDelta * psa->cbElements))) =
|
1999-01-24 19:52:55 +01:00
|
|
|
pbstrReAllocStr;
|
|
|
|
}
|
|
|
|
|
2001-09-21 23:01:31 +02:00
|
|
|
}
|
|
|
|
else if( psa->fFeatures & FADF_VARIANT ) {
|
|
|
|
|
|
|
|
for(lDelta=0; lDelta < ulWholeArraySize; lDelta++) {
|
|
|
|
VariantCopy((VARIANT*)((char *) (*ppsaOut)->pvData+(lDelta * psa->cbElements)),
|
|
|
|
(VARIANT*)((char *) psa->pvData+(lDelta * psa->cbElements)));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else { /* Simply copy the source array data into target array */
|
1999-01-24 19:52:55 +01:00
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
memcpy((*ppsaOut)->pvData, psa->pvData,
|
1999-01-24 19:52:55 +01:00
|
|
|
ulWholeArraySize*psa->cbElements);
|
|
|
|
}
|
|
|
|
|
1999-02-26 12:11:13 +01:00
|
|
|
SafeArrayUnlock(*ppsaOut);
|
1999-01-24 19:52:55 +01:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2000-07-08 20:30:41 +02:00
|
|
|
|
2002-06-01 01:06:46 +02:00
|
|
|
/************************************************************************
|
2002-06-21 01:51:21 +02:00
|
|
|
* SafeArrayGetVartype (OLEAUT32.77)
|
2000-07-08 20:30:41 +02:00
|
|
|
* Returns the VARTYPE stored in the given safearray
|
|
|
|
*/
|
2002-06-21 01:51:21 +02:00
|
|
|
HRESULT WINAPI SafeArrayGetVartype(
|
2000-07-08 20:30:41 +02:00
|
|
|
SAFEARRAY* psa,
|
|
|
|
VARTYPE* pvt)
|
|
|
|
{
|
|
|
|
HRESULT hr = E_INVALIDARG;
|
|
|
|
VARTYPE vt = VT_EMPTY;
|
|
|
|
|
|
|
|
/* const short VARTYPE_OFFSET = -4; */
|
|
|
|
|
|
|
|
if (psa->fFeatures & FADF_HAVEVARTYPE)
|
|
|
|
{
|
|
|
|
/* VT tag @ negative offset 4 in the array descriptor */
|
2001-05-09 19:31:31 +02:00
|
|
|
FIXME("Returning VT_BSTR instead of VT_...\n");
|
2000-07-08 20:30:41 +02:00
|
|
|
vt = VT_BSTR;
|
|
|
|
}
|
|
|
|
else if (psa->fFeatures & FADF_RECORD)
|
|
|
|
{
|
|
|
|
vt = VT_RECORD;
|
|
|
|
}
|
2001-09-21 23:01:31 +02:00
|
|
|
else if (psa->fFeatures & FADF_BSTR)
|
2000-07-08 20:30:41 +02:00
|
|
|
{
|
2001-09-21 23:01:31 +02:00
|
|
|
vt = VT_BSTR;
|
2000-07-08 20:30:41 +02:00
|
|
|
}
|
|
|
|
else if (psa->fFeatures & FADF_UNKNOWN)
|
|
|
|
{
|
|
|
|
vt = VT_UNKNOWN;
|
|
|
|
}
|
2001-09-21 23:01:31 +02:00
|
|
|
else if (psa->fFeatures & FADF_DISPATCH)
|
|
|
|
{
|
|
|
|
vt = VT_DISPATCH;
|
|
|
|
}
|
|
|
|
else if (psa->fFeatures & FADF_VARIANT)
|
|
|
|
{
|
|
|
|
vt = VT_VARIANT;
|
|
|
|
}
|
2000-07-08 20:30:41 +02:00
|
|
|
|
|
|
|
if (vt != VT_EMPTY)
|
|
|
|
{
|
|
|
|
*pvt = vt;
|
|
|
|
hr = S_OK;
|
|
|
|
}
|
2001-05-09 19:31:31 +02:00
|
|
|
|
|
|
|
TRACE("HRESULT = %08lx\n", hr);
|
2000-07-08 20:30:41 +02:00
|
|
|
return hr;
|
|
|
|
}
|