HOWTO: Using VC++ to Start an Active Messaging Session

ID: Q173550

The information in this article applies to:

SUMMARY

This article covers these three primary ways to create and then logon to an Active Messaging Session from Visual C++:

MORE INFORMATION

With Prompt for Profile

    /**********************************************************/ 
    // W_PROMPT.CPP
    // ------------
    // This program demonstrates how to use the Session->Logon
    // of the Active Messaging Library v1.1 via VC++, to prompt
    // the user for a Profile at Logon.  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
      {
          // Create a MAPI.Session pointer
          SessionPtr pSession("MAPI.Session");

          // Logon prompting the user for a profile
          pSession->Logon;

          // The remaining functionality of your app takes place here

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

Without Prompt for Profile

    /**********************************************************/ 
    // WO_PROMPT.CPP
    // -------------
    // This program demonstrates how to use the Session->Logon
    // of the Active Messaging Library v1.1 via VC++, to not
    // prompt the user for a Profile at Logon.  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
      {
          // Create a MAPI.Session pointer
          SessionPtr pSession("MAPI.Session");

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

          // The remaining functionality of your app takes place here

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

Creating a Dynamic Profile Programmatically

    /**********************************************************/ 
    // DYN_PROF.CPP
    // ------------
    // This program demonstrates how to use the Session->Logon
    // of the Active Messaging Library v1.1 via VC++, to create
    // a dynamic Profile at Logon. This is the likely method to
    // use if the application will be run as a Windows NT Service.
    // 
    // The key point of this sample is the final parameter to the
    // logon, which allows for creation of a temporary profile
    // for the session. The Active Messaging Library generates a
    // random name for the profile.
    // 
    // For an authenticated profile, the format of the string is:
    // 
    //   <server name> + \n + <mailbox name>
    // 
    // where the server and mailbox names can be unresolved. Note
    // that the mailbox name is not the messaging user's display
    // name, but rather the alias or account name used internally
    // by the user's organization. For example, "johnd" should be
    // used instead of "John Doe".
    // 
    // For an anonymous profile, the format is:
    // 
    //   <server distinguished name> + \n\n + "anon"
    // 
    // where the distinguished name of the server takes the form:
    // 
    //   /o=<enterprise>/ou=<site>/cn=Configuration/cn=Servers/cn=<server>
    // 
    // 
    // This sample demonstrates an authenticated logon, and
    // requires Visual C++ 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
      {
          // Create a MAPI.Session pointer
          SessionPtr pSession("MAPI.Session");

          // Create the params used in the Logon including the
          // string used for the ProfileInfo
          VARIANTARG vBoolF;
          vBoolF.vt = VT_BOOL;
          vBoolF.boolVal = FALSE;
          VARIANTARG vBoolT;
          vBoolT.vt = VT_BOOL;
          vBoolT.boolVal = TRUE;

          char * pstrProfileInfo ;

          //Modify malloc param to size needed by your app
          pstrProfileInfo = (char *) malloc (27) ;
          strcpy (pstrProfileInfo, "MyServerName\nMyMailBoxName") ;

          // Logon using the specified profile
          // params: profileName, profilePassword, showDialog,
          //         newSession, parentWindow, NoMail, ProfileInfo
          pSession->Logon("",
                          "",
                          vBoolF,
                          vBoolT,
                          vBoolF,
                          vBoolF,
                          pstrProfileInfo);

          // Display generated ProfileName to prove the we are logged on
          MessageBoxW(NULL,pSession->Name.bstrVal,L"",MB_OK) ;

          // The remaining functionality of your app takes place here

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

REFERENCES

For information on obtaining the Active Messaging Library, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q171440
   TITLE     : INFO: Where to Acquire the Collaboration Data Objects
               Libraries

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)

The Microsoft Developer Network, January 1997 or later.

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

Last Reviewed: April 8, 1999