65.9K
CodeProject is changing. Read more.
Home

MAPI/Exhange Server Name

starIconstarIconstarIconstarIconstarIcon

5.00/5 (4 votes)

Dec 8, 2011

CPOL
viewsIcon

23551

How to get the MAPI server nane

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;   // HRESULT returned by this method
	LPPROFADMIN pAdminProfiles = NULL; // Pointer to IProfAdmin object
	LPSERVICEADMIN pSvcAdmin = NULL;  // Pointer to IServiceAdmin object
	LPPROFSECT pGlobalProfSect = NULL; // Pointer to IProfSect object
	LPSPropValue pProps = NULL; // Pointer to PropValue

	OutputDebugString(_T("GetMapiServerName - MAPIInitialize\n"));
	if (FAILED(hResult = MAPIInitialize(NULL)))
		return hResult;

	// Get a Profile admin object
	OutputDebugString(_T("GetMapiServerName - MAPIAdminProfiles\n"));
	if (FAILED(hResult = MAPIAdminProfiles(0L, &pAdminProfiles)))
		goto CleanUp;

	// Get a ServiceAdmin object
	OutputDebugString(_T("GetMapiServerName - AdminServices\n"));
	if (FAILED(hResult = pAdminProfiles->AdminServices(
		(LPTSTR)(LPCTSTR)strProfileName,
		NULL,
		0L,  // Your app's window handle
		MAPI_UNICODE,
		&pSvcAdmin)))
		goto CleanUp;

	// Get the Global Profile Section by calling
	// IServiceAdmin::OpenProfileSection use pbGlobalProfileSectionGuid
	// defined in EDKMDB.H as the entry ID to request
	// The default return is an IProfSect interface.
	OutputDebugString(_T("GetMapiServerName - OpenProfileSection\n"));
	if (FAILED(hResult = pSvcAdmin->OpenProfileSection(
		(LPMAPIUID)pbGlobalProfileSectionGuid,
		NULL,
		0L,
		&pGlobalProfSect)))
		goto CleanUp;

	// Call HrGetOneProp to get PR_PROFILE_HOME_SERVER
	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"));

	// Free all memory allocated by any MAPI calls
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;
}