Article ID: 170225
Article Last Modified on 8/18/2005
#include "windows.h"
#include "edk.h"
#include "stdio.h"
#include "d:\customer\makeprofile\makeprofile\makeprofile.h"
char g_lpszProfile[256];
STDMETHODIMP ChangeMSESPSettings ( LPSTR lpszProfile, HWND hWnd )
{
HRESULT hRes = S_OK;
LPPROFADMIN pProfAdmin = NULL;
LPSERVICEADMIN pSvcAdmin = NULL;
LPMAPITABLE pMsgSvcTable = NULL;
LPSRowSet pRows = NULL;
SPropValue rgval[2];
LPMAPISESSION pSession = NULL;
SRestriction sres;
SPropValue pSvcProps;
enum {iSvcName, iSvcUID, cptaSvc};
SizedSPropTagArray (cptaSvc, sptCols) = { cptaSvc,
PR_SERVICE_NAME,
PR_SERVICE_UID };
// Copy the profile name into a global variable.
strcpy ( g_lpszProfile, lpszProfile );
for ( int i = 0; i < 2; i++)
ZeroMemory ( &rgval[i], sizeof ( SPropValue ) );
// Get IProfAdmin interface pointer.
if ( FAILED ( hRes = MAPIAdminProfiles ( 0L, &pProfAdmin ) ) )
goto Quit;
// Create a new profile -- IProfAdmin::CreateProfile()
// If the attempt to create the profile fails, remove the
// profile.
if ( FAILED ( hRes = pProfAdmin -> CreateProfile ( lpszProfile,
"",
0L,
0L ) ) )
{
hRes = pProfAdmin -> DeleteProfile ( lpszProfile, 0L );
goto Quit;
}
printf("CreateProfile worked\n");
// Get IMsgServiceAdmin interface pointer.
if ( FAILED ( hRes = GetAdminService ( &pSvcAdmin, 0L ) ) )
goto Quit;
// Add a message service to the newly created profile.
if (FAILED(hRes = pSvcAdmin->CreateMsgService ( "MSEMS",
"Microsoft Exchange",
(ULONG)hWnd,
0L ) ) )
goto Quit;
// Get the message service table of the Message service.
if ( FAILED ( hRes = pSvcAdmin->GetMsgServiceTable(0L,
&pMsgSvcTable) ) )
goto Quit;
// Get the row from the message service table that represents the
// Microsoft Exchange Server service provider.
sres.rt = RES_CONTENT;
sres.res.resContent.ulFuzzyLevel = FL_FULLSTRING;
sres.res.resContent.ulPropTag = PR_SERVICE_NAME;
sres.res.resContent.lpProp = &pSvcProps;
pSvcProps.ulPropTag = PR_SERVICE_NAME;
pSvcProps.Value.lpszA = "MSEMS";
if ( FAILED ( hRes = HrQueryAllRows ( pMsgSvcTable,
(LPSPropTagArray)&sptCols,
&sres,
NULL,
0,
&pRows ) ) )
goto Quit;
// Set the values for PR_PROFILE_UNRESOLVED_NAME and
// PR_PROFILE_UNRESOLVED_SERVER
rgval[0].ulPropTag = PR_PROFILE_UNRESOLVED_NAME;
// rgval[0].Value.lpszA = <mailbox name> ;
rgval[0].Value.lpszA = "Karenwo";
rgval[1].ulPropTag = PR_PROFILE_UNRESOLVED_SERVER;
// rgval[1].Value.lpszA = <Exchange Server Name>;
rgval[1].Value.lpszA = "Karenwo";
if ( FAILED ( hRes = pSvcAdmin -> ConfigureMsgService(
(LPMAPIUID) pRows -> aRow->lpProps[iSvcUID].Value.bin.lpb,
0, NULL, 2, rgval ) ) )
hRes = pProfAdmin -> DeleteProfile ( lpszProfile, 0L );
// Logon to a new MAPI session using new profile -- MAPILogonEx()
if ( SUCCEEDED ( hRes = MAPILogonEx ( ( ULONG )hWnd,
lpszProfile,
"",
MAPI_NEW_SESSION |
MAPI_NO_MAIL, &pSession ) ) )
{
// Release and reuse IMsgService and IMAPITable
// objects.
pSvcAdmin -> Release ( );
pMsgSvcTable -> Release ( );
FreeProws ( pRows );
pSvcAdmin = NULL;
pMsgSvcTable = NULL;
pRows = NULL;
// Get new IMsgServiceAdmin interface pointer.
hRes = pSession -> AdminServices ( 0L, &pSvcAdmin );
// Get the Message service table.
if(FAILED(hRes = pSvcAdmin->GetMsgServiceTable ( 0L,
&pMsgSvcTable ) ) )
goto Quit;
// Find the row that represents the MSEMS message service.
if ( FAILED ( hRes = HrQueryAllRows (pMsgSvcTable,
(LPSPropTagArray)&sptCols,
&sres,
NULL,
0,
&pRows ) ) )
goto Quit;
// Open the property sheet for the service.
if ( FAILED ( hRes = pSvcAdmin -> ConfigureMsgService(
(LPMAPIUID) pRows -> aRow -> lpProps[iSvcUID].Value.bin.lpb,
0,
SERVICE_UI_ALLOWED |
SERVICE_UI_ALWAYS,
5,
rgval) ) )
goto Quit;
return hRes;
}
Quit:
if ( hRes )
hRes = pProfAdmin -> DeleteProfile ( lpszProfile, 0L );
if ( pMsgSvcTable )
{
FreeProws( pRows );
pRows = NULL;
pMsgSvcTable -> Release ( );
pMsgSvcTable = NULL;
}
if ( pSvcAdmin )
{
pSvcAdmin -> Release ( );
pSvcAdmin = NULL;
}
if ( pSession )
{
pSession -> Logoff ( (ULONG)hWnd, MAPI_LOGOFF_SHARED, 0L );
pSession -> Release ( );
pSession = NULL;
}
return hRes;
}
STDMETHODIMP GetAdminService ( LPSERVICEADMIN * lppSvcAdmin,
LPMAPISESSION m_pSession)
{
HRESULT hRes = S_OK;
LPSERVICEADMIN lpSvcAdmin = NULL;
LPPROFADMIN pProfAdmin = NULL;
if(NULL!=m_pSession)
hRes = m_pSession -> AdminServices ( 0L, &lpSvcAdmin );
else
hRes = MAPIAdminProfiles(0L, &pProfAdmin);
if ( SUCCEEDED ( hRes ) )
hRes = pProfAdmin -> AdminServices( g_lpszProfile, "", 0L, 0L,
&lpSvcAdmin );
else
return hRes;
if ( SUCCEEDED ( hRes ) )
*lppSvcAdmin = lpSvcAdmin;
return hRes;
}
This code results in the messaging service property sheet for Microsoft
Exchange Server service provider being displayed with the server and
mailbox properties set for the profile.
Keywords: kbhowto kbmsg kbcode KB170225