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

 

   1: // MAPICreateBlankProfile.cpp : Defines the entry point for the console application.
   2: //
   3:  
   4: #include "stdafx.h" 
   5: #include <iostream>
   6: #include <InitGuid.h> 
   7: #define USES_IID_IMAPIProp 
   8: #include <wchar.h> 
   9: #include <winerror.h>
  10: #include <Mapix.h>
  11: #include <mapiutil.h>
  12: #include <windows.h>
  13:  
  14: // CreateProfile function: This uses the MAPI IProfAdmin to 
  15: // programmatically create a profile. No UI is displayed.
  16:  
  17: HRESULT CreateProfile(LPSTR szProfile )
  18: {
  19:     HRESULT         hRes = S_OK;            // Result from MAPI calls.
  20:     LPPROFADMIN     lpProfAdmin = NULL;     // Profile Admin object.
  21:     LPSERVICEADMIN  lpSvcAdmin = NULL;      // Service Admin object.
  22:     LPMAPITABLE     lpMsgSvcTable = NULL;   // Table to hold services.
  23:     LPSRowSet       lpSvcRows = NULL;       // Rowset to hold results of table query.
  24:  
  25:     // This indicates columns we want returned from HrQueryAllRows.
  26:     enum {iSvcName, iSvcUID, cptaSvc};
  27:     SizedSPropTagArray(cptaSvc,sptCols) = { cptaSvc, PR_SERVICE_NAME, PR_SERVICE_UID };
  28:  
  29:     // Initialize MAPI.
  30:  
  31:     if (FAILED(hRes = MAPIInitialize(NULL)))
  32:     {
  33:         std::cout<<"Error initializing MAPI.";
  34:         goto error;
  35:     }
  36:  
  37:     // Get an IProfAdmin interface.
  38:  
  39:     if (FAILED(hRes = MAPIAdminProfiles(0,              // Flags.
  40:                                         &lpProfAdmin))) // Pointer to new IProfAdmin.
  41:     {
  42:         std::cout<<"Error getting IProfAdmin interface.";
  43:         goto error;
  44:     }
  45:  
  46:     // Create a new profile.
  47:  
  48:     if (FAILED(hRes = lpProfAdmin->CreateProfile((LPTSTR)szProfile,     // Name of new profile.
  49:                                                  NULL,          // Password for profile.
  50:                                                  NULL,          // Handle to parent window.
  51:                                                  NULL)))        // Flags.
  52:     {
  53:         std::cout<<"Error creating profile.";
  54:         goto error;
  55:     }
  56:  
  57:     // Get an IMsgServiceAdmin interface off of the IProfAdmin interface.
  58:  
  59:     if (FAILED(hRes = lpProfAdmin->AdminServices((LPWSTR)szProfile,     // Profile that we want to modify.
  60:                                                  (LPWSTR)"",          // Password for that profile.
  61:                                                  NULL,          // Handle to parent window.
  62:                                                  0,             // Flags.
  63:                                                  &lpSvcAdmin))) // Pointer to new IMsgServiceAdmin.
  64:     {
  65:         std::cout<<"Error getting IMsgServiceAdmin interface.";
  66:         goto error;
  67:     }
  68:  
  69:     // Create the new message service for Exchange.
  70:     if (FAILED(hRes = lpSvcAdmin->CreateMsgService((LPTSTR)"MSEMS",     // Name of service from MAPISVC.INF.
  71:                                                    NULL,        // Display name of service.
  72:                                                    (ULONG)NULL,        // Handle to parent window.
  73:                                                    0L)))      // Flags.
  74:     {
  75:         std::cout<<"Error creating Exchange message service.";
  76:         goto error;
  77:     }
  78:  
  79:     goto cleanup;
  80:  
  81: error:
  82:     std::cout<<" hRes = "<<std::hex<<hRes<<std::dec<<std::endl;
  83:     return hRes;
  84:  
  85: cleanup:
  86:     // Clean up.
  87:     if (lpSvcRows) FreeProws(lpSvcRows);
  88:     if (lpMsgSvcTable) lpMsgSvcTable->Release();
  89:     if (lpSvcAdmin) lpSvcAdmin->Release();
  90:     if (lpProfAdmin) lpProfAdmin->Release();
  91:  
  92:     MAPIUninitialize();
  93:     return hRes;
  94:  
  95: }
  96:                 
  97:  
  98: int _tmain(int argc, _TCHAR* argv[])
  99: {
 100:     LPSTR arg1 = new CHAR[lstrlenW(argv[1])+1];
 101:     WideCharToMultiByte(CP_ACP, 0, argv[1], -1, arg1, lstrlenW(argv[1])+1, 0, 0);
 102:     CreateProfile(arg1);
 103:     return 0;
 104: }
 105: