// Set address list search order to Custom
  int Setcustomization(IMAPISession &lpSession) {
  HRESULT hr;
  LPPROFSECT lpProfileSection = NULL;
  LPSPropValue lpPropValue = NULL;
  LONG FAR * ulPropCnt = NULL;
  LPSPropValue FAR * pProps = NULL;

  hr = lpSession.OpenProfileSection((LPMAPIUID)&IID_CAPONE_PROF, NULL, MAPI_MODIFY  , &lpProfileSection);
  if (FAILED (hr)) {
	  cerr << "Error: Could not open the CAPONE profile section" <<endl;
	  return 1;
  }

  //hr = HrGetOneProp(lpProfileSection, PR_AB_CHOOSE_DIRECTORY_AUTOMATICALLY, &lpPropValue);// good for select automatically
  hr = HrGetOneProp(lpProfileSection, PR_AB_SEARCH_PATH_CUSTOMIZATION, &lpPropValue);
  if (FAILED (hr)) {
	  lpProfileSection->Release();
	  cerr << "Error: Could not open the property of the address book to set address list search order to Custom" <<endl;
	  return 2;
  }

  //cout << "Server DN: %d\n", lpPropValue->Value.b;
  lpPropValue->Value.l = SEARCHPATHREORDERTYPE_RAW;
  hr = HrSetOneProp(lpProfileSection, lpPropValue);
  if (FAILED (hr)) {
	  MAPIFreeBuffer(lpPropValue);
	  cerr << "Error: Could not set the property of the address list search order to Custom" <<endl;
	  return 3; // can't get the prop
  }

  cout << "Configured address list search order to be Custom" <<endl;
  return S_OK;
}
// Set default address list by name
STDMETHODIMP SetDefaultAddressList(IMAPISession &Session, const string &AddressList) {
   HRESULT hr = S_OK;

   // Initialize memory allocation
   LPVOID pAllocLink = NULL;
   MAPIAllocateBuffer(0, &pAllocLink);

   // Resolve address list name to ENTRYID
   ULONG cbEntryID;
   LPENTRYID lpEntryID;
   hr = ResolveAddressList(Session, AddressList, pAllocLink, &cbEntryID, &lpEntryID);
   if (FAILED(hr)) {
      cerr << "Unable to resolve address list name '" << AddressList << "'." << endl;
      return hr;
   }

   // Open address book
   LPADRBOOK lpAddrBook;
   hr = Session.OpenAddressBook(NULL, NULL, NULL, &lpAddrBook);
   if (FAILED(hr)) {
      cerr << "Error getting MAPI Address book." << endl;
      goto Exit;
   }
   // Start Modified by CL
   TraceDefaultDir(*lpAddrBook);

   // Fix IID_CAPONE_PROF See note here: (http://msdn.microsoft.com/en-us/library/office/cc839797)
   // Turning off the PR_AB_CHOOSE_DIRECTORY_AUTOMATICALLY property
   // Set default address list

   cout << "Resetting choose automatically property" <<endl;

   LPPROFSECT lpProfileSection;
   hr = Session.OpenProfileSection((LPMAPIUID)&IID_CAPONE_PROF, NULL, MAPI_MODIFY  , &lpProfileSection);
   if (FAILED(hr)) {
	   cerr << "Error opening profile section" << endl;
	   goto Exit;
   }


   SPropValue lpPropValue;
   lpPropValue.ulPropTag = PR_AB_CHOOSE_DIRECTORY_AUTOMATICALLY;
   lpPropValue.Value.l = 0;

   hr = HrSetOneProp(lpProfileSection, &lpPropValue);
   if (FAILED(hr)) {
	   cerr << "Error setting property for automatic choosing to off with error" << hr << endl;
	   goto Exit;
   }

   //End modified by CL

   // Display feedback
   TraceDefaultDir(*lpAddrBook);
   cout << "Setting default address list: " << AddressList << endl;

   // Set default address list
   hr = lpAddrBook->SetDefaultDir(cbEntryID, lpEntryID);
   if (FAILED(hr)) {
      cerr << "Error setting default address list" << endl;
      goto Exit;
   }


   // Start modified by CL

   //TraceDefaultDir(*lpAddrBook);

   // End modified by CL

Exit:
   if (pAllocLink) MAPIFreeBuffer(pAllocLink);
   return hr;
}