Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool CreateProfileWithIProfAdmin()
- {
- HRESULT hRes = S_OK; // Result from MAPI calls
- LPPROFADMIN lpProfAdmin = NULL; // Profile Admin object
- LPSERVICEADMIN lpSvcAdmin = NULL; // Service Admin object
- LPMAPITABLE lpMsgSvcTable = NULL; // Table to hold services
- LPSRowSet lpSvcRows = NULL; // Rowset to hold results of table query
- SPropValue rgval[7]; // Property structure to hold values we want to set
- SRestriction sres; // Restriction structure
- SPropValue SvcProps; // Property structure for restriction
- char szConfigFileName[80] = {0}; // String for profile name
- MyWideString strServer, strMailbox, strProfile, strProxyServer, strPrincipleName;
- LPPROFSECT lpProfSect = NULL;
- SPropValue rgvalProf[10];
- MyWideString strProfileUser;
- // This indicates columns we want returned from HrQueryAllRows
- enum {iSvcName, iSvcUID, cptaSvc};
- SizedSPropTagArray(cptaSvc,sptCols) = { cptaSvc, PR_SERVICE_NAME, PR_SERVICE_UID };
- ProfileParams params;
- // Get configuration info from user
- cout <<"Enter profile config file: ";
- cin >>szConfigFileName;
- cout << "\n";
- if (!readProfile(szConfigFileName, params)) {
- cout << "Error reading profile\n";
- return false;
- }
- strProfileUser.set(params.profileUser.c_str());
- // Initialize MAPI
- cout << "Calling MAPIInitialize\n";
- if (FAILED(hRes = MAPIInitialize(NULL)))
- {
- cout<<"Error initializing MAPI.\n";
- goto error;
- }
- // Get an IProfAdmin interface
- cout << "Calling MAPIIAdminProfiles\n";
- if (FAILED(hRes = MAPIAdminProfiles(0, // Flags
- &lpProfAdmin))) // Pointer to new IProfAdmin
- {
- cout<<"Error getting IProfAdmin interface.\n";
- goto error;
- }
- // Create a new profile
- cout << "Calling CreateProfie" << params.profileName << "\n";
- if (FAILED(hRes = lpProfAdmin->CreateProfile((LPSTR) (params.profileName.c_str()), // Name of new profile
- NULL, // Password for profile
- NULL, // Handle to parent window
- NULL))) // Flags
- {
- cout<<"Error creating profile.\n";
- goto error;
- }
- // Get an IMsgServiceAdmin interface off of the IProfAdmin interface
- cout << "Calling AdminServices" << params.profileName << "\n";
- if (FAILED(hRes = lpProfAdmin->AdminServices((LPSTR) (params.profileName.c_str()), // Profile that we want to modify
- NULL, // Password for that profile
- NULL, // Handle to parent window
- 0, // Flags
- &lpSvcAdmin))) // Pointer to new IMsgServiceAdmin
- {
- cout<<"Error getting IMsgServiceAdmin interface.\n";
- goto error;
- }
- // Create the new message service for Exchange
- cout << "Calling CreateMsgServices : MSEMS" << "\n";
- if (FAILED(hRes = lpSvcAdmin->CreateMsgService("MSEMS", // Name of service from MAPISVC.INF
- NULL, // Display name of service
- NULL, // Handle to parent window
- NULL))) // Flags
- {
- cout<<"Error creating Exchange message service.\n";
- goto error;
- }
- // We now need to get 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.
- cout << "Calling GetMsgServiceTable" << "\n";
- if (FAILED(hRes = lpSvcAdmin->GetMsgServiceTable(0, // Flags
- &lpMsgSvcTable))) // Pointer to table
- {
- cout<<"Error getting Message Service Table.\n";
- goto error;
- }
- // Set up restriction to query table.
- sres.rt = RES_CONTENT;
- sres.res.resContent.ulFuzzyLevel = FL_FULLSTRING;
- sres.res.resContent.ulPropTag = PR_SERVICE_NAME;
- sres.res.resContent.lpProp = &SvcProps;
- SvcProps.ulPropTag = PR_SERVICE_NAME;
- SvcProps.Value.lpszA = "MSEMS";
- // Query the table to get the entry for the newly create message service.
- cout << "Calling HrQueryAllRows" << "\n";
- if (FAILED(hRes = HrQueryAllRows(lpMsgSvcTable,
- (LPSPropTagArray)&sptCols,
- &sres,
- NULL,
- 0,
- &lpSvcRows)))
- {
- cout<<"Error querying table for new message service.";
- goto error;
- }
- // Setup a SPropValue array for the properties you need to configure
- strServer.set(params.profileUnresolvedServer.c_str());
- strMailbox.set(params.profileUnresolvedName.c_str());
- //strProfile.set();
- strProxyServer.set(params.proxyServer.c_str());
- // First, server name
- ZeroMemory(&rgval[0], sizeof(SPropValue) );
- rgval[0].ulPropTag = PR_PROFILE_UNRESOLVED_SERVER + 1;
- // LPCTSTR szr = CA2T(szServer);
- rgval[0].Value.lpszW = strServer.getWideStr();
- // Next, the mailbox name
- ZeroMemory(&rgval[1], sizeof(SPropValue) );
- rgval[1].ulPropTag = PR_PROFILE_UNRESOLVED_NAME + 1;
- rgval[1].Value.lpszW = strMailbox.getWideStr();
- ZeroMemory(&rgval[2], sizeof(SPropValue));
- rgval[2].ulPropTag = 0x6622001F; // PR_ROH_PROXY_SERVER;
- rgval[2].Value.lpszW = strProxyServer.getWideStr();
- cout << "Calling ConfigureMsgService. Server = " << params.profileUnresolvedServer << " mailbox = " << params.profileUnresolvedName << "\n";
- if (FAILED(hRes = lpSvcAdmin->ConfigureMsgService(
- (LPMAPIUID)lpSvcRows->aRow->lpProps[iSvcUID].Value.bin.lpb, // Entry ID of service to configure
- NULL, // Handle to parent window
- 0, // Flags
- 3, // Number of properties we are setting
- rgval))) // Pointer to SPropValue array
- {
- cout<<"Error configuring message service.";
- goto error;
- }
- /*
- */
- // Open Profile
- #define MAPI_FORCE_ACCESS 0x00080000
- hRes = lpSvcAdmin->OpenProfileSection(
- (LPMAPIUID)lpSvcRows->aRow->lpProps[iSvcUID].Value.bin.lpb, // Entry ID of service to configure
- NULL, // Handle to parent window
- MAPI_MODIFY | MAPI_FORCE_ACCESS, // Flags
- &lpProfSect
- );
- cout << "lpProfSect = " << (void*)lpProfSect << "\n";
- if (lpProfSect) {
- #define PR_EMSMDB_SECTION_UID 0x3D150102
- // SizedSPropTagArray(1, sptProps) = { PR_EMSMDB_SECTION_UID };
- ULONG sptProps[] = {1, PR_EMSMDB_SECTION_UID };
- // SPropValue propVal[1];
- SPropValue *propVal = NULL;
- ULONG count = 0;
- hRes = lpProfSect->GetProps((LPSPropTagArray)sptProps, MAPI_UNICODE, &count, &propVal);
- cout << "GetProps returned " << hex << hRes << dec << " count = " << count << endl;
- LPPROFSECT lpProfSect2 = NULL;
- if (hRes == 0) {
- if (count == 1) {
- cout << "prop bin = " << propVal->Value.bin.cb << endl;
- if (propVal && propVal->Value.bin.cb == 16) {
- hRes = lpSvcAdmin->OpenProfileSection(
- (LPMAPIUID)propVal->Value.bin.lpb, // Entry ID of service to configure
- NULL, // Handle to parent window
- MAPI_MODIFY | MAPI_FORCE_ACCESS, // Flags
- &lpProfSect2
- );
- cout << "lpProfSect2 = " << (void*)lpProfSect2 << "\n";
- }
- }
- }
- if (lpProfSect2) {
- ZeroMemory(&rgvalProf[0], sizeof(SPropValue));
- rgvalProf[0].ulPropTag = 0x6622001F; // PR_ROH_PROXY_SERVER;
- rgvalProf[0].Value.lpszW = strProxyServer.getWideStr();
- ZeroMemory(&rgvalProf[1], sizeof(SPropValue));
- rgvalProf[1].ulPropTag = 0x66230003; // PR_ROH_FLAGS;
- rgvalProf[1].Value.l = params.rohFlags;
- ZeroMemory(&rgvalProf[2], sizeof(SPropValue));
- rgvalProf[2].ulPropTag = 0x66270003; // PR_ROH_PROXY_AUTH_SCHEME;
- rgvalProf[2].Value.l = params.authScheme; // RPC_C_HTTP_AUTHN_SCHEME_BASIC
- ZeroMemory(&rgvalProf[3], sizeof(SPropValue));
- rgvalProf[3].ulPropTag = 0x66190003; // PR_PROFILE_AUTH_PACKAGE;
- rgvalProf[3].Value.l = params.authPackage; // RPC_C_AUTHN_GSS_NEGOTIATE
- strPrincipleName.set(params.proxyPrincipleName.c_str());
- ZeroMemory(&rgvalProf[4], sizeof(SPropValue));
- rgvalProf[4].ulPropTag = 0x6625001F; // PR_ROH_PROXY_PRINCIPLE_NAME;
- rgvalProf[4].Value.lpszW = strPrincipleName.getWideStr();
- char* s = (char *) params.homeServer.c_str();
- // First, server name
- ZeroMemory(&rgvalProf[5], sizeof(SPropValue) );
- // rgvalProf[1].ulPropTag = PR_PROFILE_UNRESOLVED_SERVER + 1;
- // rgvalProf[5].ulPropTag = PR_PROFILE_HOME_SERVER + 1;
- // rgvalProf[5].Value.lpszW = strServer.getWideStr();
- rgvalProf[5].ulPropTag = PR_PROFILE_HOME_SERVER;
- rgvalProf[5].Value.lpszA = s;
- // Next, the mailbox name
- ZeroMemory(&rgvalProf[6], sizeof(SPropValue) );
- // rgvalProf[0].ulPropTag = PR_PROFILE_UNRESOLVED_NAME + 1;
- rgvalProf[6].ulPropTag = PR_DISPLAY_NAME + 1;
- rgvalProf[6].Value.lpszW = strMailbox.getWideStr();
- ZeroMemory(&rgvalProf[7], sizeof(SPropValue) );
- // rgvalProf[7].ulPropTag = PR_PROFILE_USER + 1;
- // rgvalProf[7].Value.lpszW = strProfileUser.getWideStr();
- // char profileUser[128];
- s = (char *) params.profileUser.c_str();
- rgvalProf[7].ulPropTag = PR_PROFILE_USER;
- rgvalProf[7].Value.lpszA = s;
- ZeroMemory(&rgvalProf[8], sizeof(SPropValue) );
- rgvalProf[8].ulPropTag = PR_PROFILE_UNRESOLVED_SERVER + 1;
- // LPCTSTR szr = CA2T(szServer);
- rgvalProf[8].Value.lpszW = strServer.getWideStr();
- // Next, the mailbox name
- ZeroMemory(&rgvalProf[9], sizeof(SPropValue) );
- rgvalProf[9].ulPropTag = PR_PROFILE_UNRESOLVED_NAME + 1;
- rgvalProf[9].Value.lpszW = strMailbox.getWideStr();
- hRes = lpProfSect2->SetProps(10, rgvalProf, NULL);
- cout << "SetProps returned : " << hRes << "\n";
- lpProfSect2->SaveChanges(FORCE_SAVE);
- lpProfSect2->Release();
- lpProfSect2 = NULL;
- }
- lpProfSect->Release();
- lpProfSect = NULL;
- }
- /*
- */
- // Open Profile
- // Configure the message service with the above properties
- /*
- */
- goto cleanup;
- error:
- cout<<" hRes = 0x"<<hex<<hRes<<dec<<endl;
- return FALSE;
- cleanup:
- // Cleanup
- if (lpSvcRows) FreeProws(lpSvcRows);
- if (lpMsgSvcTable) lpMsgSvcTable->Release();
- if (lpSvcAdmin) lpSvcAdmin->Release();
- if (lpProfAdmin) lpProfAdmin->Release();
- MAPIUninitialize();
- return TRUE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement