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::GetDa ta()
{
#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.GetPa rtitionDat a();
}
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?
VARIANT CServerDlgAutoProxy::GetDa
{
#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.GetPa
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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();
}