
Passing a Variant 2D array of Double to C from Basic via ATL
: I need to make a function in Visual C that will accept an array from Visual
: basic via an ATL function
: I have an existing ATL function that gets a Variant from a program called
: winspec
: it is a 2d array of type double that contains data corresponding to the
: intensity of a piel on a CCD camera. I need to get this data to C so that i
: can fit it using my existing routines not ot mention that its much faster
: I would like to get it there via ATL but I am not quite sure how to do it
: The best way would be to pass the array, I think, would be to get a pointer
: corresponding to the (0)(0) and send it to C. Is it safe to assume that
: Basic makes an array from one hunk of memory?
: the array is approximately 400X600. But i dont know how to get a pointer in
: Basic.
<snip>
Don't do this. Let your C/C++ routine accept a variant. Then check the
type of the variant; it should be VT_ARRAY|VT_R8. If so then take the
parray member of the variant and use SafeArrayAccessData to get a pointer
to the data (which you can then cast to double*). Also make sure to check
the dimensions. Something like this should work (but it's untested):
HRESULT function(VARIANT v)
{
// check element type
if( v.vt != (VT_ARRAY|VT_R8) )
return E_INVALIDARG;
// check # of dimensions
SAFEARRAY * psa = v.parray;
if( psa->cDims != 2 )
return E_INVALIDARG;
// get dimensions
unsigned long height = psa->rgsabound[0].cElements;
unsigned long width = psa->rgsabound[1].cElements;
// get array contents
HRESULT hr;
void * pData;
hr = SafeArrayAccessData( psa, &pData );
if( FAILED(hr) )
return hr;
double * pDoubleData = static_cast<double*>(pData);
// now access array elements as pDoubleData[ y * height + x ]
// release array contents
(void) SafeArrayUnaccessData(psa);
return S_OK;
Quote:
}
Please don't cross-post so widely. I hope this message is relevant to the
newsgroups I've left in.
--
Any opinions expressed are my own and not necessarily those of Laser-Scan.