Microsoft KB Archive/173550

= How to use Visual C++ to start a CDO (1.x) session =

Article ID: 173550

Article Last Modified on 8/24/2005

-

APPLIES TO


 * Microsoft Collaboration Data Objects 1.1
 * Microsoft Collaboration Data Objects 1.2
 * Microsoft Collaboration Data Objects 1.21

-



This article was previously published under Q173550



SUMMARY
This article covers these three primary ways to create and then logon to a CDO (1.1, 1.2, 1.21) Session from Visual C++:
 * With prompt for Profile
 * Without prompt for Profile
 * Creating a dynamic Profile programmatically



With Prompt for Profile
/**********************************************************/    // W_PROMPT.CPP //    // This program demonstrates how to use the Session->Logon // of the CDO (1.1, 1.2, 1.21) Library via VC++, to prompt // the user for a Profile at Logon. This sample requires // VC++ version 5.0 or higher. /**********************************************************/

// To the Developer: // There are a couple of sections of this code with action items marked // "TO DO:" for you to resolve before it will successfully run.

// TO DO: Uncomment the appropriate statement //  and provide the full path to the dll. // If you are using CDO (1.1) uncomment the next line: // #import  no_namespace // If you are using CDO (1.2, 1.21) uncomment the next line: // #import "c:\\program files\\common files\\system\\mapi\\1033\\cdo.dll

#include    #include     #include 

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 {         // TO DO: // Create a MAPI.Session pointer // For CDO (1.1) uncomment the next line // SessionPtr pSession("MAPI.Session"); // For CDO (1.2, 1.21) uncomment the next line // _SessionPtr pSession("MAPI.Session");

// Logon prompting the user for a profile pSession->Logon(vtMissing, vtMissing, true);

// 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 CDO (1.1, 1.2, 1.21) Library via VC++, to not // prompt the user for a Profile at Logon. This sample // requires VC++ version 5.0 or higher. /**********************************************************/

// To the Developer: // There are a couple of sections of this code with action items marked // "TO DO:" for you to resolve before it will successfully run.

// TO DO: Uncomment the appropriate statement //  and provide the full path to the dll. // If you are using CDO (1.1) uncomment the next line: // #import  no_namespace // If you are using CDO (1.2, 1.21) uncomment the next line: // #import "c:\\program files\\common files\\system\\mapi\\1033\\cdo.dll

#include    #include     #include 

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 {         // TO DO: // Create a MAPI.Session pointer // For CDO (1.1) uncomment the next line // SessionPtr pSession("MAPI.Session"); // For CDO (1.2, 1.21) uncomment the next line // _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 CDO (1.1, 1.2, 1.21) Library 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 CDO (1.1, 1.2, 1.21) Library will // generate a random name for the profile. //    // For an authenticated profile, the format of the string is: //    //   + \n + //    // 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: //    //   + \n\n + "anon" //    // where the distinguished name of the server takes the form: //    //   /o= /ou= /cn=Configuration/cn=Servers/cn= //    //     // This sample demonstrates an authenticated logon, and // requires Visual C++ version 5.0 or higher. /**********************************************************/

// To the Developer: // There are a couple of sections of this code with action items marked // "TO DO:" for you to resolve before it will successfully run.

// TO DO: Uncomment the appropriate statement //  and provide the full path to the dll. // If you are using CDO (1.1) uncomment the next line: // #import  no_namespace // If you are using CDO (1.2, 1.21) uncomment the next line: // #import "c:\\program files\\common files\\system\\mapi\\1033\\cdo.dll

#include    #include     #include 

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 {         // TO DO: // Create a MAPI.Session pointer // For CDO (1.1) uncomment the next line // SessionPtr pSession("MAPI.Session"); // For CDO (1.2, 1.21) uncomment the next line // _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); }   }

