MAPI C++ Enumerate Profiles

A windows computer will in most probability be installed with some profiles for connecting to the Microsoft Exchange Servers. This profile will contain details of the Microsoft Exchange Server name, e-mail address etc.,

This article MAPI C++ Enumerating Profiles explains how to extract the profiles installed in the local computer. This needs the API MAPIAdminProfiles to get the pointer to the MAPI Profile of type PROFADMIN Manager installed in the system. This interface will be used to call GetProfileTable function, which in turn returns the profile names installed in the system.

MAPI C++ Enumerating Profiles – Sample Code:


//EnumProfiles.cpp
#include <edk.h>
#include <string.h>

#define MAX_SIZE 256

void main()
{
	HRESULT hr; 
	LPMAPITABLE pTable = NULL; 
	LPSRowSet pRows = NULL; 
	bool found=false; 
	SizedSPropTagArray(1, Columns) ={1, {PR_DISPLAY_NAME}}; 
	LPPROFADMIN pProfAdmin = NULL; 
	// Pointer to IProfAdmin object 
	//Initialize the MAPI C++ libraries 
	hr = MAPIInitialize (NULL); 
	char l_ProfileName[MAX_SIZE]; 
	//Get the pointer to MAPI C++ PROFADMIN interface 
	hr = MAPIAdminProfiles(0, &pProfAdmin); 
	if (!FAILED(hr)) 
	{ 
		// Get the ProfileTable which has all profiles 
		hr = pProfAdmin->GetProfileTable(0, &pTable); 
		if (FAILED(hr)) 
		{ 
			//Error initializing profile table 
		} 
	} 
	//Call the MAPI C++ HrQueryrows API for getting all the data inside the profile table 
	hr = HrQueryAllRows(pTable, 
		//pointer to table of pointers 
		(LPSPropTagArray) &Columns, 
		//list of columns we will get 
		NULL, 
		//filter NULL. We need all rows 
		NULL, 
		//we don't need any sorting here 
		0, 
		//retrieve all rows that match 
		&pRows); 
		//pointer to resulting table 
	if (!FAILED(hr) && pRows->cRows>0) 
	//result of HrQueryAllRows 
	{ 
		found=true; 
		for(int i=0;i<pRows->cRows;i++) 
		{ 
			strcpy(l_ProfileName, pRows->aRow[i].lpProps->Value.lpszA); 
			printf("%sn",l_ProfileName); 
		} 
	} 
	//Cleanup of all MAPI C++ Interfaces 
	if (pRows) 
		FreeProws(pRows); 
	if (pTable)
		pTable->Release(); 
	if (pProfAdmin) 
		pProfAdmin->Release(); 
	MAPIUninitialize();
}

The above program is a console application. This prints all the profiles present in the machine.

C++ MAPI Public Folder – Libraries Required:

Add these libraries in the project settings–> Link –> Object/library modules.
kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib Edkguid.lib msvcrt.lib Edkdebug.lib mapi32.lib

Also in the Project Settings –> Link –> Category –> Input –>Ignore Libraries , add libc.lib and libcd.lib.

When the program is compiled, if the ” LINK : fatal error LNK1104: cannot open the file “mfc40.lib” ” is seen, then this problem has to be resolved by Recompiling the MAPI Exchange Development Kit.