How to use Visual C++ to start a CDO (1.x) session (173550)
The information in this article applies to:
- Collaboration Data Objects (CDO) 1.1
- Collaboration Data Objects (CDO) 1.2
- Collaboration Data Objects (CDO) 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
MORE INFORMATIONWith 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 <olemsg32.dll> 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 <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
{
// 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 <olemsg32.dll> 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 <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
{
// 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:
//
// <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.
/**********************************************************/
// 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 <olemsg32.dll> 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 <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
{
// 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);
}
}
REFERENCES
For information on obtaining the CDO (1.x) Library, please see the
following article in the Microsoft Knowledge Base:
171440
INFO: Where to Acquire the CDO (1.x) Libraries
| Modification Type: | Major | Last Reviewed: | 8/24/2005 |
|---|
| Keywords: | kbcode kbhowto kbMsg KB173550 kbAudDeveloper |
|---|
|