HOWTO: Set and Clear an Active Messaging MessageFilter via VC++

ID: Q171429

The information in this article applies to:

SUMMARY

Changes to the Active Messaging MessageFilter properties appear to have no effect.

MORE INFORMATION

The following code sample shows how to activate, then deactivate an Active Messaging MessageFilter via Visual C++.

The key to successfully shutting or altering the MessageFilter is to be sure to destroy any reference that is made to any object related to the MessageFilter (also known as Folder:MessagesCollection:MessageFilter).

Code Sample:

   /**********************************************************/ 
   // MSGFILTER.CPP
   // -------------
   // This program demonstrates how the Active Messaging v1.1
   // MessageFilter works via VC++. This sample requires VC++
   // version 5.0 or higher.
   /**********************************************************/ 

   #import <olemsg32.dll> no_namespace

   #include <assert.h>
   #include <stdio.h>
   #include <tchar.h>

   void dump_com_error(_com_error &e)
   {
       _tprintf(_T("Oops - hit an error!\n"));
       _tprintf(_T("\a\tCode = %08lx\n"), e.Error());
       _tprintf(_T("\a\tCode meaning = %s\n"), e.ErrorMessage());
       _bstr_t bstrSource(e.Source());
       _bstr_t bstrDescription(e.Description());
       _tprintf(_T("\a\tSource = %s\n"), (LPCTSTR) bstrSource);
       _tprintf(_T("\a\tDescription = %s\n"), (LPCTSTR) bstrDescription);
    }

   // If this is placed in the scope of the smart pointers, they must be
   // explicitly Release(d) before CoUninitialize() is called.  If any
   // reference count is non-zero, a protection fault will occur.
   struct StartOle {
      StartOle() { CoInitialize(NULL); }
      ~StartOle() { CoUninitialize(); }
   } _inst_StartOle;

   void main()
   {
   try
      {
          // Uncomment one of the next two lines of code as appropriate:
          // Create a MAPI.Session pointer with CDO version 1.1
          // SessionPtr pSession("MAPI.Session");

          // Create a MAPI.Session pointer with CDO version 1.2 or higher
          //_SessionPtr pSession("MAPI.Session");

          // Logon using the specified profile
          pSession->Logon("YourValidProfileNameHere");

          // Create pointer to the Inbox Folder
          FolderPtr pFolder = pSession->Inbox;

          // Create pointer to the Messages Collection
          MessagesPtr pMessages = pFolder->Messages;

          // Get the first Message object before filter is applied
          MessagePtr pAMessage = pMessages->GetFirst();

          // Process contents of Folder
          while (pAMessage != NULL)
          {
              // Display Properties
              MessageBoxW (NULL,pAMessage->Subject.bstrVal,NULL,MB_OK);

              // Get next Message
              pAMessage = NULL;
              pAMessage = pMessages->GetNext();
          }

          // Destroy the reference to the to the Inbox, Messages
          // Collection, and Message then reaquire them
          pFolder = NULL;
          pMessages = NULL;
          pAMessage = NULL;

          // Aquire pointer to the Inbox Folder
          pFolder = pSession->Inbox;

          // Aquire pointer to the Messages Collection
          pMessages = pFolder->Messages;

          // Acquire pointer and set properties of the MessageFilter
          MessageFilterPtr pMsgFilt = pMessages->Filter;
          pMsgFilt->Unread = (bool)TRUE;

          // Get the first Message object after filter is applied
          pAMessage = pMessages->GetFirst();

          // Process contents of Folder
          while (pAMessage != NULL)
          {
              // Display Properties
              MessageBoxW (NULL,pAMessage->Subject.bstrVal,NULL,MB_OK);

              // Get next Message
              pAMessage = NULL;
              pAMessage = pMessages->GetNext();
          }

          // Destroy the reference to the Inbox, Messages Collection,
          // MessageFilter, and Message then reaquire them
          pFolder = NULL;
          pMessages = NULL;
          pMsgFilt = NULL;
          pAMessage = NULL;

          // GetFirst() without a filter in place
          // Aquire pointer to the Inbox Folder
          pFolder = pSession->Inbox;

          // Aquire pointer to the Messages Collection
          pMessages = pFolder->Messages;

          // Get the first Message object after filter is unapplied
          pAMessage = pMessages->GetFirst();

          // Process contents of Folder
          while (pAMessage != NULL)
          {
              // Display Properties
              MessageBoxW (NULL,pAMessage->Subject.bstrVal,NULL,MB_OK);

              // Get next Message
              pAMessage = NULL;
              pAMessage = pMessages->GetNext();
          }

          // Logoff of the MAPI Session
          pSession->Logoff();
      }
      catch (_com_error &e)
      {
          dump_com_error(e);
      }
   }

REFERENCES

For additional information about Collaboration Data Objects versus Active Messaging, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q176916
   TITLE     : INFO: Active Messaging and Collaboration Data Objects (CDO)

Keywords          : kbcode kbCDO110 kbMsg kbVC kbGrpMsg 
Version           : WINDOWS:1.1
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: April 15, 1999