How To Determine the Size of Exchange 2000 Server Mailbox with ADO in C++ (291368)



The information in this article applies to:

  • Microsoft Exchange 2000 Server
  • ActiveX Data Objects (ADO) 2.5

This article was previously published under Q291368

SUMMARY

This article provides a Microsoft Visual C++ (VC) code sample that demonstrates how to use ActiveX Data Objects (ADO) to determine the size of a user's mailbox.

MORE INFORMATION

To determine the size of the mailbox, follow these steps:
  1. With the Win32 Console Application AppWizard, create a new Simple project and name it GetFolderSize.
  2. Open GetFolderSize.cpp. Paste the following code after
    #include "stdafx.h"
    						
    and replace the main function.
    // You need to link with Activeds.lib and Adsiid.lib. 
    
    #include <activeds.h>
    #include <stdio.h>
    
    #define IMPORTOPTS no_namespace rename("EOF", "adoEOF")
    #import "c:\program files\common files\system\ado\msado15.dll" IMPORTOPTS
    
    HRESULT GetDomainName(BSTR * bstrDomainName);
    HRESULT GetMailboxSize(BSTR bstrDomainName, BSTR bstrMailboxname);
    
    struct StartOle {
        StartOle() { CoInitialize(NULL); }
        ~StartOle() { CoUninitialize(); }
    } _inst_StartOle;
    
    void main()
    {
       HRESULT hr = S_OK;
       BSTR    bstrDomainDNSName;
    
       hr = GetDomainName(&bstrDomainDNSName);
       // try to find out "Administrator" mailbox size
       hr = GetMailboxSize(bstrDomainDNSName, L"administrator");
    }
    
    
    HRESULT GetMailboxSize(BSTR bstrDomainName, BSTR bstrMailboxName)
    {
       HRESULT hr = S_OK;
       long   size = 0;
    
       _bstr_t szConnString = "file://./backofficestorage/" +
                (_bstr_t)bstrDomainName +
                "/MBX/" +
                bstrMailboxName;
       _bstr_t szSQL = "Select ";
       szSQL = szSQL +
         "\"http://schemas.microsoft.com/exchange/foldersize\"";
       szSQL = szSQL + ", \"DAV:displayname\"";
       szSQL = szSQL + " from scope ('shallow traversal of ";
       szSQL = szSQL + "\"" + szConnString + "\"" + "')";
       szSQL = szSQL + " WHERE \"DAV:isfolder\" = true";
         
       try {
          _ConnectionPtr   pConn(_uuidof(Connection));
          _RecordsetPtr   pRs(_uuidof(Recordset));
    
          pConn->Provider = "Exoledb.DataSource";
    
          hr = pConn->Open(szConnString, "", "", 0);
    
          if (pConn->State == adStateOpen)
             printf("Connection Opened\n");
          else
          {
             printf("Connection Failed\n");
             return hr;
          }
    
          hr = pRs->Open(szSQL, 
             pConn->ConnectionString,
             adOpenForwardOnly,
             adLockReadOnly,
             0);
    
          
          // Determine if any folders were found.
          if (pRs->RecordCount == 0)
          {
              printf("No object found\n");
              return hr;
          }
    
          // Move to the first folder.
          hr = pRs->MoveFirst();
    
          while (VARIANT_FALSE == pRs->adoEOF)
          {                     
           FieldsPtr Flds = pRs->GetFields(); 
           FieldPtr Fld = Flds->GetItem("DAV:displayname");
           printf("Folder Name: %s\n", (char *)(_bstr_t)(Fld->Value));
      
           Fld = Flds->GetItem(
                   "http://schemas.microsoft.com/exchange/foldersize");
           printf("Folder Size: %ld\n\n", (long)Fld->Value);
           size = size + (long)Fld->Value;
           pRs->MoveNext();
          }
    
          printf("Total Mailbox size: %ld\n\n", size);
          
          hr = pRs->Close();
          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;
       }
    }
    
      
    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;
    }
    					
  3. On the Project menu, click Settings, and then click the Link tab. In Object/Library Modules, add Activeds.lib and Adsiid.lib.
  4. Compile and build the project.

Modification Type:MinorLast Reviewed:7/1/2004
Keywords:kbhowto kbMsg KB291368