Leaked source code of windows server 2003
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.

251 lines
6.3 KiB

  1. /****************************** Module Header ******************************\
  2. * Module Name: who.c
  3. *
  4. * Copyright (c) 1998, Microsoft Corporation
  5. *
  6. * Command line exchange lookup of phone and office numbers.
  7. *
  8. * History:
  9. * 26-Mar-1998 vadimg created
  10. \***************************************************************************/
  11. #include <stdio.h>
  12. #include <locale.h>
  13. #include <mapix.h>
  14. #include <mapi.h>
  15. #include <mapiwin.h>
  16. #include <mapiutil.h>
  17. #include <mapidefs.h>
  18. #include <initguid.h>
  19. #include <mapiguid.h>
  20. #define DISPLAY_NAME 0
  21. #define ACCOUNT 1
  22. #define BUSINESS_TELEPHONE_NUMBER 2
  23. #define OFFICE_LOCATION 3
  24. DWORD gdwCodePage;
  25. void FreeRowSet(LPSRowSet psrs)
  26. {
  27. ULONG i;
  28. for (i = 0; i < psrs->cRows; i++) {
  29. LPSPropValue pspv = psrs->aRow[i].lpProps;
  30. if (pspv != NULL) {
  31. MAPIFreeBuffer((LPVOID)pspv);
  32. }
  33. }
  34. MAPIFreeBuffer((LPVOID)psrs);
  35. }
  36. void PrintName(ULONG cProps, LPSPropValue rgspv)
  37. {
  38. if (rgspv[DISPLAY_NAME].Value.err != MAPI_E_NOT_FOUND) {
  39. WCHAR buffW[30];
  40. int len;
  41. len = MultiByteToWideChar(CP_ACP,
  42. 0,
  43. rgspv[DISPLAY_NAME].Value.lpszA,
  44. -1,
  45. buffW,
  46. 30);
  47. WideCharToMultiByte(gdwCodePage,
  48. 0,
  49. buffW,
  50. len,
  51. rgspv[DISPLAY_NAME].Value.lpszA,
  52. 30, NULL, NULL);
  53. printf("%-25.23s", rgspv[DISPLAY_NAME].Value.lpszA);
  54. }
  55. if (rgspv[ACCOUNT].Value.err != MAPI_E_NOT_FOUND) {
  56. printf("%-17.15s", rgspv[ACCOUNT].Value.lpszA);
  57. }
  58. if (rgspv[BUSINESS_TELEPHONE_NUMBER].Value.err != MAPI_E_NOT_FOUND) {
  59. printf("%-22.21s", rgspv[BUSINESS_TELEPHONE_NUMBER].Value.lpszA);
  60. }
  61. if (rgspv[OFFICE_LOCATION].Value.err != MAPI_E_NOT_FOUND) {
  62. printf("%-15.15s", rgspv[OFFICE_LOCATION].Value.lpszA);
  63. }
  64. printf("\n");
  65. }
  66. BOOL PrintNames(LPSTR psz, LPSRowSet psrs)
  67. {
  68. ULONG i;
  69. if (psrs->cRows == 0)
  70. return FALSE;
  71. for (i = 0; i < psrs->cRows; i++) {
  72. PrintName(psrs->aRow[i].cValues, psrs->aRow[i].lpProps);
  73. }
  74. return TRUE;
  75. }
  76. BOOL FindNames(LPMAPITABLE pmtb, char *psz)
  77. {
  78. SRestriction sres;
  79. SPropValue spv;
  80. SizedSPropTagArray(4, rgspt) = {4, {PR_DISPLAY_NAME, PR_ACCOUNT,
  81. PR_BUSINESS_TELEPHONE_NUMBER, PR_OFFICE_LOCATION}};
  82. if (FAILED(pmtb->SetColumns((LPSPropTagArray)&rgspt, 0)))
  83. return FALSE;
  84. spv.ulPropTag = PR_ANR;
  85. spv.Value.lpszA = psz;
  86. sres.rt = RES_PROPERTY;
  87. sres.res.resProperty.relop = RELOP_EQ;
  88. sres.res.resProperty.ulPropTag = spv.ulPropTag;
  89. sres.res.resProperty.lpProp = &spv;
  90. if (FAILED(pmtb->Restrict(&sres, 0)))
  91. return FALSE;
  92. return TRUE;
  93. }
  94. BOOL GetGlobalAdressListTable(LPADRBOOK padb, LPMAPITABLE *ppmtb)
  95. {
  96. ULONG ulType;
  97. IABContainer *pabc = NULL;
  98. LPMAPITABLE pmtb = NULL;
  99. LPSRowSet psrs = NULL;
  100. SRestriction sres;
  101. SPropValue spv;
  102. SizedSPropTagArray(1, rgspt) = {1, {PR_ENTRYID}};
  103. BOOL fRet = FALSE;
  104. if (FAILED(padb->OpenEntry(0, NULL, NULL, MAPI_BEST_ACCESS |
  105. MAPI_DEFERRED_ERRORS, &ulType, (LPUNKNOWN*)&pabc)))
  106. goto Cleanup;
  107. if (FAILED(pabc->GetHierarchyTable(MAPI_DEFERRED_ERRORS, &pmtb)))
  108. goto Cleanup;
  109. pabc->Release();
  110. pabc = NULL;
  111. spv.ulPropTag = PR_DISPLAY_NAME;
  112. spv.Value.lpszA = "Global Address List";
  113. sres.rt = RES_PROPERTY;
  114. sres.res.resProperty.relop = RELOP_EQ;
  115. sres.res.resProperty.ulPropTag = spv.ulPropTag;
  116. sres.res.resProperty.lpProp = &spv;
  117. if (FAILED(pmtb->FindRow(&sres, BOOKMARK_BEGINNING, 0)))
  118. goto Cleanup;
  119. if (FAILED(pmtb->SetColumns((LPSPropTagArray)&rgspt, 0)))
  120. goto Cleanup;
  121. if (FAILED(pmtb->QueryRows(1, 0, &psrs)))
  122. goto Cleanup;
  123. if (FAILED(padb->OpenEntry(psrs->aRow[0].lpProps[0].Value.bin.cb,
  124. (LPENTRYID)psrs->aRow[0].lpProps[0].Value.bin.lpb,
  125. NULL, MAPI_BEST_ACCESS | MAPI_DEFERRED_ERRORS,
  126. &ulType, (LPUNKNOWN*)&pabc)))
  127. goto Cleanup;
  128. if (FAILED(pabc->GetContentsTable(MAPI_DEFERRED_ERRORS, ppmtb)))
  129. goto Cleanup;
  130. fRet = TRUE;
  131. Cleanup:
  132. if (pmtb != NULL) {
  133. pmtb->Release();
  134. }
  135. if (pabc != NULL) {
  136. pabc->Release();
  137. }
  138. if (psrs != NULL) {
  139. FreeRowSet(psrs);
  140. }
  141. return fRet;
  142. }
  143. void PrintUsage(void)
  144. {
  145. printf("Sample Usage:\n");
  146. printf(" who jane\n");
  147. printf(" who janed\n");
  148. printf(" who \"jane d\"\n");
  149. }
  150. int __cdecl main(int argc, char **argv)
  151. {
  152. IMAPISession *pmss = NULL;
  153. LPADRBOOK padb = NULL;
  154. LPMAPITABLE pmtb;
  155. LPSRowSet psrs = NULL;
  156. ULONG cRows;
  157. BOOL fContinue;
  158. LPSTR psz = argv[1];
  159. char lBuf[6];
  160. if (argc == 1 || (argc == 2 && argv[1][0] == '-')) {
  161. PrintUsage();
  162. return 0;
  163. }
  164. gdwCodePage = GetConsoleOutputCP();
  165. #if 0
  166. SetThreadLocale(MAKELCID(MAKELANGID(LANG_NEUTRAL, SUBLANG_SYS_DEFAULT),
  167. SORT_DEFAULT));
  168. sprintf(lBuf, ".%.6d", gdwCodePage);
  169. setlocale(LC_ALL, lBuf);
  170. #endif
  171. if (FAILED(MAPIInitialize(NULL)))
  172. return 0;
  173. if (FAILED(MAPILogonEx(0, NULL, NULL, MAPI_ALLOW_OTHERS |
  174. MAPI_USE_DEFAULT, &pmss)))
  175. goto Cleanup;
  176. if (FAILED(pmss->OpenAddressBook(NULL, NULL, AB_NO_DIALOG, &padb)))
  177. goto Cleanup;
  178. if (!GetGlobalAdressListTable(padb, &pmtb))
  179. goto Cleanup;
  180. if (!FindNames(pmtb, psz))
  181. goto Cleanup;
  182. if (FAILED(pmtb->GetRowCount(0, &cRows)))
  183. goto Cleanup;
  184. do {
  185. if (FAILED(pmtb->QueryRows(cRows, 0, &psrs)))
  186. goto Cleanup;
  187. if (psrs == NULL)
  188. break;
  189. fContinue = PrintNames(psz, psrs);
  190. FreeRowSet(psrs);
  191. } while (fContinue);
  192. Cleanup:
  193. if (padb != NULL) {
  194. padb->Release();
  195. }
  196. if (pmss != NULL) {
  197. pmss->Release();
  198. }
  199. MAPIUninitialize();
  200. return 0;
  201. }