Deleting 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.
//Deleting an Existing Contact with CDO
//Link to the activeds.lib and adsiid.lib libraries.
#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 DeleteContacts(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);
// TODO: Change the user name and full name of the contact.
// Delete the contact "John Smith" in the "User1" mailbox.
hr = DeleteContacts(bstrDomainDNSName, "User1", "john smith");
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// DeleteContacts
//
// Params:
// [in] LPSTR lpUser Username
// [in] LPSTR lpContactName Full Name of a contact to be deleted
// Output: HRESULT
// Purpose: Delete a contact based on the full name.
/////////////////////////////////////////////////////////////////////////////////////////////////
HRESULT DeleteContacts(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))
{
hr = pMyContacts->Delete(adAffectCurrent);
if (SUCCEEDED(hr))
printf("Delete Contact: %s\n", lpContactName);
else
printf("Delete Contact: %s Failed\n", lpContactName);
}
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;
}