PRB: CDO (1.x) Session GetDefaultFolder Method Fails with VARIANT (179233)



The information in this article applies to:

  • Collaboration Data Objects (CDO) 1.2
  • Collaboration Data Objects (CDO) 1.21

This article was previously published under Q179233

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.

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) {
          ....
    }
    //----------------------------------------------------
				

Modification Type:MinorLast Reviewed:3/4/2004
Keywords:kbcode kberrmsg kbMsg kbprb KB179233