// 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;
}