The following function returns the Microsoft Exchange server name of a MAPI profile:
#include <objbase.h>
#include <mapix.h>
#include <mapidefs.h>
#include <mapiguid.h>
#include <mapiutil.h>
#pragma comment (lib, "mapi32.lib")
#define pidExchangeXmitReservedMin 0x3FE0
#define pidExchangeNonXmitReservedMin 0x65E0
#define pidProfileMin 0x6600
#define pidStoreMin 0x6618
#define pidFolderMin 0x6638
#define pidMessageReadOnlyMin 0x6640
#define pidMessageWriteableMin 0x6658
#define pidAttachReadOnlyMin 0x666C
#define pidSpecialMin 0x6670
#define pidAdminMin 0x6690
#define pidSecureProfileMin PROP_ID_SECURE_MIN
#define pbGlobalProfileSectionGuid "\x13\xDB\xB0\xC8\xAA\x05\x10\x1A\x9B\xB0\x00\xAA\x00\x2F\xC4\x5A"
#define PR_PROFILE_HOME_SERVER PROP_TAG(PT_UNICODE, pidProfileMin+0x02)
#define PR_PROFILE_HOME_SERVER_DN PROP_TAG(PT_UNICODE, pidProfileMin+0x12)
#define PR_PROFILE_HOME_SERVER_ADDRS PROP_TAG(PT_MV_UNICODE, pidProfileMin+0x13)
HRESULT GetMapiServerName(CString& strServerName, CString strProfileName)
{
HRESULT hResult = S_OK; LPPROFADMIN pAdminProfiles = NULL; LPSERVICEADMIN pSvcAdmin = NULL; LPPROFSECT pGlobalProfSect = NULL; LPSPropValue pProps = NULL;
OutputDebugString(_T("GetMapiServerName - MAPIInitialize\n"));
if (FAILED(hResult = MAPIInitialize(NULL)))
return hResult;
OutputDebugString(_T("GetMapiServerName - MAPIAdminProfiles\n"));
if (FAILED(hResult = MAPIAdminProfiles(0L, &pAdminProfiles)))
goto CleanUp;
OutputDebugString(_T("GetMapiServerName - AdminServices\n"));
if (FAILED(hResult = pAdminProfiles->AdminServices(
(LPTSTR)(LPCTSTR)strProfileName,
NULL,
0L, MAPI_UNICODE,
&pSvcAdmin)))
goto CleanUp;
OutputDebugString(_T("GetMapiServerName - OpenProfileSection\n"));
if (FAILED(hResult = pSvcAdmin->OpenProfileSection(
(LPMAPIUID)pbGlobalProfileSectionGuid,
NULL,
0L,
&pGlobalProfSect)))
goto CleanUp;
OutputDebugString(_T("GetMapiServerName - HrGetOneProp\n"));
if (FAILED(hResult = HrGetOneProp(pGlobalProfSect,
PR_PROFILE_HOME_SERVER,
&pProps)))
goto CleanUp;
strServerName = pProps->Value.lpszW;
OutputDebugString(_T("GetMapiServerName - ") + strServerName + _T("\n"));
CleanUp:
if (NULL != pAdminProfiles)
pAdminProfiles->Release();
if (NULL != pSvcAdmin)
pSvcAdmin->Release();
if (NULL != pGlobalProfSect)
pGlobalProfSect->Release();
if (NULL != pProps)
MAPIFreeBuffer(&pProps);
pSvcAdmin = NULL;
pGlobalProfSect = NULL;
pProps = NULL;
pAdminProfiles = NULL;
MAPIUninitialize();
return hResult;
}