The day before yesterday, I encountered someone asking SAFEARRAY. I happened to be interested in how to get the array returned by the COM component in VB SCRIPT, so I tried it.
IDE uses VC7.1
It's almost the same at the beginning, build a project, build a COM object, and create a method:
Interface definition:
[id(2), helpstring("method GetArray")] HRESULT GetArray1([out,retval] VARIANT* v);
Method implementation:
STDMETHODIMP CATL3::GetArray1(VARIANT * pvaVariant)
{
std::vector< float> vct;
vct.push_back( 11.4 );
vct.push_back( 12.4 );
vct.push_back( 14.4 );
CComSafeArray<VARIANT> saMatrix;
for ( int i = 0 ; i < (); ++i )
{
( CComVariant(vct[i]));
}
VariantInit(pvaVariant);
CComVariant var( saMatrix );
( pvaVariant );
return S_OK;
}
One thing to note is that in VBS or other script environments, the type in the array is VARIANT type.
VBS test script
Dim v4
1, 2
v4 = Cnxn4.GetArray1()
Dim x
For x = Lbound(v4) To Ubound( v4 )
MsgBox v4(x)
Next
IDE uses VC7.1
It's almost the same at the beginning, build a project, build a COM object, and create a method:
Interface definition:
[id(2), helpstring("method GetArray")] HRESULT GetArray1([out,retval] VARIANT* v);
Method implementation:
STDMETHODIMP CATL3::GetArray1(VARIANT * pvaVariant)
{
std::vector< float> vct;
vct.push_back( 11.4 );
vct.push_back( 12.4 );
vct.push_back( 14.4 );
CComSafeArray<VARIANT> saMatrix;
for ( int i = 0 ; i < (); ++i )
{
( CComVariant(vct[i]));
}
VariantInit(pvaVariant);
CComVariant var( saMatrix );
( pvaVariant );
return S_OK;
}
One thing to note is that in VBS or other script environments, the type in the array is VARIANT type.
VBS test script
Dim v4
1, 2
v4 = Cnxn4.GetArray1()
Dim x
For x = Lbound(v4) To Ubound( v4 )
MsgBox v4(x)
Next