Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

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


View products that this article applies to.

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.

↑ Back to the top


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.

↑ Back to the top


Article Info
Article ID : 291368
Revision : 7
Created on : 1/1/0001
Published on : 1/1/0001
Exists online : False
Views : 298