PRB: GetDefaultFolder Method of CDO Session Fails with VARIANT

Last reviewed: January 13, 1998
Article ID: Q179233
The information in this article applies to:
  • Collaboration Data Objects (CDO), version 1.2

SYMPTOMS

When writing a C++ program that uses the Collaboration Data Objects (CDO) library, the reference to a pointer to a folder declared as a VARIANT data type causes a system error in the program. The error that occurs is:

   Unhandled Exception: Privileged Instruction.

CAUSE

The GetDefaultFolder method returns a value of type variant_t on the stack. This is copied to the VARIANT variable. The default constructor of the VARIANT does not increment the reference counter for the dispval member so that when the temporary variant_t is destroyed, dispatch member of the VARIANT is no longer valid.

RESOLUTION

Declare the variable that holds the result of the GetDefaultFolder method as variant_t rather than VARIANT. The constructor of the variant_t increments the reference counter for the resulting folder pointer, so that the dispatch member remains valid.

For example, in the code sample below, the variable vtFolder should be declared as type variant_t not as type VARIANT.

STATUS

Microsoft is researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

Steps to Reproduce Behavior

The following portion of code illustrates the problem:

   //----------------------------------------------------
   #import "cdo.dll"

    try {
         VARIANT vtCalendar;

         VARIANT vtFolder;  // To Correct Problem: Remark this line and
         //variant_t vtFolder; // un-remark this line.

         _SessionPtr pSession("MAPI.Session");
         pSession->Logon();

         vtCalendar.vt = VT_I4;
         vtCalendar.lVal = CdoDefaultFolderCalendar;

         vtFolder = pSession->GetDefaultFolder(vtCalendar);

         // The problem occurs in the "smart-pointer" FolderPtr
         // constructor that attempts to call QueryInterface
         // on the IDispatch pointer.
         FolderPtr pFolder = vtFolder.pdispVal;

         pSession->Logoff();

      } catch(_com_error e) {
          ....
    }
    //----------------------------------------------------

Keywords          : kbcode cdo
Version           : WINDOWS:1.2
Platform          : WINDOWS
Issue type        : kbprb


================================================================================


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