Changing an Existing Contact 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.
//Changing an Existing Contact with CDO
//You need to link to activeds.lib adsiid.lib.
#include <activeds.h>
#include <stdio.h>
#include <shlwapi.h>
#import <msado15.dll> no_namespace rename("EOF", "adoEOF")
#import <cdoex.dll> no_namespace
HRESULT GetDomainName(BSTR * bstrDomainName);
HRESULT ChangeContacts(BSTR bstrDomainName, LPSTR lpUser, LPSTR lpContactName);
struct StartOle {
StartOle() { CoInitialize(NULL); }
~StartOle() { CoUninitialize(); }
} _inst_StartOle;
void main()
{
HRESULT hr = S_OK;
BSTR bstrDomainDNSName;
// Get the domain name.
hr = GetDomainName(&bstrDomainDNSName);
printf("Domain DNS Name: %S\n", bstrDomainDNSName);
// Change the e-mail address of contact "John Smith" in the "User1" mailbox contact folder.
// TODO: Change the user name and full name of the contact.
hr = ChangeContacts(bstrDomainDNSName, "User1", "john smith");
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// ChangeContacts
//
// Params:
// [in] BSTR bstrDomainName Domain DNS Name
// [in] LPSTR lpUser Username
// [in] LPSTR lpContactName Full Name of a contact to be changed
// Output: HRESULT
// Purpose: Change the e-mail address of a contact based on its full name.
/////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT ChangeContacts(BSTR bstrDomainName, LPSTR lpUser, LPSTR lpContactName)
{
HRESULT hr = S_OK;
int iRecCount = 0;
_bstr_t szConnString = "file://./backofficestorage/" + (_bstr_t)bstrDomainName + "/MBX/" + (_bstr_t)lpUser + "/Contacts";
char ContactFullName[100];
try {
_ConnectionPtr pConn(_uuidof(Connection));
_RecordsetPtr pMyContacts(_uuidof(Recordset));
IPersonPtr pPerson(_uuidof(Person));
IDataSourcePtr pDsrc;
pConn->Provider = "Exoledb.DataSource";
hr = pConn->Open(szConnString, "", "", 0);
if (FAILED(hr))
{
printf("Open Connection Failed\n");
return hr;
}
if (pConn->State == adStateOpen)
printf("Connection Opened\n");
else
{
printf("Connection Failed\n");
return hr;
}
_bstr_t strSQL = "Select \"DAV:displayname\", \"DAV:href\" from scope ('shallow traversal of \""+ szConnString + "\"')";
hr = pMyContacts->Open(strSQL,
pConn->ConnectionString,
adOpenForwardOnly,
adLockOptimistic,
0);
if (FAILED(hr))
{
printf("Open Contacts Failed\n");
return hr;
}
printf("Open Contacts: \n");
iRecCount = pMyContacts->RecordCount;
if (iRecCount == 0)
{
printf("No Contacts Found\n\n");
if (SUCCEEDED(hr = pMyContacts->Close()))
printf("Close Contacts\n");
if (SUCCEEDED(hr = pConn->Close()))
printf("Close Connection\n");
return hr;
}
hr = pMyContacts->MoveFirst();
if (FAILED(hr))
{
printf("MoveFirst Failed\n");
return hr;
}
for (int i = 0; i < iRecCount; i++)
{
FieldsPtr pFlds = pMyContacts->GetFields();
FieldPtr pFld = pFlds->GetItem("DAV:href");
hr = pPerson->get_DataSource(&pDsrc);
hr = pDsrc->Open((_bstr_t)pFld->Value,
NULL,
adModeReadWrite,
adFailIfNotExists,
adOpenSource,
"",
"");
sprintf(ContactFullName, "%s %s", (char *)pPerson->FirstName, (char *)pPerson->LastName);
// StrCmpI is not case-sensitive
if (!StrCmpI(ContactFullName, lpContactName))
{
// TODO: replace with the appropriate e-mail address
pPerson->Email = "JohnSmith@contoso.com";
// save changes
hr = pDsrc->Save();
if (SUCCEEDED(hr))
printf("Contact changed\n");
else
printf("Failed to change contact\n");
break;
}
printf("\n");
hr = pMyContacts->MoveNext();
if (FAILED(hr))
{
printf("MoveNext Failed\n");
return hr;
}
pFlds = NULL;
pFld = NULL;
}
hr = pMyContacts->Close();
if (FAILED(hr))
{
printf("Close Contacts Failed\n");
return hr;
}
else
printf("Close Contacts\n");
hr = pConn->Close();
if (FAILED(hr))
{
printf("Close Connection Failed\n");
return hr;
}
else
printf("Connection Closed\n\n");
return hr;
}
catch(_com_error e)
{
printf("HResult = %x\n", e.Error());
printf("%S\n", e.Description());
return hr;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// GetDomainName
//
// Params: [out] BSTR * bstrDomainName
// Output: HRESULT
// Purpose: Retrieve the Domain DNS name.
/////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT GetDomainName(BSTR * bstrDomainName)
{
HRESULT hr = S_OK;
IADsADSystemInfo *pADsys;
hr = CoCreateInstance(CLSID_ADSystemInfo,
NULL,
CLSCTX_INPROC_SERVER,
IID_IADsADSystemInfo,
(void**)&pADsys);
hr = pADsys->get_DomainDNSName(bstrDomainName);
if (pADsys)
pADsys->Release();
return hr;
}