C++ ActiveX Property returns VARIANT Array of BSTR for VBScript or JavaScript 
Author Message
 C++ ActiveX Property returns VARIANT Array of BSTR for VBScript or JavaScript

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;

Quote:
}

// 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



Sun, 10 Nov 2002 03:00:00 GMT  
 C++ ActiveX Property returns VARIANT Array of BSTR for VBScript or JavaScript
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;

Quote:
}

// 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



Sun, 10 Nov 2002 03:00:00 GMT  
 C++ ActiveX Property returns VARIANT Array of BSTR for VBScript or JavaScript
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);

Quote:
}

SafeArrayUnaccessData(psa);
pVal->parray = psa;


Quote:
> A VBScript client will only be able to use an array of variants of subtype

String, as opposed to an
Quote:
> 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
Quote:
> "Variant()".

> --
> Michael Harris
> MVP Scripting




Quote:
> 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



Sun, 10 Nov 2002 03:00:00 GMT  
 C++ ActiveX Property returns VARIANT Array of BSTR for VBScript or JavaScript
In JScript you have to convert the returned SAFEARRAY to a JScript Array using the VBArray()
function.

--
Michael Harris
MVP Scripting

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);

Quote:
}

SafeArrayUnaccessData(psa);
pVal->parray = psa;


Quote:
> A VBScript client will only be able to use an array of variants of subtype

String, as opposed to an
Quote:
> 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
Quote:
> "Variant()".

> --
> Michael Harris
> MVP Scripting




Quote:
> 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



Sun, 10 Nov 2002 03:00:00 GMT  
 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



Mon, 11 Nov 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. C++ ActiveX Property returns VARIANT Array of BSTR for VBScript or JavaScript

2. Variant containing a Byte Array returned from C++

3. JavaScript access variant array returned by COM object?

4. com variant bstr array

5. Converting Javascript strings to C++ BSTR* ???

6. Returning arrays from VB/VC components or VBScript to JavaScript in ASP

7. Returning arrays of VARIANTs to VBScript

8. Returning arrays of VARIANTs to VBScript

9. Returning a BSTR from a C++ DLL

10. converting Array to Variant and Variant to Array

11. Array to Variant and Variant to array problem

12. Accessing an ActiveX control property that is a variant

 

 
Powered by phpBB® Forum Software