HRESULT ExchangeAdmin::DeleteProfile(wstring strProfile) { HRESULT hr = S_OK; Zimbra::Util::ScopedBuffer<char> strProfName; WtoA((LPWSTR)strProfile.c_str(), strProfName.getref()); // delete profile if (FAILED(hr = m_pProfAdmin->DeleteProfile((LPTSTR)strProfName.get(), 0)) && (hr != MAPI_E_NOT_FOUND)) throw ExchangeAdminException(hr, L"DeleteProfile(): DeleteProfile Failed.", ERR_DELETE_PROFILE, __LINE__, __FILE__); return hr; }
HRESULT ExchangeAdmin::CreateProfile(wstring strProfileName, wstring strMailboxName, wstring strPassword) { HRESULT hr = S_OK; Zimbra::Util::ScopedBuffer<char> strServer; Zimbra::Util::ScopedBuffer<char> strMBName; Zimbra::Util::ScopedBuffer<char> strProfName; Zimbra::Util::ScopedBuffer<char> strProfPwd; Zimbra::Util::ScopedInterface<IMsgServiceAdmin> pSvcAdmin; Zimbra::Util::ScopedInterface<IMAPITable> pMsgSvcTable; Zimbra::Util::ScopedRowSet pSvcRows; SPropValue rgval[2] = { 0 }; SPropValue sProps = { 0 }; SRestriction sres; WCHAR errDescrption[256] = {}; // Columns to get from HrQueryAllRows. enum { iSvcName, iSvcUID, cptaSvc }; SizedSPropTagArray(cptaSvc, sptCols) = { cptaSvc, PR_SERVICE_NAME, PR_SERVICE_UID }; WtoA((LPWSTR)strProfileName.c_str(), strProfName.getref()); WtoA((LPWSTR)strPassword.c_str(), strProfPwd.getref()); // create new profile if (FAILED(hr = m_pProfAdmin->CreateProfile((LPTSTR)strProfName.get(), (LPTSTR)strProfPwd.get(), NULL, 0))) { throw ExchangeAdminException(hr, L"CreateProfile(): CreateProfile Failed.", ERR_CREATE_EXCHPROFILE, __LINE__, __FILE__); } // Get an IMsgServiceAdmin interface off of the IProfAdmin interface. if (FAILED(hr = m_pProfAdmin->AdminServices((LPTSTR)strProfName.get(), (LPTSTR)strProfPwd.get(), NULL, 0, pSvcAdmin.getptr()))) { wcscpy(errDescrption, L"CreateProfile(): AdminServices Failed."); goto CRT_PROFILE_EXIT; } // Create the new message service for Exchange. if (FAILED(hr = pSvcAdmin->CreateMsgService((LPTSTR)"MSEMS", (LPTSTR)"MSEMS", NULL, NULL))) { wcscpy(errDescrption, L"CreateProfile(): CreateMsgService Failed."); goto CRT_PROFILE_EXIT; } // Need to obtain the entry id for the new service. This can be done by getting the message service table // and getting the entry that corresponds to the new service. if (FAILED(hr = pSvcAdmin->GetMsgServiceTable(0, pMsgSvcTable.getptr()))) { wcscpy(errDescrption, L"CreateProfile(): GetMsgServiceTable Failed."); goto CRT_PROFILE_EXIT; } sres.rt = RES_CONTENT; sres.res.resContent.ulFuzzyLevel = FL_FULLSTRING; sres.res.resContent.ulPropTag = PR_SERVICE_NAME; sres.res.resContent.lpProp = &sProps; sProps.ulPropTag = PR_SERVICE_NAME; sProps.Value.lpszA = "MSEMS"; // Query the table to obtain the entry for the newly created message service. if (FAILED(hr = HrQueryAllRows(pMsgSvcTable.get(), (LPSPropTagArray) & sptCols, NULL, NULL, 0, pSvcRows.getptr()))) { wcscpy(errDescrption, L"CreateProfile(): HrQueryAllRows Failed."); goto CRT_PROFILE_EXIT; } // Set up a SPropValue array for the properties that you have to configure. if (pSvcRows->cRows > 0) { // First, the exchange server name. ZeroMemory(&rgval[0], sizeof (SPropValue)); rgval[0].ulPropTag = PR_PROFILE_UNRESOLVED_SERVER; WtoA((LPWSTR)m_strServer.c_str(), strServer.getref()); rgval[0].Value.lpszA = (LPSTR)strServer.get(); // Next, the user's AD name. ZeroMemory(&rgval[1], sizeof (SPropValue)); rgval[1].ulPropTag = PR_PROFILE_UNRESOLVED_NAME; WtoA((LPWSTR)strMailboxName.c_str(), strMBName.getref()); rgval[1].Value.lpszA = (LPSTR)strMBName.get(); // Configure the message service by using the previous properties. // int trials = 10; int trials = 2; int itrTrials = 0; hr = 0x81002746; // WSAECONNRESET while ((hr == 0x81002746) && (itrTrials < trials)) { hr = pSvcAdmin->ConfigureMsgService( (LPMAPIUID)pSvcRows->aRow->lpProps[iSvcUID].Value.bin.lpb, NULL, 0, 2, rgval); //if (hr == 0x81002746) // Sleep(30000); //Sleep(10000); itrTrials++; } if (FAILED(hr)) { /* = * pSvcAdmin->ConfigureMsgService((LPMAPIUID)pSvcRows->aRow->lpProps[iSvcUID]. * Value.bin.lpb,NULL, 0, 2, rgval)))*/ wcscpy(errDescrption, L"CreateProfile(): ConfigureMsgService Failed."); goto CRT_PROFILE_EXIT; } } CRT_PROFILE_EXIT: if (hr != S_OK) { DeleteProfile(strProfileName); throw ExchangeAdminException(hr, errDescrption, ERR_CREATE_EXCHPROFILE, __LINE__, __FILE__); } else { //Create supporting OL profile entries else crash may happen! if(!Zimbra::MAPI::Util::SetOLProfileRegistryEntries(strProfileName.c_str())) { throw ExchangeAdminException(hr, L"ExchangeAdmin::CreateProfile()::SetOLProfileRegistryEntries Failed.", ERR_CREATE_EXCHPROFILE, __LINE__, __FILE__); } /* Zimbra::Util::ScopedBuffer<char> strProfName; WtoA((LPWSTR)strProfileName.c_str(), strProfName.getref()); hr=m_pProfAdmin->SetDefaultProfile((LPTSTR)strProfName.get(),NULL); */ } return hr; }