Microsoft KB Archive/297398

= How To Delete a Mailbox Using CDOEXM and Visual C++ =

Article ID: 297398

Article Last Modified on 2/23/2007

-

APPLIES TO


 * Microsoft Windows 2000 Service Pack 1
 * Microsoft Windows 2000 Service Pack 2
 * Microsoft Windows 2000 Advanced Server
 * Microsoft Windows 2000 Advanced Server
 * Microsoft Active Directory Service Interfaces 2.5
 * Microsoft Exchange 2000 Enterprise Server
 * Microsoft Exchange 2000 Server Standard Edition
 * Microsoft Visual C++ 6.0 Service Pack 5

-



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.



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:

http://www.microsoft.com/msdownload/platformsdk/setuplauncher.htm

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

 * 1) include &quot;stdio.h&quot;
 * 2) include &quot;activeds.h&quot;

// Importing the libraries required to use CDOEXM
 * 1) import  no_namespace raw_interfaces_only exclude(&quot;IDataSource2&quot;)

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

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

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

//Bind to the existing mailbox enabled user using ADSI hr = ADsGetObject(strUserLDAPPath, IID_IADsUser, (void**)&pUser); if (FAILED(hr)) { wprintf(L&quot;Failed to get IADsUser: 0x%x\n&quot;, 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&quot;QueryInterface of IMailboxStore failed!\n&quot;); pUser->Release; pMailbox->Release; CoUninitialize; return hr; }

//Delete Mailbox in Cache hr = pMailbox->DeleteMailbox; if (!SUCCEEDED(hr)) {       wprintf(L&quot;DeleteMailbox failed!\n&quot;); 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&quot;SetInfo failed!\n&quot;); 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
// Importing the libraries required to use CDOEXM int main(void) { // Declare variables HRESULT hr; LPWSTR strUserLDAPPath; IPerson* pUser = NULL; IDataSource* pDataSource = NULL; IMailboxStore *pMailbox = NULL; //Set the LDAP path strUserLDAPPath = L&quot;LDAP://dsadsidom/CN=user1 vishals,OU=VishOU,DC=dsadsidom,DC=nttest,DC=microsoft,DC=com&quot;; //Initialize COM hr = CoInitialize(NULL); if (FAILED(hr)) { wprintf(L&quot;Failed to CoInitialize: 0x%x\n&quot;, 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&quot;Failed to Create Instance of IPerson object: 0x%x\n&quot;, hr); pUser->Release; pUser = NULL; CoUninitialize; return hr; } hr = pUser->get_DataSource(&pDataSource); if (FAILED(hr)) { wprintf(L&quot;Failed to get_DataSource: 0x%x\n&quot;, 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&quot;&quot;,       L&quot;&quot;); if (FAILED(hr)) { wprintf(L&quot;Failed to bind to LDAP path: 0x%x\n&quot;, 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&quot;QueryInterface of IMailboxStore failed!\n&quot;); 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&quot;DeleteMailbox failed!\n&quot;); 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&quot;Failed to Save DataSource!\n&quot;); pUser->Release; pMailbox->Release; pDataSource->Release; pUser = NULL; pMailbox = NULL; pDataSource = NULL; CoUninitialize; return hr; } CoUninitialize; return 0; }
 * 1) include &quot;stdio.h&quot;
 * 1) import  no_namespace raw_interfaces_only
 * 2) import  no_namespace raw_interfaces_only rename(&quot;Folder&quot;,&quot;CDOEXFolder&quot;)
 * 3) import  no_namespace raw_interfaces_only

