
C++ ActiveX Property returns VARIANT Array of BSTR for VBScript or JavaScript
Thanks Michael,
Again, that did the trick. If you are ever near Arizona
and looking for a software engineering job, let me know.
I can use another multilingual programmer.
Jeff McLamb
For those interested and following the thread...
In JavaScript accessing :
<SCRIPT LANGUAGE=javascript = runat=server>
var myObj = Server.CreateObject("newtONCMComponent.InterrogateDatabase");
var nIdx;
var stdJavaArray;
var myVBArrayObject = VBArray(myObj.DumpChildrenNamesArray);
// If array was multidimensional, it is now one array.
// example: (1, 2, 3), (4, 5, 6) = 1, 2, 3, 4, 5, 6
stdJavaArray = myVBArrayObject.toArray();
for (nIdx = 0; nIdx < myVBArrayObject.ubound(1); nIdx++)
{
response.write(stdJavaArray[nIdx]+"<BR>");
}
</SCRIPT>
Quote:
> In JScript you have to convert the returned SAFEARRAY to a JScript Array
using the VBArray()
> function.
> --
> Michael Harris
> MVP Scripting
Quote:
> That did the trick. Thank you.
> Incase anyone searches on this thread in the
> future, here are the changes to the code.
> It is now returning a Variant Array of Variants of type BSTR;
> Still can't get it to work in JavaScript, anyone know how
> to do it in JavaScript?
> Jeff
> VariantInit(pVal);
> pVal->vt = VT_ARRAY | VT_VARIANT;
> SAFEARRAY* psa;
> SAFEARRAYBOUND bounds = {m_vecChildrenNames.size(), 0};
> psa = SafeArrayCreate(VT_VARIANT, 1, &bounds);
> VARIANT* vArray;
> SafeArrayAccessData(psa, (void**)&vArray);
> std::vector<CComBSTR>::iterator it;
> int i = 0;
> for (it = m_vecChildrenNames.begin(); it != m_vecChildrenNames.end();
it++,
> i++)
> {
> VariantInit(&vArray[i]);
> vArray[i].vt = VT_BSTR;
> vArray[i].bstrVal = SysAllocString((*it).m_str);
> }
> SafeArrayUnaccessData(psa);
> pVal->parray = psa;
> > A VBScript client will only be able to use an array of variants of
subtype
> String, as opposed to an
> > array of strings (which is what you have)...
> > In VBScript if you use TypeName() to display what is returned you will
see
> "String()" when you need
> > "Variant()".
> > --
> > Michael Harris
> > MVP Scripting
> > Type Mismatch errors .... ARRRG ... Please help.
> > I have 3 segments of code here. The first
> > is a property from a C++ ActiveX component.
> > It returns a VARIANT which has an array of BSTR's.
> > The second is a VB 6.0 Client which calls the
> > property and then loops through the Variant array of
> > Strings and adds them to a list box. Works like a charm.
> > The Third is VB Script. Which tries to do the same thing.
> > However, I keep getting an invalid type error. When I do
> > a TypeName on the variable which is supposed to be
> > an array of strings, it thinks it is a string instead of an array.
> > How do I get VB Script to recognize a Variant array of
> > strings and be able to use that data?
> > Also if you know, how would you do it in JavaScript?
> > I couldn't figure out how to declare a variant in JavaScript either.
> > Extra info, UBound can correctly identify the size of the array
> > at 20 element.
> > Thanks for any help.
> > [propget, id(21)] HRESULT DumpTreeArray([out, retval] VARIANT *pVal);
> > // Based off of "WROX ATL COM Programming Ex. p.187"
> > STDMETHODIMP CInterrogateDatabase::get_DumpTreeArray(VARIANT *pVal)
> > {
> > VariantInit(pVal);
> > pVal->vt = VT_ARRAY | VT_BSTR;
> > SAFEARRAY* psa;
> > SAFEARRAYBOUND bounds = {m_vecDumpTree.size(), 0};
> > psa = SafeArrayCreate(VT_BSTR, 1, &bounds);
> > BSTR* bstrArray;
> > SafeArrayAccessData(psa, (void**)&bstrArray);
> > std::vector<CComBSTR>::iterator it;
> > int i = 0;
> > for (it = m_vecDumpTree.begin(); it != m_vecDumpTree.end(); it++, i++)
> > bstrArray[i] = SysAllocString((*it).m_str);
> > SafeArrayUnaccessData(psa);
> > pVal->parray = psa;
> > return S_OK;
> > }
> > // Based off of "WROX ATL COM Programming Ex. p. 194
> > Visual Basic 6.0 Client
> > listDumpTreeArray.Clear
> > Dim Size As Long
> > Dim DumpTreeArray As Variant
> > DumpTreeArray = jeff1.DumpTreeArray
> > Size = UBound(DumpTreeArray) - LBound(DumpTreeArray) + 1
> > For x = LBound(DumpTreeArray) To UBound(DumpTreeArray)
> > listDumpTreeArray.AddItem DumpTreeArray(x)
> > Next
> > // Trying to do the same thing in VB Script????
> > Visual Basic Script
> > Dim Size, x
> > DumpTreeArray = myObj.DumpTreeArray
> > Size = UBound(DumpTreeArray) - LBound(DumpTreeArray) + 1
> > For x = LBound(DumpTreeArray) To UBound(DumpTreeArray)
> > Response.Write(DumpTreeArray(x))
> > Next