PRB: "ASSERTION FAILED" with Excel 5.0 Automation Classes

Last reviewed: July 22, 1997
Article ID: Q114372
1.50 WINDOWS kbprg kbprb

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with: Microsoft Visual C++ for Windows, version 1.5

SYMPTOMS

When calling methods of Automation objects for Microsoft Excel 5.0, you may receive the following message:

   ASSERTION FAILED oledisp2.cpp, line 352!

CAUSE

ClassWizard creates the COleDispatchDriver-derived classes for use with Excel 5.0 by reading the Excel type library, XLEN50.OLB. The information in that type library states that some parameters passed to, and return values received from IDispatch::Invoke are of type VARIANT. These VARIANTs are unions of several data types with a VARTYPE member specifying the actual contained data. MFC 2.5 source file OLEDISP2.CPP contains the following code at line 352:

   ASSERT(vtRet == vaResult.vt);

This assertion checks the VARTYPE member to determine what is actually contained in the VARIANT. The type library information is saying that this should be VT_VARIANT, while the actual returned value is different.

Refer to the header file VARIANT.H in your include directory for the possible data types and their VARTYPE specifiers.

RESOLUTION

The intent of this ASSERT is to ensure strong type checking of arguments used in IDispatch::Invoke calls to the Automation server. There are several solutions:

  • The function of the class generated by ClassWizard may be edited to return the actual type specified by the VARTYPE member of the VARIANT after the call to IDispatch::Invoke. This requires stepping through the code up to the point after the call to Invoke and examining the VARTYPE vt member of the returned VARIANT. Match this against the possible values in VARIANT.H and then alter the member function of the class to use and return this type.
  • Another solution is to directly edit the MFC source code. Move the ASSERT on line 352 to line 351 in OLEDISP2.CPP, inside the closing brace of the if(vtRet!=VT_VARIANT) block.

Before these changes were done, ignoring the assertion would cause the entire VARIANT to be returned. With the first change listed above, the assertion goes away and only the correct member of the VARIANT specified by VARESULT.VT is returned. The code for each member function must be edited to reflect this. With the second change listed above, the assertion goes away and the entire VARIANT struct is returned. After this change occurs, a similar assertion may be placed in the function for strong type checking.

In Visual C++ for Windows versions 1.51 and later and Visual C++ 32-bit edition, MFC has been modified as described in the second solution above. If you wish to perform strong type checking on VARIANT arguments to an automation method, place an assertion in your COleDispatchDriver derived class member function.

Sample Code

// Machine generated IDispatch driver class(es) created with
// ClassWizard.

// This is the function generated by ClassWizard
VARIANT Worksheet::Application() {
     VARIANT result;
     InvokeHelper(0x94,DISPATCH_METHOD,VT_VARIANT,(void*)&result,NULL);
     return result;
}

// This is the same function with change #1
LPDISPATCH Worksheet::Application() {
     LPDISPATCH lpDispatch;
     InvokeHelper(0x94, DISPATCH_METHOD, VT_DISPATCH,
                 (void*)&lpDispatch, NULL);
     return lpDispatch;
}

// This is the same function with change #2
VARIANT Worksheet::Application() {
     VARIANT result;
     InvokeHelper(0x94,DISPATCH_METHOD,VT_VARIANT,(void*)&result,NULL);
     ASSERT(result.vt == VT_DISPATCH);

     return result.;
}


Additional reference words: 1.50 2.50 automation displatch ole oledisp2
KBCategory: kbprg kbprb
KBSubcategory: MfcOLE
Keywords : kb16bitonly
Technology : kbMfc


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: July 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.