Help and Support

FIX: Error in COleDispatchException Constructor

Article ID:140590
Last Review:November 21, 2006
Revision:3.1
This article was previously published under Q140590
On This Page

SYMPTOMS

In an MFC application, AfxThrowOleDispatchException is used to throw an exception from within an OLE Automation function. When Microsoft Visual Basic is used as the automation client application, it will not be able to correctly interpret the error code passed to AfxThrowOleDispatchException.

Back to the top

CAUSE

AfxThrowOleDispatchException constructs and throws a COleDispatchException object, which is used to handle exceptions specific to the OLE IDispatch interface. The constructor of COleDispatchException incorrectly initializes the COleDispatchException object, resulting in the Visual Basic client being unable to interpret the error code of the exception.

Back to the top

RESOLUTION

To implement the proper behavior for throwing an OLE dispatch exception from an MFC server, do not use AfxThrowOleDispatchException. Instead, construct your own COleDispatchException, set m_scError = 0, and throw the exception yourself as illustrated in the sample code in this article.

Back to the top

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug was corrected in Visual C++ 4.1.

Back to the top

MORE INFORMATION

COleDispatchException has two public members (m_wCode and m_scError) which are mapped by COleDispatchException::Process() to the wCode and scode members of an EXCEPINFO structure. An EXCEPTINFO structure is used by an automation object to describe an exception that occurred during IDispatch::Invoke. When an automation object raises an exception, it should store an error code inside either wCode or scode but not both. If an error code is passed to the constructor of the COleDispatchException object, the values of both wCode and scode in the EXCEPINFO structure will be set.

Back to the top

Sample Code

// Insert the following code instead of AfxThrowOleDispatchException
COleDispatchException* pException;
pException = new COleDispatchException(
                 _T("Some Exception"), 200, 425);
pException->m_scError = 0;
THROW(pException);
				

Back to the top

REFERENCES

"Inside OLE" second edition by Kraig Brockschmidt, published by Microsoft Press, Chapter 14, pages 658 - 660.

Back to the top


APPLIES TO
Microsoft Visual Basic 4.0 Standard Edition
Microsoft Visual Basic 4.0 Professional Edition
Microsoft Foundation Class Library 4.2, when used with:
  Microsoft Visual C++ 1.5 Professional Edition
  Microsoft Visual C++ 1.51
  Microsoft Visual C++ 1.52 Professional Edition
  Microsoft Visual C++ 2.0 Professional Edition
  Microsoft Visual C++ 2.1
  Microsoft Visual C++ 2.2
  Microsoft Visual C++ 4.0 Standard Edition

Back to the top

Keywords: 
kbautomation kbbug kbfix kbnoupdate kbvc410fix KB140590

Back to the top

Article Translations

 

Other Support Options

  • Contact Microsoft
    Phone Numbers, Support Options and Pricing, Online Help, and more.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.