Microsoft KB Archive/218454

From BetaArchive Wiki
Knowledge Base


How To Implement Array Arguments in Visual C++ COM Objects for Active Server Pages

Article ID: 218454

Article Last Modified on 7/1/2004



APPLIES TO

  • Microsoft Active Server Pages 4.0
  • Microsoft Visual C++ 6.0 Enterprise Edition
  • Microsoft Visual C++ 6.0 Professional Edition
  • Microsoft Visual C++ 6.0 Standard Edition
  • Microsoft Internet Information Server 4.0
  • Microsoft Internet Information Services 5.0



This article was previously published under Q218454

SUMMARY

This article describes, by example, how to implement arrays to be passed as parameters from Active Server Pages (ASP) to a Visual C++ COM Object.

It is important to keep in mind that array arguments for methods must be declared as variant data type and must the passed by reference. Declaring an array argument any other way may cause an error, such as the following:


error 'ASP 0115' - A trappable error occured in an external object


-or-


Invalid procedure call or argument


-or-


Type Mismatch


-or-


Object Does not Support This property or method


MORE INFORMATION

Use the following steps to implement arrays to be passed as parameters from ASP to a Visual C++ COM Object:

  1. Create an ATL DLL Project called ASPArray."
  2. Insert an ATL Object named VCArrayObj. Add a Method with the following information:

    Method Name: TestArray<BR/>
    Parameters : [in, out] VARIANT* pArray, [out, retval] long* pVal<BR/>
                        


  3. Implement the Method as follows:

    STDMETHODIMP CVCArrayObj::TestArray(VARIANT *pArray, long *pVal)
    {
        SAFEARRAY *psa;
        long lLBound, lUBound, cElements;  
        
    if( pArray->vt & VT_BYREF )
        psa = *(pArray->pparray);
    else
        psa = pArray->parray;
    
        //Check the Dimension of the Array
        if ( SafeArrayGetDim( psa ) != 1 )
        return E_INVALIDARG; 
    
        //Get the lower and upper bounds of the array
        SafeArrayGetLBound( psa, 1, &lLBound );
        SafeArrayGetUBound( psa, 1, &lUBound );
    
        //Compute the Number of Elements
        cElements = lUBound - lLBound + 1;
    
        //Access the elements of the array
        for ( long cCnt = lLBound; cCnt <= lUBound; cCnt++ )
        {
           VARIANT vVal;
           SafeArrayGetElement( psa, &cCnt, &vVal );
        }
    
        *pVal = cElements;    
        return S_OK;
    }
                            
  4. Create an ASP page with the following code:

    <%
      Dim oTestObj, vMyArray(2), vRtnValue
               
      vMyArray(0) = "Element 1"
      vMyArray(1) = "Element 2"
      vMyArray(2) = "Element 3"
    
      Set oTestObj = Server.CreateObject("ASPArray.VCArrayObj") 
      vRtnValue = oTestObj.TestArray( vMyArray )
      Response.Write( "Return Value = " & vRtnValue )
    %>
                            


Keywords: kberrmsg kbhowto kbcodesnippet kbaspobj KB218454