Hi,

Thanks for checking out my post.

I am trying to send a SAFEARRAY from a Visual C++ ATL COM component to a
VBScript client.

I have managed to get this working with a "[out,retval] VARIANT *" on the
server side and reading a 1D (one dimesional) array on the VBScript client.
I create a SAFEARRAY, filled it with VARIANTS of type BSTR and then send it
off to the client via a VARIANT *

But I can't seem to get it working with a 2D array?? I get that typical
"Type mismatch error" when I try to access a member of the array on the
client side, like this :-

###################### begin client code
########################################
Set objArray = CreateObject("ArraySamp.Array") aArray =
objArray.Fetch()

aArray = objArray.Fetch()
Response.write aArray(0)(0) <-----THIS ERRORS
####



The server (VC++) code is below. Thanks for any help!

Mike


########################### begin server code
###########################################

STDMETHODIMP CArray::Fetch(VARIANT *pAray)
{

SAFEARRAYBOUND saBound[2];
SAFEARRAY *psa;

//first dimension
saBound[0].cElements = 2;
saBound[0].lLbound = 0;

//second dimension
saBound[1].cElements = 2;
saBound[1].lLbound = 0;

//create a 2D SAFEARRAY
psa = SafeArrayCreate(VT_VARIANT, 2, saBound);

//the data we will add to the array
CComBSTR str(_T("mike"));
VARIANT vVal;
vVal.vt = VT_BSTR;
vVal.bstrVal = str.Copy();

CComBSTR str2(_T("fred"));
VARIANT vVal2;
vVal2.vt = VT_BSTR;
vVal2.bstrVal = str2.Copy();


//vector of indexes for each dimension of the array
//which is used to traverse the array and to add
//elements to the array
long aiIndex[2];
HRESULT hr = S_OK;

//traverse the rows
for(aiIndex[0] = 0; aiIndex[0] < 2; aiIndex[0]++)
{
//go along the columns for this single row
for(aiIndex[1] = 0; aiIndex[1] < 2; aiIndex[1]++)
{
//this is just to put either "fred" or "mike"
//in each string
if (aiIndex[0] == 0)
{
hr = SafeArrayPutElement(psa, aiIndex, &vVal);
}
else
{
hr = SafeArrayPutElement(psa, aiIndex, &vVal2);
}
}
}

VariantInit(pAray);

pAray->vt = VT_ARRAY | VT_VARIANT;
pAray->parray = psa;

return S_OK;
}

Re: passing array from VC++ COM to VBScript by mt

mt
Mon Jun 21 15:21:21 CDT 2004

That was it. I was accessing the 2D array incorrectly.

Thanks Tom!

Mike

"Tom Lavedas" <tlavedas@hotmail.remove.com> wrote in message
news:38D4363A-4128-4105-ABD6-67FB57748BF3@microsoft.com...
> I don't know if this is all of the problem, but arrays in all VB dielects
I know are addressed with a syntax of (x,y) , not (x)(y). Try ...
>
> Response.write aArray(0, 0)
>
> Tom Lavedas
> ===========
>
> "mt" wrote:
>
> > Hi,
> >
> > Thanks for checking out my post.
> >
> > I am trying to send a SAFEARRAY from a Visual C++ ATL COM component to a
> > VBScript client.
> >
> > I have managed to get this working with a "[out,retval] VARIANT *" on
the
> > server side and reading a 1D (one dimesional) array on the VBScript
client.
> > I create a SAFEARRAY, filled it with VARIANTS of type BSTR and then send
it
> > off to the client via a VARIANT *
> >
> > But I can't seem to get it working with a 2D array?? I get that typical
> > "Type mismatch error" when I try to access a member of the array on the
> > client side, like this :-
> >
> > ###################### begin client code
> > ########################################
> > Set objArray = CreateObject("ArraySamp.Array") aArray =
> > objArray.Fetch()
> >
> > aArray = objArray.Fetch()
> > Response.write aArray(0)(0) <-----THIS ERRORS
> > ####
> >
> >
> >
> > The server (VC++) code is below. Thanks for any help!
> >
> > Mike
> >
> >
> > ########################### begin server code
> > ###########################################
> >
> > STDMETHODIMP CArray::Fetch(VARIANT *pAray)
> > {
> >
> > SAFEARRAYBOUND saBound[2];
> > SAFEARRAY *psa;
> >
> > //first dimension
> > saBound[0].cElements = 2;
> > saBound[0].lLbound = 0;
> >
> > //second dimension
> > saBound[1].cElements = 2;
> > saBound[1].lLbound = 0;
> >
> > //create a 2D SAFEARRAY
> > psa = SafeArrayCreate(VT_VARIANT, 2, saBound);
> >
> > //the data we will add to the array
> > CComBSTR str(_T("mike"));
> > VARIANT vVal;
> > vVal.vt = VT_BSTR;
> > vVal.bstrVal = str.Copy();
> >
> > CComBSTR str2(_T("fred"));
> > VARIANT vVal2;
> > vVal2.vt = VT_BSTR;
> > vVal2.bstrVal = str2.Copy();
> >
> >
> > //vector of indexes for each dimension of the array
> > //which is used to traverse the array and to add
> > //elements to the array
> > long aiIndex[2];
> > HRESULT hr = S_OK;
> >
> > //traverse the rows
> > for(aiIndex[0] = 0; aiIndex[0] < 2; aiIndex[0]++)
> > {
> > //go along the columns for this single row
> > for(aiIndex[1] = 0; aiIndex[1] < 2; aiIndex[1]++)
> > {
> > //this is just to put either "fred" or "mike"
> > //in each string
> > if (aiIndex[0] == 0)
> > {
> > hr = SafeArrayPutElement(psa, aiIndex, &vVal);
> > }
> > else
> > {
> > hr = SafeArrayPutElement(psa, aiIndex, &vVal2);
> > }
> > }
> > }
> >
> > VariantInit(pAray);
> >
> > pAray->vt = VT_ARRAY | VT_VARIANT;
> > pAray->parray = psa;
> >
> > return S_OK;
> > }
> >
> >
> >