Advertisement
Guest User

Untitled

a guest
Jun 18th, 2015
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.82 KB | None | 0 0
  1. bool CreateProfileWithIProfAdmin()
  2. {
  3. HRESULT hRes = S_OK; // Result from MAPI calls
  4. LPPROFADMIN lpProfAdmin = NULL; // Profile Admin object
  5. LPSERVICEADMIN lpSvcAdmin = NULL; // Service Admin object
  6. LPMAPITABLE lpMsgSvcTable = NULL; // Table to hold services
  7. LPSRowSet lpSvcRows = NULL; // Rowset to hold results of table query
  8. SPropValue rgval[7]; // Property structure to hold values we want to set
  9. SRestriction sres; // Restriction structure
  10. SPropValue SvcProps; // Property structure for restriction
  11. char szConfigFileName[80] = {0}; // String for profile name
  12. MyWideString strServer, strMailbox, strProfile, strProxyServer, strPrincipleName;
  13.  
  14. LPPROFSECT lpProfSect = NULL;
  15. SPropValue rgvalProf[10];
  16. MyWideString strProfileUser;
  17.  
  18. // This indicates columns we want returned from HrQueryAllRows
  19. enum {iSvcName, iSvcUID, cptaSvc};
  20. SizedSPropTagArray(cptaSvc,sptCols) = { cptaSvc, PR_SERVICE_NAME, PR_SERVICE_UID };
  21.  
  22. ProfileParams params;
  23. // Get configuration info from user
  24. cout <<"Enter profile config file: ";
  25. cin >>szConfigFileName;
  26. cout << "\n";
  27. if (!readProfile(szConfigFileName, params)) {
  28. cout << "Error reading profile\n";
  29. return false;
  30. }
  31. strProfileUser.set(params.profileUser.c_str());
  32. // Initialize MAPI
  33.  
  34. cout << "Calling MAPIInitialize\n";
  35. if (FAILED(hRes = MAPIInitialize(NULL)))
  36. {
  37. cout<<"Error initializing MAPI.\n";
  38. goto error;
  39. }
  40.  
  41. // Get an IProfAdmin interface
  42. cout << "Calling MAPIIAdminProfiles\n";
  43.  
  44. if (FAILED(hRes = MAPIAdminProfiles(0, // Flags
  45. &lpProfAdmin))) // Pointer to new IProfAdmin
  46. {
  47. cout<<"Error getting IProfAdmin interface.\n";
  48. goto error;
  49. }
  50.  
  51. // Create a new profile
  52.  
  53. cout << "Calling CreateProfie" << params.profileName << "\n";
  54. if (FAILED(hRes = lpProfAdmin->CreateProfile((LPSTR) (params.profileName.c_str()), // Name of new profile
  55. NULL, // Password for profile
  56. NULL, // Handle to parent window
  57. NULL))) // Flags
  58. {
  59. cout<<"Error creating profile.\n";
  60. goto error;
  61. }
  62.  
  63. // Get an IMsgServiceAdmin interface off of the IProfAdmin interface
  64.  
  65. cout << "Calling AdminServices" << params.profileName << "\n";
  66. if (FAILED(hRes = lpProfAdmin->AdminServices((LPSTR) (params.profileName.c_str()), // Profile that we want to modify
  67. NULL, // Password for that profile
  68. NULL, // Handle to parent window
  69. 0, // Flags
  70. &lpSvcAdmin))) // Pointer to new IMsgServiceAdmin
  71. {
  72. cout<<"Error getting IMsgServiceAdmin interface.\n";
  73. goto error;
  74. }
  75.  
  76. // Create the new message service for Exchange
  77.  
  78. cout << "Calling CreateMsgServices : MSEMS" << "\n";
  79. if (FAILED(hRes = lpSvcAdmin->CreateMsgService("MSEMS", // Name of service from MAPISVC.INF
  80. NULL, // Display name of service
  81. NULL, // Handle to parent window
  82. NULL))) // Flags
  83. {
  84. cout<<"Error creating Exchange message service.\n";
  85. goto error;
  86. }
  87.  
  88. // We now need to get the entry id for the new service
  89. // This can be done by getting the message service table
  90. // and getting the entry that corresponds to the new service.
  91.  
  92. cout << "Calling GetMsgServiceTable" << "\n";
  93. if (FAILED(hRes = lpSvcAdmin->GetMsgServiceTable(0, // Flags
  94. &lpMsgSvcTable))) // Pointer to table
  95. {
  96. cout<<"Error getting Message Service Table.\n";
  97. goto error;
  98. }
  99.  
  100. // Set up restriction to query table.
  101.  
  102. sres.rt = RES_CONTENT;
  103. sres.res.resContent.ulFuzzyLevel = FL_FULLSTRING;
  104. sres.res.resContent.ulPropTag = PR_SERVICE_NAME;
  105. sres.res.resContent.lpProp = &SvcProps;
  106.  
  107. SvcProps.ulPropTag = PR_SERVICE_NAME;
  108. SvcProps.Value.lpszA = "MSEMS";
  109.  
  110. // Query the table to get the entry for the newly create message service.
  111.  
  112. cout << "Calling HrQueryAllRows" << "\n";
  113. if (FAILED(hRes = HrQueryAllRows(lpMsgSvcTable,
  114. (LPSPropTagArray)&sptCols,
  115. &sres,
  116. NULL,
  117. 0,
  118. &lpSvcRows)))
  119. {
  120. cout<<"Error querying table for new message service.";
  121. goto error;
  122. }
  123.  
  124. // Setup a SPropValue array for the properties you need to configure
  125. strServer.set(params.profileUnresolvedServer.c_str());
  126. strMailbox.set(params.profileUnresolvedName.c_str());
  127. //strProfile.set();
  128.  
  129. strProxyServer.set(params.proxyServer.c_str());
  130. // First, server name
  131. ZeroMemory(&rgval[0], sizeof(SPropValue) );
  132. rgval[0].ulPropTag = PR_PROFILE_UNRESOLVED_SERVER + 1;
  133. // LPCTSTR szr = CA2T(szServer);
  134. rgval[0].Value.lpszW = strServer.getWideStr();
  135.  
  136. // Next, the mailbox name
  137. ZeroMemory(&rgval[1], sizeof(SPropValue) );
  138. rgval[1].ulPropTag = PR_PROFILE_UNRESOLVED_NAME + 1;
  139. rgval[1].Value.lpszW = strMailbox.getWideStr();
  140.  
  141. ZeroMemory(&rgval[2], sizeof(SPropValue));
  142. rgval[2].ulPropTag = 0x6622001F; // PR_ROH_PROXY_SERVER;
  143. rgval[2].Value.lpszW = strProxyServer.getWideStr();
  144.  
  145. cout << "Calling ConfigureMsgService. Server = " << params.profileUnresolvedServer << " mailbox = " << params.profileUnresolvedName << "\n";
  146. if (FAILED(hRes = lpSvcAdmin->ConfigureMsgService(
  147. (LPMAPIUID)lpSvcRows->aRow->lpProps[iSvcUID].Value.bin.lpb, // Entry ID of service to configure
  148. NULL, // Handle to parent window
  149. 0, // Flags
  150. 3, // Number of properties we are setting
  151. rgval))) // Pointer to SPropValue array
  152. {
  153. cout<<"Error configuring message service.";
  154. goto error;
  155. }
  156. /*
  157. */
  158. // Open Profile
  159. #define MAPI_FORCE_ACCESS 0x00080000
  160. hRes = lpSvcAdmin->OpenProfileSection(
  161. (LPMAPIUID)lpSvcRows->aRow->lpProps[iSvcUID].Value.bin.lpb, // Entry ID of service to configure
  162. NULL, // Handle to parent window
  163. MAPI_MODIFY | MAPI_FORCE_ACCESS, // Flags
  164. &lpProfSect
  165. );
  166. cout << "lpProfSect = " << (void*)lpProfSect << "\n";
  167. if (lpProfSect) {
  168.  
  169. #define PR_EMSMDB_SECTION_UID 0x3D150102
  170. // SizedSPropTagArray(1, sptProps) = { PR_EMSMDB_SECTION_UID };
  171. ULONG sptProps[] = {1, PR_EMSMDB_SECTION_UID };
  172. // SPropValue propVal[1];
  173. SPropValue *propVal = NULL;
  174. ULONG count = 0;
  175. hRes = lpProfSect->GetProps((LPSPropTagArray)sptProps, MAPI_UNICODE, &count, &propVal);
  176. cout << "GetProps returned " << hex << hRes << dec << " count = " << count << endl;
  177. LPPROFSECT lpProfSect2 = NULL;
  178. if (hRes == 0) {
  179. if (count == 1) {
  180. cout << "prop bin = " << propVal->Value.bin.cb << endl;
  181. if (propVal && propVal->Value.bin.cb == 16) {
  182. hRes = lpSvcAdmin->OpenProfileSection(
  183. (LPMAPIUID)propVal->Value.bin.lpb, // Entry ID of service to configure
  184. NULL, // Handle to parent window
  185. MAPI_MODIFY | MAPI_FORCE_ACCESS, // Flags
  186. &lpProfSect2
  187. );
  188. cout << "lpProfSect2 = " << (void*)lpProfSect2 << "\n";
  189. }
  190. }
  191. }
  192. if (lpProfSect2) {
  193. ZeroMemory(&rgvalProf[0], sizeof(SPropValue));
  194. rgvalProf[0].ulPropTag = 0x6622001F; // PR_ROH_PROXY_SERVER;
  195. rgvalProf[0].Value.lpszW = strProxyServer.getWideStr();
  196.  
  197. ZeroMemory(&rgvalProf[1], sizeof(SPropValue));
  198. rgvalProf[1].ulPropTag = 0x66230003; // PR_ROH_FLAGS;
  199. rgvalProf[1].Value.l = params.rohFlags;
  200.  
  201. ZeroMemory(&rgvalProf[2], sizeof(SPropValue));
  202. rgvalProf[2].ulPropTag = 0x66270003; // PR_ROH_PROXY_AUTH_SCHEME;
  203. rgvalProf[2].Value.l = params.authScheme; // RPC_C_HTTP_AUTHN_SCHEME_BASIC
  204.  
  205. ZeroMemory(&rgvalProf[3], sizeof(SPropValue));
  206. rgvalProf[3].ulPropTag = 0x66190003; // PR_PROFILE_AUTH_PACKAGE;
  207. rgvalProf[3].Value.l = params.authPackage; // RPC_C_AUTHN_GSS_NEGOTIATE
  208.  
  209. strPrincipleName.set(params.proxyPrincipleName.c_str());
  210. ZeroMemory(&rgvalProf[4], sizeof(SPropValue));
  211. rgvalProf[4].ulPropTag = 0x6625001F; // PR_ROH_PROXY_PRINCIPLE_NAME;
  212. rgvalProf[4].Value.lpszW = strPrincipleName.getWideStr();
  213.  
  214. char* s = (char *) params.homeServer.c_str();
  215. // First, server name
  216. ZeroMemory(&rgvalProf[5], sizeof(SPropValue) );
  217. // rgvalProf[1].ulPropTag = PR_PROFILE_UNRESOLVED_SERVER + 1;
  218. // rgvalProf[5].ulPropTag = PR_PROFILE_HOME_SERVER + 1;
  219. // rgvalProf[5].Value.lpszW = strServer.getWideStr();
  220. rgvalProf[5].ulPropTag = PR_PROFILE_HOME_SERVER;
  221. rgvalProf[5].Value.lpszA = s;
  222.  
  223. // Next, the mailbox name
  224. ZeroMemory(&rgvalProf[6], sizeof(SPropValue) );
  225. // rgvalProf[0].ulPropTag = PR_PROFILE_UNRESOLVED_NAME + 1;
  226. rgvalProf[6].ulPropTag = PR_DISPLAY_NAME + 1;
  227. rgvalProf[6].Value.lpszW = strMailbox.getWideStr();
  228.  
  229. ZeroMemory(&rgvalProf[7], sizeof(SPropValue) );
  230. // rgvalProf[7].ulPropTag = PR_PROFILE_USER + 1;
  231. // rgvalProf[7].Value.lpszW = strProfileUser.getWideStr();
  232. // char profileUser[128];
  233. s = (char *) params.profileUser.c_str();
  234. rgvalProf[7].ulPropTag = PR_PROFILE_USER;
  235. rgvalProf[7].Value.lpszA = s;
  236.  
  237. ZeroMemory(&rgvalProf[8], sizeof(SPropValue) );
  238. rgvalProf[8].ulPropTag = PR_PROFILE_UNRESOLVED_SERVER + 1;
  239. // LPCTSTR szr = CA2T(szServer);
  240. rgvalProf[8].Value.lpszW = strServer.getWideStr();
  241.  
  242. // Next, the mailbox name
  243. ZeroMemory(&rgvalProf[9], sizeof(SPropValue) );
  244. rgvalProf[9].ulPropTag = PR_PROFILE_UNRESOLVED_NAME + 1;
  245. rgvalProf[9].Value.lpszW = strMailbox.getWideStr();
  246.  
  247. hRes = lpProfSect2->SetProps(10, rgvalProf, NULL);
  248. cout << "SetProps returned : " << hRes << "\n";
  249. lpProfSect2->SaveChanges(FORCE_SAVE);
  250. lpProfSect2->Release();
  251. lpProfSect2 = NULL;
  252. }
  253. lpProfSect->Release();
  254. lpProfSect = NULL;
  255.  
  256. }
  257. /*
  258. */
  259.  
  260. // Open Profile
  261. // Configure the message service with the above properties
  262.  
  263. /*
  264. */
  265.  
  266. goto cleanup;
  267.  
  268. error:
  269. cout<<" hRes = 0x"<<hex<<hRes<<dec<<endl;
  270. return FALSE;
  271.  
  272. cleanup:
  273. // Cleanup
  274. if (lpSvcRows) FreeProws(lpSvcRows);
  275. if (lpMsgSvcTable) lpMsgSvcTable->Release();
  276. if (lpSvcAdmin) lpSvcAdmin->Release();
  277. if (lpProfAdmin) lpProfAdmin->Release();
  278.  
  279. MAPIUninitialize();
  280. return TRUE;
  281.  
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement