Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

return a safearray as a variant from a COM objects

Status
Not open for further replies.

abramowi

Technical User
May 2, 2003
10
GB
Hi!

I'm trying to retun a safe array as a variant in a COM object. This is what I do:

COMObject::GetArray(VARIANT* arr)
{
CComSafeArray<BSTR> safeArray;
safeArray.add(&quot;Hello&quot;);

CComVariant return(safeArray);
ret.Detach(groups);
}

However, when I try to get the array from VB Script using the following lines:

Dim Arr
Arr = comobj.GetArray

I get the following error:

Invalid access to memory location

has ANYONE got any idea why??? It would be really nice to solve this prob before Monday.

Thanks in advance /dave
 
Hi Dave,

For the safearrays you should use
- Use SafeArrayCreate() to create the array
and
- Use SafeArrayAccessData() to gain access to the array’s data elements

otherwise you most likely run into memory problems as you did.
if you have access to MSDN get some more info on these.
Hope this helps

Cheers,
onlyshiza
 
Thank you for your quick reply. Hmmm... I tried changing the code to:

COMObject::GetArray(VARIANT* arr)
{
CComSafeArray<BSTR> * safeArray = new CComSafeArray<BSTR>(1); //parameter is size of array
safeArray.add(&quot;Hello&quot;);

CComVariant return(*safeArray);
ret.Detach(groups);
}

And I still get the same exception.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top