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

 

   1: // MAPIDeleteHotmailAccount.cpp : Defines the entry point for the console application.
   2: //
   3:  
   4: #include "stdafx.h"           
   5: //The following headers may be required if not already in the project      
   6: #include <stdio.h>      
   7: #include <iostream>
   8: #include <InitGuid.h> 
   9: #define USES_IID_IMAPIProp 
  10: #include <wchar.h> 
  11: #include <winerror.h>
  12: #include <Mapix.h>
  13: #include <mapiutil.h>
  14: #include <windows.h>
  15:  
  16:  
  17: /*******************************************************************************      
  18: UpdateProfile          
  19: This function will delete the MSN_CON service from all existing MAPI profiles. It assumes that you have already initialized MAPI.          
  20: Parameters      
  21: lpszProfile The name of the profile you are going to modify.             
  22: Output      
  23: HRESULT hRes Returns S_OK if completed successfully, otherwise returns a MAPI error.      
  24: *******************************************************************************/          
  25:  
  26: #define PidTagProviderDisplay 0x3006001E      
  27: #define PR_PROVIDER_DISPLAY PROP_TAG( PT_STRING8, PidTagProviderDisplay )     
  28: #define PR_PROVIDER_DISPLAY_A PROP_TAG( PT_STRING8, PidTagProviderDisplay )     
  29: #define PR_PROVIDER_DISPLAY_W PROP_TAG( PT_STRING8, PidTagProviderDisplay )   
  30:  
  31: HRESULT UpdateProfile(LPWSTR lpszProfile)     
  32: {              
  33:     HRESULT hRes = S_OK; // Result code returned from MAPI calls.          
  34:     LPPROFADMIN lpProfAdmin = NULL; // Profile Admin pointer.         
  35:     LPSERVICEADMIN lpSvcAdmin = NULL; // Message Service Admin pointer.        
  36:     LPPROVIDERADMIN lpProvAdmin = NULL; // Provider Admin pointer.         
  37:     LPMAPITABLE lpMsgSvcTable = NULL; // MAPI table pointer.        
  38:     LPPROFSECT lpProfileSection = NULL;// Profile Section Pointer.       
  39:     LPSRowSet lpSvcRows = NULL; // Row set pointer.        
  40:     LPSPropValue lpProp = NULL;        
  41:     SRestriction sres;        
  42:     SPropValue SvcProps;        
  43:     LPMAPIUID lpProviderUID = {0};      
  44:  
  45:     enum {iDispName, iSvcName, iSvcUID, iProvUID, cptaSvc};  
  46:  
  47:     // This structure tells HrQueryAllRows what columns we want returned.         
  48:     SizedSPropTagArray(cptaSvc,sptCols) = { cptaSvc,                                                 
  49:         PR_DISPLAY_NAME,                                               
  50:         PR_SERVICE_NAME,                                          
  51:         PR_SERVICE_UID,                                          
  52:         PR_PROVIDER_UID};              
  53:  
  54:     // This structure tells our GetProps call what properties to get from the global profile section.        
  55:     SizedSPropTagArray(1, sptGlobal) = { 1, PR_STORE_PROVIDERS };      
  56:  
  57:     // Get an IProfAdmin interface.        
  58:     hRes = MAPIAdminProfiles(    0, // Flags                                   
  59:         &lpProfAdmin); // Pointer to new IProfAdmin       
  60:  
  61:     if (FAILED(hRes)) goto error_handler;      
  62:     printf("Retrieved IProfAdmin interface.\n");     
  63:  
  64:     // Get an IMsgServiceAdmin interface off of the IProfAdmin interface.     
  65:     hRes = lpProfAdmin->AdminServices(  lpszProfile, // Profile that we want to modify.     
  66:         LPWSTR(""), // Password for that profile.                                        
  67:         NULL, // Handle to parent window.                                       
  68:         0, // Flags.                                      
  69:         &lpSvcAdmin); // Pointer to new IMsgServiceAdmin.      
  70:  
  71:     if (FAILED(hRes)) goto error_handler;     
  72:     printf("Retrieved IMsgServiceAdmin interface.\n");     
  73:  
  74:     // We now need to get the entry id for the Exchange service.    
  75:     // First, we get the Message service table.             
  76:     hRes = lpSvcAdmin->GetMsgServiceTable(  0, // Flags           
  77:         &lpMsgSvcTable); // Pointer to table      
  78:  
  79:     if (FAILED(hRes)) goto error_handler;        
  80:     printf("Retrieved message service table from profile.\n");        
  81:     
  82:     // Set up restriction to query table.       
  83:     sres.rt = RES_CONTENT;       
  84:     sres.res.resContent.ulFuzzyLevel = FL_FULLSTRING;       
  85:     sres.res.resContent.ulPropTag = PR_SERVICE_NAME_A;    
  86:     sres.res.resContent.lpProp = &SvcProps;      
  87:     SvcProps.ulPropTag = PR_SERVICE_NAME_A;        
  88:     SvcProps.Value.lpszA = "MSN_CON";              
  89:     
  90:     // Query the table to get the entry for the Exchange message service.    
  91:     hRes = HrQueryAllRows(  lpMsgSvcTable,                        
  92:         (LPSPropTagArray)&sptCols,                              
  93:         &sres,                                
  94:         NULL,                               
  95:         0,                             
  96:         &lpSvcRows);      
  97:     
  98:     if (FAILED(hRes)) goto error_handler;     
  99:     printf("Queried table for Exchange message service.\n");           
 100:     
 101:     if (lpSvcRows->cRows>0)        
 102:     {             
 103:         for (unsigned int i=0; i<lpSvcRows->cRows; i++) 
 104:         { 
 105:             hRes = lpSvcAdmin->DeleteMsgService((LPMAPIUID)lpSvcRows->aRow[i].lpProps[iSvcUID].Value.bin.lpb);      
 106:             if (FAILED(hRes)) goto error_handler;     
 107:             printf("Service %d deleted\n", i);  
 108:         }
 109:          
 110:     }              
 111:     else
 112:     {
 113:         printf("No Hotmail Service found\n"); 
 114:     }
 115:  
 116:     goto cleanup;
 117:     
 118: error_handler:      
 119:         printf("ERROR     hRes = %0x\n", hRes);   
 120:  
 121: // Clean up.      
 122: cleanup:        
 123:     if (lpSvcRows) FreeProws(lpSvcRows);     
 124:     if (lpMsgSvcTable) lpMsgSvcTable->Release();  
 125:     if (lpSvcAdmin) lpSvcAdmin->Release();     
 126:     if (lpProfAdmin) lpProfAdmin->Release();    
 127:     if (lpProvAdmin) lpProvAdmin->Release();       
 128:     if (lpProfileSection) lpProfileSection->Release();    
 129:     printf("Done processing.\n");
 130:     printf("=============================================\n");
 131:     return hRes;      
 132: }         
 133:  
 134: HRESULT ProcessProfiles() 
 135:   { 
 136:       // Process each MAPI profile in turn  
 137:       HRESULT hRes=S_OK; 
 138:       LPPROFADMIN lpProfAdmin = NULL; 
 139:       // Profile Admin pointer 
 140:       LPSERVICEADMIN lpSvcAdmin = NULL; 
 141:       // Message Service Admin pointer 
 142:       LPMAPITABLE lpProfiles=NULL;  
 143:       // Retrieve profile table 
 144:       hRes = MAPIAdminProfiles(0, &lpProfAdmin); 
 145:       if (!lpProfAdmin)  
 146:       { 
 147:           printf("Failed to retrieve profile interface.\n"); 
 148:           return hRes; 
 149:       }  
 150:       // Get profile table 
 151:       hRes=lpProfAdmin->GetProfileTable(0, &lpProfiles); 
 152:       if (!lpProfiles) 
 153:       { 
 154:           printf("Failed to read profiles.\n"); 
 155:           return hRes; 
 156:       }  
 157:       // Now go through the profile table and process each one 
 158:       SizedSPropTagArray(1,profcols) = {1, { PR_DISPLAY_NAME } }; 
 159:       LPSRowSet prows=NULL; 
 160:       ULONG ulRows=0; 
 161:       hRes=lpProfiles->GetRowCount(0, &ulRows); 
 162:       hRes=lpProfiles->QueryRows(ulRows,0,&prows); 
 163:       if (hRes==S_OK) 
 164:       { 
 165:           for (unsigned int i=0; i<prows->cRows; i++) 
 166:           { 
 167:               printf("Processing profile: ");
 168:               printf(prows->aRow[i].lpProps->Value.lpszA);
 169:               printf("\n");
 170:               UpdateProfile(prows->aRow[i].lpProps->Value.lpszW);
 171:           } 
 172:           FreeProws(prows); 
 173:       }  
 174:       lpProfiles->Release(); 
 175:       lpProfAdmin->Release();  
 176:       return hRes; 
 177:   }   
 178: int _tmain(int argc, _TCHAR* argv[])     
 179: {         
 180:     HRESULT hRes = S_OK;     
 181:                 printf("Processing all profiles.\n");
 182:             MAPIInitialize(NULL);
 183:             ProcessProfiles();
 184:             MAPIUninitialize();    
 185:       
 186:     return 0;    
 187: }