![]() |
|
||||
Starting a CDO Session#include <afxwin.h>
#define dispidM_Logon 119
int main()
// Create an instance of the Session object.
// Get IDispatch interface.
Let's go through code and see what happens. Before entering main I define the dispatch identifier (119) for Logon method for CDO session. Knowledge of this ID saves me one function call to IDispatch::GetIDsOfNames. I will need this ID to put in the following IDispatch::Invoke call. GUID_OM_SESSION uuid identifies CDO session object. I pass it to the CoCreateInstance function immediately after initializing COM library. I use the resulting IUnknown pointer to inquire about IDispatch interface and then call its Invoke method to process logon. That's standard COM way of doing things. Alternatively, you could use the COleDispatchDriver MFC class and benefit from the opportunity of ClassWizard to generate COleDispatchDriver derived classes automatically. This is accomplished in the following way:
The following code fragment (from my CDO/SessionMFC project) describes how you can start a CDO session in this environment: void CSessionMFCDlg::OnStart()
The first thing I do here is call the COleDispatchDriver::CreateDispatch method. This creates an IDispatch interface and attaches it to the session object. The Logon in this fragment is done via the InvokeHelper call. You may wonder why I do it in the following way instead of calling the Logon method generated by the ClassWizard. If you take a closer look at Logon you will see that it takes 7 parameters, all VARIANTs. Initializing such number of VARIANTs is painful. I made a shortcut by looking at how ClassWizard implements the Logon and doing the same with little overhead. Notice the 0x77 parameter (decimal 119), used in the first code fragment. Although ClassWizard in fact generates a lot of code, it does not provide you with convenient methods. You still need to do a lot of initialization work yourself, or overload methods. In this context, Visual Basic would be perhaps a better choice of development environment. You should also consider Visual Basic for Active Server Pages programming. My goal now, however, is to demonstrate C++ way of using CDO. Microsoft provides the CDOMFC sample, where you should definitely take a look if you are going this way. This sample is a dialog based app, with which you can create and send e-mail messages. The classes generated by ClassWizard have been modified to simplify usage (by providing the Logon function with no parameters, for example, in the way similar to the above). One other detail that I need to address here is exception orientation
of COleDispatchDriver derived classes. This means the code will throw if
something goes wrong. Thus, you need to use try/catch blocks to handle
errors.
|