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.

363 lines
5.8 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. mapi.c
  5. Abstract:
  6. This file implements wrappers for all mapi apis.
  7. The wrappers are necessary because mapi does not
  8. implement unicode and this code must be non-unicode.
  9. Environment:
  10. WIN32 User Mode
  11. Author:
  12. Wesley Witt (wesw) 7-Aug-1996
  13. --*/
  14. #include "faxcpl.h"
  15. #include <mapiwin.h>
  16. #include <mapix.h>
  17. static HMODULE MapiMod = NULL;
  18. static LPMAPIADMINPROFILES MapiAdminProfiles = NULL;
  19. static LPMAPIINITIALIZE MapiInitialize = NULL;
  20. static LPMAPIUNINITIALIZE MapiUnInitialize = NULL;
  21. static LPMAPIFREEBUFFER pMAPIFreeBuffer = NULL;
  22. static LPPROFADMIN lpProfAdmin;
  23. BOOL isMapiEnabled = FALSE;
  24. static
  25. LPWSTR
  26. AnsiStringToUnicodeString(
  27. LPSTR AnsiString,
  28. LPWSTR UnicodeString
  29. )
  30. {
  31. DWORD Count;
  32. //
  33. // first see how big the buffer needs to be
  34. //
  35. Count = MultiByteToWideChar(
  36. CP_ACP,
  37. MB_PRECOMPOSED,
  38. AnsiString,
  39. -1,
  40. NULL,
  41. 0
  42. );
  43. //
  44. // i guess the input string is empty
  45. //
  46. if (!Count) {
  47. return NULL;
  48. }
  49. //
  50. // convert the string
  51. //
  52. Count = MultiByteToWideChar(
  53. CP_ACP,
  54. MB_PRECOMPOSED,
  55. AnsiString,
  56. -1,
  57. UnicodeString,
  58. Count
  59. );
  60. //
  61. // the conversion failed
  62. //
  63. if (!Count) {
  64. return NULL;
  65. }
  66. return UnicodeString;
  67. }
  68. static
  69. LPSTR
  70. UnicodeStringToAnsiString(
  71. LPWSTR UnicodeString,
  72. LPSTR AnsiString
  73. )
  74. {
  75. DWORD Count;
  76. //
  77. // first see how big the buffer needs to be
  78. //
  79. Count = WideCharToMultiByte(
  80. CP_ACP,
  81. 0,
  82. UnicodeString,
  83. -1,
  84. NULL,
  85. 0,
  86. NULL,
  87. NULL
  88. );
  89. //
  90. // i guess the input string is empty
  91. //
  92. if (!Count) {
  93. return NULL;
  94. }
  95. //
  96. // convert the string
  97. //
  98. Count = WideCharToMultiByte(
  99. CP_ACP,
  100. 0,
  101. UnicodeString,
  102. -1,
  103. AnsiString,
  104. Count,
  105. NULL,
  106. NULL
  107. );
  108. //
  109. // the conversion failed
  110. //
  111. if (!Count) {
  112. return NULL;
  113. }
  114. return AnsiString;
  115. }
  116. VOID
  117. FreeSRowSet(
  118. LPSRowSet prws
  119. )
  120. {
  121. ULONG irw;
  122. if (!prws) {
  123. return;
  124. }
  125. for(irw = 0; irw < prws->cRows; irw++) {
  126. pMAPIFreeBuffer( prws->aRow[irw].lpProps );
  127. }
  128. pMAPIFreeBuffer( prws );
  129. }
  130. ULONG
  131. MLCRelease(
  132. LPUNKNOWN punk
  133. )
  134. {
  135. return (punk) ? punk->lpVtbl->Release(punk) : 0;
  136. }
  137. BOOL
  138. ValidateProp(
  139. LPSPropValue pval,
  140. ULONG ulPropTag
  141. )
  142. {
  143. if (pval->ulPropTag != ulPropTag) {
  144. pval->ulPropTag = ulPropTag;
  145. pval->Value.lpszA = "???";
  146. return TRUE;
  147. }
  148. return FALSE;
  149. }
  150. BOOL
  151. GetDefaultMapiProfile(
  152. LPWSTR ProfileName
  153. )
  154. {
  155. BOOL rVal = FALSE;
  156. LPMAPITABLE pmt = NULL;
  157. LPSRowSet prws = NULL;
  158. LPSPropValue pval;
  159. DWORD i;
  160. //
  161. // get the mapi profile table object
  162. //
  163. if (lpProfAdmin->lpVtbl->GetProfileTable( lpProfAdmin, 0, &pmt )) {
  164. goto exit;
  165. }
  166. //
  167. // get the actual profile data, FINALLY
  168. //
  169. if (pmt->lpVtbl->QueryRows( pmt, 4000, 0, &prws )) {
  170. goto exit;
  171. }
  172. //
  173. // enumerate the profiles looking for the default profile
  174. //
  175. for (i=0; i<prws->cRows; i++) {
  176. pval = prws->aRow[i].lpProps;
  177. if (pval[2].Value.b) {
  178. //
  179. // this is the default profile
  180. //
  181. AnsiStringToUnicodeString( pval[0].Value.lpszA, ProfileName );
  182. rVal = TRUE;
  183. break;
  184. }
  185. }
  186. exit:
  187. FreeSRowSet( prws );
  188. MLCRelease( (LPUNKNOWN)pmt );
  189. return rVal;
  190. }
  191. BOOL
  192. InitializeMapi(
  193. VOID
  194. )
  195. {
  196. MAPIINIT_0 MapiInit;
  197. //
  198. // load the mapi dll
  199. //
  200. MapiMod = LoadLibrary( TEXT("mapi32.dll") );
  201. if (!MapiMod) {
  202. return FALSE;
  203. }
  204. //
  205. // get the addresses of the mapi functions that we need
  206. //
  207. MapiAdminProfiles = (LPMAPIADMINPROFILES) GetProcAddress( MapiMod, "MAPIAdminProfiles" );
  208. MapiInitialize = (LPMAPIINITIALIZE) GetProcAddress( MapiMod, "MAPIInitialize" );
  209. MapiUnInitialize = (LPMAPIUNINITIALIZE) GetProcAddress( MapiMod, "MAPIUninitialize" );
  210. pMAPIFreeBuffer = (LPMAPIFREEBUFFER) GetProcAddress( MapiMod, "MAPIFreeBuffer" );
  211. if (!MapiAdminProfiles || !MapiInitialize || !MapiUnInitialize || !pMAPIFreeBuffer) {
  212. return FALSE;
  213. }
  214. //
  215. // initialize mapi for our calls
  216. //
  217. MapiInit.ulVersion = 0;
  218. MapiInit.ulFlags = 0;
  219. if (MapiInitialize( &MapiInit )) {
  220. return FALSE;
  221. }
  222. //
  223. // get the admin profile object
  224. //
  225. if (MapiAdminProfiles( 0, &lpProfAdmin )) {
  226. MapiUnInitialize();
  227. FreeLibrary(MapiMod);
  228. return FALSE;
  229. }
  230. return TRUE;
  231. }
  232. VOID
  233. ShutdownMapi(
  234. VOID
  235. )
  236. {
  237. if (isMapiEnabled) {
  238. MapiUnInitialize();
  239. FreeLibrary(MapiMod);
  240. isMapiEnabled = FALSE;
  241. }
  242. }
  243. BOOL
  244. GetMapiProfiles(
  245. HWND hwnd
  246. )
  247. {
  248. BOOL rVal = FALSE;
  249. HMODULE MapiMod = NULL;
  250. LPMAPITABLE pmt = NULL;
  251. LPSRowSet prws = NULL;
  252. LPSPropValue pval;
  253. DWORD i;
  254. //
  255. // get the mapi table object
  256. //
  257. if (lpProfAdmin->lpVtbl->GetProfileTable( lpProfAdmin, 0, &pmt )) {
  258. goto exit;
  259. }
  260. //
  261. // get the actual profile data, FINALLY
  262. //
  263. if (pmt->lpVtbl->QueryRows( pmt, 4000, 0, &prws )) {
  264. goto exit;
  265. }
  266. //
  267. // enumerate the profiles and put the name
  268. // of each profile in the combo box
  269. //
  270. for (i=0; i<prws->cRows; i++) {
  271. pval = prws->aRow[i].lpProps;
  272. SendMessageA(
  273. hwnd,
  274. CB_ADDSTRING,
  275. 0,
  276. (LPARAM) pval[0].Value.lpszA
  277. );
  278. }
  279. rVal = TRUE;
  280. exit:
  281. FreeSRowSet( prws );
  282. MLCRelease( (LPUNKNOWN)pmt );
  283. return rVal;
  284. }