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
In VBScript if you use TypeName() to display what is returned you will see "String()" when you need
"Variant()".
--
Michael Harris
MVP Scripting
"Jeff McLamb" <jeff....@onsemi.com> wrote in message news:8gh1v0$k83$1...@spsnews.sps.mot.com...
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;
Michael Harris <Please....@To.NewsGroup> wrote in message
news:uAjbMWbx$GA.197@cppssbbsa04...
--
Michael Harris
MVP Scripting
"Jeff McLamb" <jeff....@onsemi.com> wrote in message news:8ghi32$o19$1...@spsnews.sps.mot.com...
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>
Michael Harris <Please...@To.NewsGroup> wrote in message
news:uXJ3qrex$GA....@cppssbbsa02.microsoft.com...