Click here to Skip to main content
Click here to Skip to main content
Go to top

MAPI/Exhange Server Name

, 8 Dec 2011
Rate this:
Please Sign up or sign in to vote.
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;
}

License

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

Share

About the Author

Mihai MOGA
Architect Printec Group Romania SRL
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 (XHTML, CSS, XML, PHP/MySQL, JavaScript, Flash).
 
If you like my articles, please visit my website for more: http://www.mihaimoga.com/
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 8 Dec 2011
Article Copyright 2011 by Mihai MOGA
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid