How To Delete a Mailbox Using CDOEXM and Visual C++ (297398)



The information in this article applies to:

  • Microsoft Windows 2000 Server SP1
  • Microsoft Windows 2000 Server SP2
  • Microsoft Windows 2000 Advanced Server SP1
  • Microsoft Windows 2000 Advanced Server SP2
  • Microsoft Active Directory Services Interface, System Component
  • Microsoft Exchange 2000 Enterprise Server
  • Microsoft Exchange 2000 Server
  • Microsoft Visual C++, 32-bit Editions 6.0

This article was previously published under Q297398

SUMMARY

This article demonstrates two ways to use Visual C++ to programmatically disable the mailbox of a Windows 2000 mailbox-enabled domain user. The first sample demonstrates this by using Active Directory Service Interfaces (ADSI) and Collaborative Data Objects for Exchange Management (CDOEXM). The second sample demonstrates this through the use of Collaboration Data Objects (CDO) and CDOEXM.

MORE INFORMATION

Background

In Microsoft Exchange 2000, deleting a mailbox requires mailbox-disabling the user object in the Active Directory. The mailbox resources in the information store are not marked for deletion until later during the regular store maintenance.

The CDOEXM::IMailboxStore interface performs mailbox-related tasks like enabling mailboxes for users, disabling mailboxes, and moving user mailboxes across mailbox stores in a Windows 2000 domain.

The CDOEXM interfaces are available in Cdoexm.dll, which is installed with the Microsoft Exchange 2000 System Management Tools only on a computer running Windows 2000 Server or Windows 2000 Advanced Server. Do not manually register this file into the registry.

The CDOEXM interfaces can be derived either from ADSI or CDO interfaces and cannot be directly used to bind to an object in the Active Directory. For additional information about the advantages and disadvantages of using each of these interfaces, click the following article number to view the article in the Microsoft Knowledge Base:

297390 INFO: Comparing Use of ADSI and CDO to Access CDOEXM Recipient-Related Methods

The CDOEXM::IMailboxStore method, DeleteMailbox, deletes several properties that are the mail and mailbox-related attributes on the user object in the Active Directory.

Using ADSI

This sample requires Adsiid.lib and Activeds.lib for the ADSI interfaces, which can be found in the Libs folder under a Platform SDK installation.

The Platform SDK can be downloaded from the following Microsoft Web site: In addition to these ADSI libraries, this sample requires Cdoexm.dll. To build the sample, include Adsiid.lib and Activeds.lib in the Object/library modules textbox, which is accessible from the Project menu by clicking Settings, and then clicking the Link tab.

Sample Code

#include "stdio.h"
#include "activeds.h"

// Importing the libraries required to use CDOEXM
#import <G:\Program Files\Exchsvr\BIN\cdoexm.dll> no_namespace raw_interfaces_only exclude("IDataSource2")

int main(void)
{
	// Declare variables
	HRESULT hr;
	LPWSTR strUserLDAPPath;
	IADsUser *pUser = NULL;
	IMailboxStore *pMailbox = NULL;

	//Set the LDAP path
	strUserLDAPPath = L"LDAP://dsadsidom/CN=user1 vishals,OU=VishOU,DC=dsadsidom,DC=nttest,DC=microsoft,DC=com";

	//Initialize COM
	hr = CoInitialize(NULL);
	if (FAILED(hr)) {
		wprintf(L"Failed to CoInitialize: 0x%x\n", hr);
		return hr;
	}

	//Bind to the existing mailbox enabled user using ADSI
	hr = ADsGetObject(strUserLDAPPath, IID_IADsUser, (void**)&pUser);
	if (FAILED(hr)) {
		wprintf(L"Failed to get IADsUser: 0x%x\n", hr);
		pUser->Release();
		CoUninitialize();
		return hr;
	}

	//Query for the IMailboxStore Interface using the IADsUser object
	hr = pUser->QueryInterface(__uuidof(IMailboxStore), (void**)&pMailbox);
	if (!SUCCEEDED(hr)) 
	{
		wprintf(L"QueryInterface of IMailboxStore failed!\n");
		pUser->Release();
		pMailbox->Release();
		CoUninitialize();
		return hr;
	}

	//Delete Mailbox in Cache
	hr = pMailbox->DeleteMailbox();
	if (!SUCCEEDED(hr)) 
	{
		wprintf(L"DeleteMailbox failed!\n");
		pUser->Release();
		pMailbox->Release();
		CoUninitialize();
		return hr;
	}

	//Apply changes made to the cache back onto the user object in the Active Directory
	hr = pUser->SetInfo();
	if (!SUCCEEDED(hr)) 
	{
		wprintf(L"SetInfo failed!\n");
		pUser->Release();
		pMailbox->Release();
		CoUninitialize();
		return hr;
	}

	//Cleanup and uninitialize COM
	pUser->Release();
	pMailbox->Release();
	pUser = NULL;
	pMailbox = NULL;
	CoUninitialize();
	return 0;
}
				

Using CDO

This sample requires Cdoex.dll for the IPerson interface on the CDO.Person object. Cdoex.dll is available only on an Exchange 2000 Server. In addition to Cdoex.dll, this sample requires Cdoexm.dll. This sample can only be run on an Exchange 2000 Server. Do not manually attempt to register either .dll file into the registry to work around this limitation.

Sample Code

#include "stdio.h"
 
// Importing the libraries required to use CDOEXM
#import <c:\program files\common files\system\ado\msado15.dll> no_namespace raw_interfaces_only
#import <C:\Program Files\Common Files\Microsoft Shared\CDO\cdoex.dll> no_namespace raw_interfaces_only rename("Folder","CDOEXFolder") 
#import <G:\Program Files\Exchsvr\BIN\cdoexm.dll> no_namespace raw_interfaces_only
 
int main(void)
{
 // Declare variables
 HRESULT hr;
 LPWSTR strUserLDAPPath;
 IPerson* pUser = NULL;
 IDataSource* pDataSource = NULL;
 IMailboxStore *pMailbox = NULL;
 
 //Set the LDAP path
 strUserLDAPPath = L"LDAP://dsadsidom/CN=user1 vishals,OU=VishOU,DC=dsadsidom,DC=nttest,DC=microsoft,DC=com";
 
 //Initialize COM
 hr = CoInitialize(NULL);
 if (FAILED(hr)) {
  wprintf(L"Failed to CoInitialize: 0x%x\n", hr);
  return hr;
 }
 
 //Create Instance of IPerson interface
 hr = CoCreateInstance( __uuidof(Person),
                      NULL,
                      CLSCTX_INPROC_SERVER,
                      __uuidof(IPerson),
                     (void**) &pUser
                     );
 if (FAILED(hr)) {
  wprintf(L"Failed to Create Instance of IPerson object: 0x%x\n", hr);
  pUser->Release();
  pUser = NULL;
  CoUninitialize();
  return hr;
 }
 
 hr = pUser->get_DataSource(&pDataSource);
 if (FAILED(hr)) {
  wprintf(L"Failed to get_DataSource: 0x%x\n", hr);
  pUser->Release();
  pDataSource->Release();
  pUser = NULL;
  pDataSource = NULL;
  CoUninitialize();
  return hr;
 }
 
 //Bind to the existing mailbox enabled user
 hr = pDataSource->Open(strUserLDAPPath,
       NULL,
       adModeReadWrite,
       adFailIfNotExists,
       adOpenSource,
       L"",
       L"");
 if (FAILED(hr)) {
  wprintf(L"Failed to bind to LDAP path: 0x%x\n", hr);
  pUser->Release();
  pDataSource->Release();
  pUser = NULL;
  pDataSource = NULL;
  CoUninitialize();
  return hr;
 }
 
 //Query for the IMailboxStore Interface using the IADsUser object
 hr = pUser->QueryInterface(__uuidof(IMailboxStore), (void**)&pMailbox);
 if (!SUCCEEDED(hr)) 
 {
  wprintf(L"QueryInterface of IMailboxStore failed!\n");
  pUser->Release();
  pDataSource->Release();
  pMailbox->Release();
  pUser = NULL;
  pMailbox = NULL;
  pDataSource = NULL;
  CoUninitialize();
  return hr;
 }
 
 //Delete Mailbox in Cache
 hr = pMailbox->DeleteMailbox();
 if (!SUCCEEDED(hr)) 
 {
  wprintf(L"DeleteMailbox failed!\n");
  pUser->Release();
  pMailbox->Release();
  pDataSource->Release();
  pUser = NULL;
  pMailbox = NULL;
  pDataSource = NULL;
  CoUninitialize();
  return hr;
 }
 
 //Update changes made by CDOEXM by Saving DataSource
 hr = pDataSource->Save();
 if (!SUCCEEDED(hr)) 
 {
  wprintf(L"Failed to Save DataSource!\n");
  pUser->Release();
  pMailbox->Release();
  pDataSource->Release();
  pUser = NULL;
  pMailbox = NULL;
  pDataSource = NULL;
  CoUninitialize();
  return hr;
 }
 
 CoUninitialize();
 return 0;
}
				

REFERENCES

For more information about CDOEXM::IMailBoxStore, see:

Modification Type:MinorLast Reviewed:7/1/2004
Keywords:kbDSWADSI2003Swept kbDSWADSI2003Swept kbhowto KB297398