Partager via


Adding a New Contact with CDOEX

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.

//Adding a New Contact 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: CreateContact\r\n");
         printf("Example: CreateContact \"file://./backofficestorage/mydomain.contoso.com/MBX/User1/Contacts\"\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::IPersonPtr pPerson(_uuidof(CDO::Person));
      CDO::IDataSourcePtr pDsrc;

      pPerson->put_FirstName(L"John");
      pPerson->put_LastName(L"Smith");
      pPerson->put_HomeCity(L"Los Angeles");
      pPerson->put_HomeState(L"California");
      pPerson->put_Email(L"JohnSmith@contoso.com");
      pPerson->get_DataSource(&pDsrc);

      // Note: using adCreateOverwrite may cause an existing
      // contact 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("Contact created\n"));

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

   SysFreeString(bstrFolderURL);
}