PRB: GetDefaultFolder Method of CDO Session Fails with VARIANT

ID: Q179233

The information in this article applies to:

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 kberrmsg kbCDO120 kbVC kbGrpMsg 
Version           : WINDOWS:1.2
Platform          : WINDOWS
Issue type        : kbprb

Last Reviewed: April 8, 1999