Microsoft Knowledge Base Email Alertz

(218454) - 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...

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 218454 - Last Review: July 1, 2004 - Revision: 4.2

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

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

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++, 32-bit Learning Edition 6.0
  • Microsoft Internet Information Server 4.0
  • Microsoft Internet Information Services 5.0
Keywords: 
kbaspobj kbcodesnippet kberrmsg kbhowto KB218454
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate