PRB: Script Error Occurs When Referencing Non-variant Array

Last reviewed: September 29, 1997
Article ID: Q165967
The information in this article applies to:
  • Microsoft Visual Basic, Scripting Edition, versions 1.0, 1.1, 2.0, 3.0
  • Microsoft JScript, versions 1.0, 2.0
  • Internet Explorer (Programming), version 4.0

SYMPTOMS

When a script attempts to reference the elements of an array returned by a component, the script engine reports:

   "Object doesn't support this property or method '<object>.<method>'"

CAUSE

The VBSCRIPT active scripting engine supplied by Microsoft only supports the indexing of SAFEARRAYs of VARIANTs. While VBSCRIPT is capable of accepting arrays of non-variant type for the purposes of boundary checking and passing it to other automation objects, the engine does not allow manipulation of the array contents at this time.

The JSCRIPT active scripting engine does not provide support for testing the bounds or indexing SAFEARRAYs of any type including VARIANTs. However, JSCRIPT is capable of passing SAFEARRAYs from one automation object to another.

RESOLUTION

To function correctly with applications and components that host VBSCRIPT, automation objects should create SAFEARRAYs of VARIANTs. Non-VARIANT data should be packaged in the VARIANT elements of the SAFEARRAY to be returned to the VBSCRIPT engine.

Scripts written in VBSCRIPT should use the TypeName function to check the data type of a variable. The TypeName function returns the string "Variant()," excluding the quotes, when passed an array of VARIANTs.

Scripts written in JSCRIPT should use the typeof operator to test the data type of a variable. The typeof operator returns the string "unknown," excluding the quotes for datatypes unsupported by JSCRIPT.

STATUS

This behavior is by design.

MORE INFORMATION

The VBSARRAY is a simple Active Template Library (ATL) version 2.1 component object that demonstrates this behavior. The component implements a dual interface Ivbsa that supports three methods: TestBstrs, TestVariants, and TestPassArray. The first demonstrates the problem by returning a SAFEARRAY of BSTRs. The second demonstrates the solution by packaging each of the BSTRs in a VARIANT. The third demonstrates that an array of non-VARIANT type can be passed from component to VBSCRIPT or JSCRIPT to component. The data remains intact.

Here is the implementation of TestVariants:

      // Return a VARIANT array of VARIANTs which hold BSTRs
      STDMETHODIMP Cvbsa::TestVariants(VARIANT * pvaVariant)
      {
         HRESULT hr = NOERROR;
         LPSAFEARRAY psa;
         SAFEARRAYBOUND rgsabound[]  = { 3, 0 }; // 3 elements, 0-based
         int i;

         if (!pvaVariant)
         {
            return E_INVALIDARG;
         }

         VariantInit(pvaVariant);

         psa = SafeArrayCreate(VT_VARIANT, 1, rgsabound);
         if (!psa)
         {
            return E_OUTOFMEMORY;
         }

         VARIANT vFlavors[3];
         for (i = 0; i < 3; i++)
         {
            VariantInit(&vFlavors[i]);
            V_VT(&vFlavors[i]) = VT_BSTR;
         }

         V_BSTR(&vFlavors[0]) = SysAllocString(OLESTR("Vanilla"));
         V_BSTR(&vFlavors[1]) = SysAllocString(OLESTR("Chocolate"));
         V_BSTR(&vFlavors[2]) = SysAllocString(OLESTR("Espresso Chip"));
         if (!V_BSTR(&vFlavors[0]) || !V_BSTR(&vFlavors[1]) ||
             !V_BSTR(&vFlavors[2]))
         {
            hr = E_OUTOFMEMORY;
            goto Error;
         }

         {
                //Plug references to the data into the SAFEARRAY
               LPVARIANT rgElems;
               if (FAILED(hr = SafeArrayAccessData(psa,(LPVOID*)&rgElems)))

            {
               goto Error;
            }
            for (i = 0; i < 3; i++)
            {
               rgElems[i] = vFlavors[i];
            }
            SafeArrayUnaccessData(psa);
         }

         V_VT(pvaVariant) = VT_ARRAY | VT_VARIANT;
         V_ARRAY(pvaVariant) = psa;

         return NOERROR;

   Error:
         for (i = 0; i < 3; i++)
         {
            if (V_BSTR(&vFlavors[i])
            {
               VariantClear(&vFlavors[i]);
            }
         }
         return hr;
   }

Here is the JSCRIPT code from VBSARRAY.HTM, the test page included with the sample, that checks the datatype of a variable:
      <SCRIPT LANGUAGE=JSCRIPT>
      function JScriptSafeArrayTest()
      {
         pvaBstr = SimpleComponent.TestBstrs()
         if (typeof(pvaBstr) == "unknown")
         {
            Alert("JSCRIPT cannot handle the type returned by TestBstrs()")
            SimpleComponent.TestPassedArray(pvaBstr)
         }

         pvaVariant = SimpleComponent.TestVariants()
         if (typeof(pvaVariant) == "unknown")
         {
         Alert("JSCRIPT cannot handle the type returned by TestVariants()")
            SimpleComponent.TestPassedArray(pvaVariant)
         }
      }
      </SCRIPT>

To demonstrate the problem and the solution, perform the following steps:

  1. Follow the instructions below on obtaining the sample component VBSARRAY.DLL and sample page VBSARRAY.HTM.

  2. Register the component VBSARRAY.DLL using a tool such as REGSVR32.EXE.

  3. Launch Internet Explorer, and load the test page VBSARRAY.HTM.

  4. Follow the instructions on the page, clicking the various buttons and observing the resulting behavior.

The sample component was created using the Active Template Library version 2.1 including with Visual C++ 5.0 in the Visual Studio 97 environment. If you have Visual Studio 97, you can load the VBSARRAY project directly. Otherwise, you can open the relevant source file VBSA.CPP in any editor to see how the SAFEARRAY is constructed in both the VARIANT and the BSTR cases. Search for the implementation of both Cvbsa::TestVariants and Cvbsa::TestBstrs in the source file.

The following file is available for download from the Microsoft Software Library:

 ~ Vbsarray.exe (size: 71069 bytes) 

For more information about downloading files from the Microsoft Software Library, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q119591
   TITLE     : How to Obtain Microsoft Support Files from Online Services

REFERENCES

Platform SDK Automation Reference

Microsoft Visual Basic Scripting Edition Language Reference

Keywords          : AXSDKSripting kbsample kbprb
Technology        : kbAtl kbInetDev
Version           : Win:1.0,1.1,2.0,3.0
Platform          : WINDOWS
Issue type        : kbprb
Solution Type     : kbfile


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.