Microsoft KB Archive/197957

= PRB: Passing Parameters By Reference to a VC COM Object =

Article ID: 197957

Article Last Modified on 3/21/2005

-

APPLIES TO


 * Microsoft Active Server Pages 4.0
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 5.0 Professional 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 Q197957



SYMPTOMS
When passing a parameter to a COM Component's method in Active Server Pages (ASP) using VBScript, either the following error occurs:

Microsoft VBScript runtime error '800a000d' Type mismatch

Or the value stored in the variable passed to the method is not changed.



CAUSE
VBScript will pass parameters to a method by value if the argument's data type is NOT declared as a variant and the parameter is passed by reference if the argument's data type is declared as variant by the method.



RESOLUTION
Parameters to be passed as [out] parameters (that is, by reference) to a method should always be declared as a pointer to a variant data type by the method, while [in] parameters (that is, passed by value) can be declared as any type.



STATUS
This behavior is by design.



Steps to Reproduce Behavior
 Create an ATL DLL Project called "ByRefProj." Insert an ATL Object named "ByRefObj." Add and implement the method.   Add a method with the following information:      Method Name: ByRefMethod Parameters : [out]BSTR* bstrVal   Implement the method as follows: STDMETHODIMP CByRefObj::ByRefMethod( BSTR* bstrVal ) { CComBSTR bstrRtnVal = L"This variable is passed by Reference"; *bstrVal = bstrRtnVal.Detach; return S_OK; }                       </li></ol> </li>  Create an Active Server Pages (ASP) page that calls this method: <% Dim objTest, strByRefVal Set objTest = Server.CreateObject("ByRefProj.ByRefObj") objTest.ByRefMethod strByRefVal %> </li> Run ASP, and the following error occurs:

Microsoft VBScript runtime error '800a000d'

Type mismatch: 'ByRefMethod'

</li></ol>

To fix this problem, change the type to VARIANT: <ol> Change the IDL declaration in the ByRefProj.idl file to:

<pre class="fixed_text">[id(1), helpstring("method ByRefMethod")] HRESULT ByRefMethod([out] VARIANT* vVal);

</li> Change the method in the .h and .cpp file. Change the method as follows:

<pre class="fixed_text">// Where m_bstr is a BSTR member of SomeComObject. vVal->vt = VT_BSTR; vVal->bstrVal = m_bstr.Copy;

</li> Recompile.</li></ol>

<div class="references_section">