Link to home
Start Free TrialLog in
Avatar of wjh021797
wjh021797

asked on

how do I return an array (COleSafeArray?) using automation?

I am writing two programs which interact with automation, and the client wants to get an array (of floats, actually) back from the server. I set up the server method (GetData) to return a VARIANT, and have tried the following, just as a test for returning a CByteArray:

VARIANT CServerDlgAutoProxy::GetData()
{
#if 0
// ignore VStudio generated stuff
     VARIANT vaResult;
     VariantInit(&vaResult);
     // TODO: Add your dispatch handler code here
#endif

     CByteArray byteArray;
     char c;
     for (c=0; c<10; c++)
          byteArray.Add(c);
     COleVariant v(byteArray);
     return v;
}

My client code looks like this

void CTestAutoDlg::OnGetData()
{
     VARIANT ret;
     ret=m_KlustaDispatch.GetPartitionData();
}

However, this doesn't work. I get exceptions and kernel page faults, and various nasty messages.

Can someone tell me what is the correct way to return an array using automation? What I would really like to return is a COleSafeArray.

Also, if I do this, which program owns the memory, and how do I displose of it? Where should I use Detach(), if at all?


ASKER CERTIFIED SOLUTION
Avatar of job_s
job_s

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xzxzzxw
xzxzzxw

While the above is a perfectly good answer, there is actually an easy way to return / use a COleSafeArray, when you're trying to pass back a byte array, and it's as easy as this:

VARIANT MyClass::MyFunc( void )
{
      COleSafeArray vaRes;
      
      char *sString = ; // Insert string here
      vaRes.vt = (VT_ARRAY | VT_UI1);
      vaRes.CreateOneDim( VT_UI1, (DWORD) strlen( sString ), sString );

      return vaRes.Detach();
}