Click here to Skip to main content
15,885,546 members
Articles / Desktop Programming / MFC
Tip/Trick

MAPI/Exhange Server Name

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
8 Dec 2011CPOL 23.4K  
How to get the MAPI server nane

The following function returns the Microsoft Exchange server name of a MAPI profile:


C++
#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;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer NXP Semiconductors
Romania Romania
My professional background includes knowledge of analyst programmer for Microsoft Visual C++, Microsoft Visual C#, Microsoft Visual Basic, Sun Java, assembly for Intel 80x86 microprocessors, assembly for PIC microcontrollers (produced by Microchip Inc.), relational databases (MySQL, Oracle, SQL Server), concurrent version systems, bug tracking systems, web design (HTML5, CSS3, XML, PHP/MySQL, JavaScript).

Comments and Discussions

 
-- There are no messages in this forum --