MAPI Application: How to programmatically enable Cached Exchange Mode in existing Outlook profiles No ratings yet.

0

Note: You need to use the Outlook MAPI header files with this code.

 

The exe uses two switches –A for processing all the profiles and –N followed by a profile name for processing a specific profile.

 

   1: // MAPISetCachedMode.cpp : Defines the entry point for the console application.

   2: //

   3:  

   4: #include "stdafx.h" 

   5: #include <stdio.h>      

   6: #include <iostream>

   7: #include <InitGuid.h> 

   8: #define USES_IID_IMAPIProp 

   9: #include <wchar.h> 

  10: #include <winerror.h>

  11: #include <Mapix.h>

  12: #include <mapiutil.h>

  13: #include <windows.h>

  14: #include <mapidefs.h>

  15: #include <mapiguid.h>

  16:  

  17: #define pbGlobalProfileSectionGuid    "\x13\xDB\xB0\xC8\xAA\x05\x10\x1A\x9B\xB0\x00\xAA\x00\x2F\xC4\x5A"

  18: #define pidProfileMin 0x6600

  19: #define PR_PROFILE_CONFIG_FLAGS            PROP_TAG( PT_LONG, pidProfileMin+0x01)

  20: /*******************************************************************************

  21: UpdateProfile

  22:  

  23: This function will add an additional Exchange mailbox to an existing MAPI profile. It assumes that you have already initialized MAPI.

  24: Parameters:

  25:  lpszProfile The name of the profile you are going to modify.

  26: Output:

  27:  HRESULT hRes Returns S_OK if completed successfully, otherwise returns a MAPI error.

  28:  *******************************************************************************/

  29:  

  30: HRESULT UpdateProfile(LPWSTR lpszProfile)

  31:  {

  32:     HRESULT hRes = S_OK;                // Result code returned from MAPI calls.

  33:     LPPROFADMIN lpProfAdmin = NULL;        // Profile Admin pointer.

  34:     LPSERVICEADMIN lpSvcAdmin = NULL;    // Message Service Admin pointer.

  35:     LPPROVIDERADMIN lpProvAdmin = NULL; // Provider Admin pointer.

  36:     LPMAPITABLE lpMsgSvcTable = NULL;    // MAPI table pointer.

  37:     LPPROFSECT lpProfileSection = NULL;    // Profile Section Pointer.

  38:     LPSRowSet lpSvcRows = NULL;            // Row set pointer.

  39:     SRestriction sres;                    // Restriction structure (used in HrQueryAllRows).

  40:     SPropValue SvcProps;                // Property value structure used in restriction.

  41:     ULONG ulProps = 0;                    // Count of props.

  42:     ULONG cbNewBuffer = 0;                // Count of bytes for new buffer.

  43:  

  44:     // Enumeration for convenience.

  45:     enum {iDispName, iSvcName, iSvcUID, cptaSvc};

  46:  

  47:     // This structure tells HrQueryAllRows what columns we want returned.

  48:     SizedSPropTagArray(cptaSvc,sptCols) = { cptaSvc, PR_DISPLAY_NAME, PR_SERVICE_NAME, PR_SERVICE_UID };

  49:  

  50:     // This structure tells our GetProps call what properties to get from the global profile section.

  51:     SizedSPropTagArray(1, sptGlobal) = { 1, PR_STORE_PROVIDERS };

  52:  

  53:     // Get an IProfAdmin interface.

  54:     hRes = MAPIAdminProfiles(0, // Flags

  55:                             &lpProfAdmin); // Pointer to new IProfAdmin

  56:  

  57:     if (FAILED(hRes)) goto error_handler;

  58:         printf("Retrieved IProfAdmin interface.\n");

  59:  

  60:     // Get an IMsgServiceAdmin interface off of the IProfAdmin interface. 

  61:     hRes = lpProfAdmin->AdminServices(    lpszProfile,// Profile that we want to modify.

  62:                                         LPWSTR(""),            // Password for that profile.

  63:                                         NULL,                // Handle to parent window.

  64:                                         0,                    // Flags.

  65:                                         &lpSvcAdmin);        // Pointer to new IMsgServiceAdmin.

  66:  

  67:     if (FAILED(hRes)) goto error_handler;

  68:         printf("Retrieved IMsgServiceAdmin interface.\n");

  69:  

  70:     // We now need to get the entry id for the Exchange service.

  71:     // First, we get the Message service table.

  72:     hRes = lpSvcAdmin->GetMsgServiceTable(    0, // Flags        

  73:                                             &lpMsgSvcTable); // Pointer to table

  74:  

  75:     if (FAILED(hRes)) goto error_handler;

  76:         printf("Retrieved message service table from profile.\n");

  77:  

  78:     // Set up restriction to query table.

  79:     sres.rt = RES_CONTENT;

  80:     sres.res.resContent.ulFuzzyLevel = FL_FULLSTRING;

  81:     sres.res.resContent.ulPropTag = PR_SERVICE_NAME_A;

  82:     sres.res.resContent.lpProp = &SvcProps;

  83:     SvcProps.ulPropTag = PR_SERVICE_NAME_A;

  84:     SvcProps.Value.lpszA = "MSEMS";

  85:  

  86:     // Query the table to get the entry for the Exchange message service.

  87:     hRes = HrQueryAllRows(    lpMsgSvcTable,

  88:                             (LPSPropTagArray)&sptCols,

  89:                             &sres,

  90:                             NULL,

  91:                             0,

  92:                             &lpSvcRows);

  93:  

  94:     if (FAILED(hRes)) goto error_handler;

  95:         printf("Queried table for Exchange message service.\n");

  96:  

  97:     if (lpSvcRows->cRows>0)

  98:     {

  99:         // Get a provider to the ProviderAdmin interface

 100:         hRes = lpSvcAdmin->AdminProviders(    (LPMAPIUID)lpSvcRows->aRow->lpProps[iSvcUID].Value.bin.lpb,

 101:                                             0,

 102:                                             &lpProvAdmin);

 103:  

 104:         if (FAILED(hRes)) goto error_handler;

 105:             printf("Retrieved IProviderAdmin interface\n");

 106:  

 107:         LPMAPIPROP pMAPIProp=NULL; 

 108:  

 109:         // Open the Global Profile Section

 110:         hRes = lpProvAdmin->OpenProfileSection(    (LPMAPIUID)pbGlobalProfileSectionGuid,

 111:                                                  NULL,

 112:                                                  MAPI_MODIFY,

 113:                                                  &lpProfileSection);

 114:  

 115:         if (hRes==S_OK) 

 116:         { 

 117:             hRes=lpProfileSection->QueryInterface(IID_IMAPIProp, (void**)&pMAPIProp); 

 118:  

 119:             // bind to the PR_PROFILE_CONFIG_FLAGS property

 120:             LPSPropValue profileConfigFlags = NULL;

 121:             hRes=HrGetOneProp(pMAPIProp,PR_PROFILE_CONFIG_FLAGS, &profileConfigFlags);

 122:         

 123:             if (hRes==S_OK) 

 124:             { 

 125:                     // Set the updated value for enabling cached mode 

 126:                     profileConfigFlags[0].Value.i = 6532;

 127:                     hRes=lpProfileSection->SetProps(1,profileConfigFlags,NULL); 

 128:                     hRes = lpProfileSection->SaveChanges(0); 

 129:             } 

 130:             if (profileConfigFlags) MAPIFreeBuffer(profileConfigFlags);

 131:         }

 132:  

 133:         if (pMAPIProp) MAPIFreeBuffer(pMAPIProp);

 134:  

 135:         if (FAILED(hRes)) goto error_handler;

 136:             printf("Finished processing changes\n");

 137:     }

 138:     goto cleanup;

 139:  

 140: error_handler:

 141:     printf("ERROR: hRes = %0x\n", hRes);

 142:  

 143: cleanup:

 144:     // Clean up.

 145:  

 146:     if (lpSvcRows) FreeProws(lpSvcRows);

 147:     if (lpMsgSvcTable) lpMsgSvcTable->Release();

 148:     if (lpSvcAdmin) lpSvcAdmin->Release();

 149:     if (lpProfAdmin) lpProfAdmin->Release();

 150:     if (lpProvAdmin) lpProvAdmin->Release();

 151:     if (lpProfileSection) lpProfileSection->Release();

 152:  

 153:     printf("Done cleaning up.\n");

 154:     printf("=============================================\n");

 155:     return hRes;

 156:  }

 157:  

 158:   HRESULT ProcessProfiles() 

 159:   { 

 160:       // Process each MAPI profile in turn  

 161:       HRESULT hRes=S_OK; 

 162:       LPPROFADMIN lpProfAdmin = NULL; 

 163:       // Profile Admin pointer 

 164:       LPSERVICEADMIN lpSvcAdmin = NULL; 

 165:       // Message Service Admin pointer 

 166:       LPMAPITABLE lpProfiles=NULL;  

 167:       // Retrieve profile table 

 168:       hRes = MAPIAdminProfiles(0, &lpProfAdmin); 

 169:       if (!lpProfAdmin)  

 170:       { 

 171:           printf("Failed to retrieve profile interface.\n"); 

 172:           return hRes; 

 173:       }  

 174:       // Get profile table 

 175:       hRes=lpProfAdmin->GetProfileTable(0, &lpProfiles); 

 176:       if (!lpProfiles) 

 177:       { 

 178:           printf("Failed to read profiles.\n"); 

 179:           return hRes; 

 180:       }  

 181:       // Now go through the profile table and process each one 

 182:       SizedSPropTagArray(1,profcols) = {1, { PR_DISPLAY_NAME } }; 

 183:       LPSRowSet prows=NULL; 

 184:       ULONG ulRows=0; 

 185:       hRes=lpProfiles->GetRowCount(0, &ulRows); 

 186:       hRes=lpProfiles->QueryRows(ulRows,0,&prows); 

 187:       if (hRes==S_OK) 

 188:       { 

 189:           for (unsigned int i=0; i<prows->cRows; i++) 

 190:           { 

 191:               printf("Processing profile: ");

 192:               printf(prows->aRow[i].lpProps->Value.lpszA);

 193:               printf("\n");

 194:               UpdateProfile(prows->aRow[i].lpProps->Value.lpszW);

 195:           } 

 196:           FreeProws(prows); 

 197:       }  

 198:       lpProfiles->Release(); 

 199:       lpProfAdmin->Release();  

 200:       return hRes; 

 201:   }   

 202:  

 203: int _tmain(int argc, _TCHAR* argv[])

 204: {

 205:     if (3 <= argc)

 206:     {

 207:         LPSTR arg1 = new CHAR[lstrlenW(argv[1])+1];

 208:         WideCharToMultiByte(CP_ACP, 0, argv[1], -1, arg1, lstrlenW(argv[1])+1, 0, 0);

 209:  

 210:         LPSTR arg2 = new CHAR[lstrlenW(argv[2])+1];

 211:         WideCharToMultiByte(CP_ACP, 0, argv[2], -1, arg2, lstrlenW(argv[2])+1, 0, 0);

 212:  

 213:         HRESULT hRes = S_OK;

 214:         

 215:         if ((strncmp(arg1,"-A", strlen("-A")) == 0) || (strncmp(arg1,"-a", strlen("-a")) == 0) )

 216:         {

 217:             printf("Processing all profiles.\n");

 218:             MAPIInitialize(NULL);

 219:             ProcessProfiles();

 220:             MAPIUninitialize();

 221:         }

 222:         else

 223:         {

 224:             if ((strncmp(arg1,"-N", strlen("-N")) == 0) || (strncmp(arg1,"-n", strlen("-n")) == 0) )

 225:             {

 226:                 printf("Processing one profile.\n");

 227:                 MAPIInitialize(NULL);

 228:                 UpdateProfile((LPWSTR)arg2);

 229:                 MAPIUninitialize();

 230:             }

 231:             else

 232:             {

 233:                 printf("Invalid option. Please use \"-A\" to process all the profiles or \"-N\" followed by the profile name.\n");

 234:                 printf("For example MAPISetCachedMode.exe -A or MAPISetCachedMode.exe -N \"Outlook\"");

 235:             }

 236:         }

 237:     }

 238:     else

 239:     {

 240:         if (2 <= argc)

 241:         {

 242:             LPSTR arg1 = new CHAR[lstrlenW(argv[1])+1];

 243:             WideCharToMultiByte(CP_ACP, 0, argv[1], -1, arg1, lstrlenW(argv[1])+1, 0, 0);

 244:             if ((strncmp(arg1,"-A", strlen("-A")) == 0) || (strncmp(arg1,"-a", strlen("-a")) == 0) )

 245:             {

 246:                 printf("Processing all profiles.\n");

 247:                 MAPIInitialize(NULL);

 248:                 ProcessProfiles();

 249:                 MAPIUninitialize();

 250:             }

 251:             else

 252:             {

 253:                 if ((strncmp(arg1,"-N", strlen("-N")) == 0) || (strncmp(arg1,"-n", strlen("-n")) == 0) )

 254:                 {

 255:                     printf("No profile name specified. Please use \"-A\" to process all the profiles or \"-N\" followed by the profile name.\n");

 256:                     printf("For example MAPISetCachedMode.exe -N \"Outlook\"");

 257:                 }

 258:                 else

 259:                 {

 260:                     printf("Invalid option. Please use \"-A\" to process all the profiles or \"-N\" followed by the profile name.\n");

 261:                     printf("For example MAPISetCachedMode.exe -A or MAPISetCachedMode.exe -N \"Outlook\"");

 262:                 }

 263:             }

 264:         }

 265:         else

 266:         {

 267:             printf("No option specified. Please use \"-A\" to process all the profiles or \"-N\" followed by the profile name.\n");

 268:             printf("For example MAPISetCachedMode.exe -A or MAPISetCachedMode.exe -N \"Outlook\"");

 269:         }

 270:     }

 271:     return 0;

 272:  }

0

Please rate this

Leave a Reply

Your email address will not be published. Required fields are marked *