Creating an Appointment with CDOEX (C++)

Topic Last Modified: 2006-06-12

Visual C++

Note

The following example uses a file URL with the Exchange OLE DB (ExOLEDB) provider. The ExOLEDB provider also supports The HTTP: URL Scheme. Using The HTTP: URL Scheme allows both client and server applications to use a single URL scheme.

//Creating an Appointment with CDOEx

#import <cdoex.dll> rename_namespace("CDO") raw_interfaces_only

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

enum ConnectModeEnum
{
    adModeUnknown = 0,
    adModeRead = 1,
    adModeWrite = 2,
    adModeReadWrite = 3,
    adModeShareDenyRead = 4,
    adModeShareDenyWrite = 8,
    adModeShareExclusive = 12,
    adModeShareDenyNone = 16,
    adModeRecursive = 4194304
};

enum RecordCreateOptionsEnum
{
    adCreateCollection = 8192,
    adCreateStructDoc = -2147483648,
    adCreateNonCollection = 0,
    adOpenIfExists = 33554432,
    adCreateOverwrite = 67108864,
    adFailIfNotExists = -1
};

enum RecordOpenOptionsEnum
{
    adOpenRecordUnspecified = -1,
    adOpenSource = 8388608,
    adOpenAsync = 4096,
    adDelayFetchStream = 16384,
    adDelayFetchFields = 32768
};

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);
}

struct StartOle {
    StartOle() { CoInitialize(NULL); }
    ~StartOle() { CoUninitialize(); }
} _inst_StartOle;

void main(int argc, char** argv)
{
   HRESULT hr = S_OK;

   if(argc != 2)
   {
         printf("Usage: CreateAppt\r\n");
         printf("Example: CreateAppt \"file://./backofficestorage/mydomain.contoso.com/MBX/User1/calendar\"\n");
        exit(0);
   }

   wchar_t* folderURL = NULL;
   size_t size;
   size = mbstowcs(NULL,argv[1],0);
   folderURL = (wchar_t*)malloc(size * sizeof(wchar_t*) + 1);
   mbstowcs(folderURL,argv[1],size+1);

   BSTR bstrFolderURL = SysAllocString(folderURL);

   try
   {

      CDO::IAppointmentPtr pAppt(_uuidof(CDO::Appointment));
      CDO::IDataSourcePtr pDsrc;

      SYSTEMTIME st = {0};
      SYSTEMTIME et = {0};
      DATE dst, det;

      st.wYear = 1999;
      st.wMonth = 12;
      st.wDay = 21;
      st.wHour = 15;
      st.wMinute = 30;

      et.wYear = 1999;
      et.wMonth = 12;
      et.wDay = 21;
      et.wHour = 16;
      et.wMinute = 30;

      // Convert the system value to double date.
      SystemTimeToVariantTime(&st, &dst);
      SystemTimeToVariantTime(&et, &det);

      // Set the properties of the appointment.
      pAppt->put_Subject(L"CDO_VC:Important Appointment");
      pAppt->put_Location(L"Conference Room");
      pAppt->put_TextBody(L"Be there on time");
      pAppt->put_StartTime(dst);
      pAppt->put_EndTime(det);
      pAppt->get_DataSource(&pDsrc);

      // Save the appointment.
      // Note: using adCreateOverwrite may cause an existing
      // appointment to be overwritten.
      hr = pDsrc->SaveToContainer(bstrFolderURL,
                        0,
                        (enum CDO::ConnectModeEnum) adModeReadWrite,
                        (enum CDO::RecordCreateOptionsEnum)adCreateOverwrite,
                        (enum CDO::RecordOpenOptionsEnum)adOpenSource,
                        L"",
                        L"");
      if (SUCCEEDED(hr))
         _tprintf(_T("Appointment created\n"));

   }
   catch(_com_error &e)
   {
      dump_com_error(e);
   }

   SysFreeString(bstrFolderURL);
}