Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
5.0 KiB

  1. #include "precomp.h"
  2. #include <mapix.h>
  3. #include "wabutil.h"
  4. #include "AdLkup.h"
  5. #include "MapiInit.h"
  6. // From Platform SDK AddrLkup.c
  7. ////////////////////////////////////////////////////////////////////////////
  8. /*
  9. This function was ripped out from the source for AdrLkup.lib
  10. because it is the only function that we use from that lib, and linking
  11. to it required linking to MAPI32.lib as well as C-RunTime LIBs.
  12. */
  13. ////////////////////////////////////////////////////////////////////////////
  14. //$--HrFindExchangeGlobalAddressList-------------------------------------------------
  15. // Returns the entry ID of the global address list container in the address
  16. // book.
  17. // -----------------------------------------------------------------------------
  18. HRESULT HrFindExchangeGlobalAddressList( // RETURNS: return code
  19. IN LPADRBOOK lpAdrBook, // address book pointer
  20. OUT ULONG *lpcbeid, // pointer to count of bytes in entry ID
  21. OUT LPENTRYID *lppeid) // pointer to entry ID pointer
  22. {
  23. HRESULT hr = NOERROR;
  24. ULONG ulObjType = 0;
  25. ULONG i = 0;
  26. LPMAPIPROP lpRootContainer = NULL;
  27. LPMAPITABLE lpContainerTable = NULL;
  28. LPSRowSet lpRows = NULL;
  29. ULONG cbContainerEntryId = 0;
  30. LPENTRYID lpContainerEntryId = NULL;
  31. LPSPropValue lpCurrProp = NULL;
  32. SRestriction SRestrictAnd[2] = {0};
  33. SRestriction SRestrictGAL = {0};
  34. SPropValue SPropID = {0};
  35. SPropValue SPropProvider = {0};
  36. BYTE muid[] = MUIDEMSAB;
  37. SizedSPropTagArray(1, rgPropTags) =
  38. {
  39. 1,
  40. {
  41. PR_ENTRYID,
  42. }
  43. };
  44. if(FAILED(hr))
  45. return(hr);
  46. *lpcbeid = 0;
  47. *lppeid = NULL;
  48. // Open the root container of the address book
  49. hr = (lpAdrBook)->OpenEntry(
  50. 0,
  51. NULL,
  52. NULL,
  53. MAPI_DEFERRED_ERRORS,
  54. &ulObjType,
  55. (LPUNKNOWN FAR *)&lpRootContainer);
  56. if(FAILED(hr))
  57. {
  58. goto cleanup;
  59. }
  60. if(ulObjType != MAPI_ABCONT)
  61. {
  62. hr = E_FAIL;
  63. goto cleanup;
  64. }
  65. // Get the hierarchy table of the root container
  66. hr = (((LPABCONT)lpRootContainer))->GetHierarchyTable(
  67. MAPI_DEFERRED_ERRORS|CONVENIENT_DEPTH,
  68. &lpContainerTable);
  69. if(FAILED(hr))
  70. {
  71. goto cleanup;
  72. }
  73. // Restrict the table to the global address list (GAL)
  74. // ---------------------------------------------------
  75. // Initialize provider restriction to only Exchange providers
  76. SRestrictAnd[0].rt = RES_PROPERTY;
  77. SRestrictAnd[0].res.resProperty.relop = RELOP_EQ;
  78. SRestrictAnd[0].res.resProperty.ulPropTag = PR_AB_PROVIDER_ID;
  79. SPropProvider.ulPropTag = PR_AB_PROVIDER_ID;
  80. SPropProvider.Value.bin.cb = 16;
  81. SPropProvider.Value.bin.lpb = (LPBYTE)muid;
  82. SRestrictAnd[0].res.resProperty.lpProp = &SPropProvider;
  83. // Initialize container ID restriction to only GAL container
  84. SRestrictAnd[1].rt = RES_PROPERTY;
  85. SRestrictAnd[1].res.resProperty.relop = RELOP_EQ;
  86. SRestrictAnd[1].res.resProperty.ulPropTag = PR_EMS_AB_CONTAINERID;
  87. SPropID.ulPropTag = PR_EMS_AB_CONTAINERID;
  88. SPropID.Value.l = 0;
  89. SRestrictAnd[1].res.resProperty.lpProp = &SPropID;
  90. // Initialize AND restriction
  91. SRestrictGAL.rt = RES_AND;
  92. SRestrictGAL.res.resAnd.cRes = 2;
  93. SRestrictGAL.res.resAnd.lpRes = &SRestrictAnd[0];
  94. // Restrict the table to the GAL - only a single row should remain
  95. // Get the row corresponding to the GAL
  96. //
  97. // Query all the rows
  98. //
  99. hr = lpfnHrQueryAllRows(
  100. lpContainerTable,
  101. (LPSPropTagArray)&rgPropTags,
  102. &SRestrictGAL,
  103. NULL,
  104. 0,
  105. &lpRows);
  106. if(FAILED(hr) || (lpRows == NULL) || (lpRows->cRows != 1))
  107. {
  108. hr = E_FAIL;
  109. goto cleanup;
  110. }
  111. // Get the entry ID for the GAL
  112. lpCurrProp = &(lpRows->aRow[0].lpProps[0]);
  113. if(lpCurrProp->ulPropTag == PR_ENTRYID)
  114. {
  115. cbContainerEntryId = lpCurrProp->Value.bin.cb;
  116. lpContainerEntryId = (LPENTRYID)lpCurrProp->Value.bin.lpb;
  117. }
  118. else
  119. {
  120. hr = EDK_E_NOT_FOUND;
  121. goto cleanup;
  122. }
  123. hr = lpfnMAPIAllocateBuffer(cbContainerEntryId, (LPVOID *)lppeid);
  124. if(FAILED(hr))
  125. {
  126. *lpcbeid = 0;
  127. *lppeid = NULL;
  128. }
  129. else
  130. {
  131. CopyMemory(
  132. *lppeid,
  133. lpContainerEntryId,
  134. cbContainerEntryId);
  135. *lpcbeid = cbContainerEntryId;
  136. }
  137. cleanup:
  138. lpRootContainer -> Release();
  139. lpContainerTable -> Release();
  140. lpfnFreeProws( lpRows );
  141. if(FAILED(hr))
  142. {
  143. lpfnMAPIFreeBuffer(*lppeid);
  144. *lpcbeid = 0;
  145. *lppeid = NULL;
  146. }
  147. return(hr);
  148. }