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.

174 lines
5.3 KiB

  1. /*
  2. * MAPI.C
  3. *
  4. * Layer on top of MAPI calls
  5. *
  6. * Copyright 1996 Microsoft Corporation. All Rights Reserved.
  7. *
  8. * History:
  9. * 11/14/96 BruceK First version to allow wab migration without mapi32.dll
  10. */
  11. #include <windows.h>
  12. #include <mapix.h>
  13. #include <wab.h>
  14. #include <wabguid.h>
  15. #include "apitest.h"
  16. #include "instring.h"
  17. #include "dbgutil.h"
  18. LPMAPIINITIALIZE lpfnMAPIInitialize = NULL;
  19. LPMAPILOGONEX lpfnMAPILogonEx = NULL;
  20. LPMAPIALLOCATEBUFFER lpfnMAPIAllocateBuffer = NULL;
  21. LPMAPIALLOCATEMORE lpfnMAPIAllocateMore = NULL;
  22. LPMAPIFREEBUFFER lpfnMAPIFreeBuffer = NULL;
  23. static HINSTANCE hinstMAPIDll = NULL;
  24. // Constant strings
  25. const TCHAR szMapiDll[] = TEXT("MAPI32.DLL");
  26. const TCHAR szMAPIAllocateBuffer[] = TEXT("MAPIAllocateBuffer");
  27. const TCHAR szMAPIAllocateMore[] = TEXT("MAPIAllocateMore");
  28. const TCHAR szMAPIFreeBuffer[] = TEXT("MAPIFreeBuffer");
  29. const TCHAR szMAPIInitialize[] = TEXT("MAPIInitialize");
  30. const TCHAR szMAPILogonEx[] = TEXT("MAPILogonEx");
  31. HRESULT MAPIInitialize(LPVOID lpMapiInit) {
  32. HRESULT hResult = hrSuccess;
  33. // If MAPI DLL is not loaded, do so now.
  34. if (! hinstMAPIDll) {
  35. if (! (hinstMAPIDll = LoadLibrary(szMapiDll))) {
  36. DWORD dwErr = GetLastError();
  37. DebugTrace("Couldn't load MAPI dll [%s] -> %u\n", szMapiDll, dwErr);
  38. switch (dwErr) {
  39. case ERROR_NOT_ENOUGH_MEMORY:
  40. case ERROR_OUTOFMEMORY:
  41. hResult = ResultFromScode(MAPI_E_NOT_ENOUGH_MEMORY);
  42. break;
  43. case ERROR_HANDLE_DISK_FULL:
  44. case ERROR_DISK_FULL:
  45. hResult = ResultFromScode(MAPI_E_NOT_ENOUGH_DISK);
  46. break;
  47. default:
  48. case ERROR_FILE_NOT_FOUND:
  49. case ERROR_PATH_NOT_FOUND:
  50. hResult = ResultFromScode(MAPI_E_NOT_FOUND);
  51. break;
  52. }
  53. goto exit;
  54. } else {
  55. // Get the function pointers
  56. if (! (lpfnMAPIInitialize = (LPMAPIINITIALIZE)GetProcAddress(hinstMAPIDll,
  57. szMAPIInitialize))) {
  58. DebugTrace("Couldn't get Fn addr %s from %s -> %u\n", szMAPIInitialize, szMapiDll, GetLastError());
  59. goto exit;
  60. }
  61. if (! (lpfnMAPILogonEx = (LPMAPILOGONEX)GetProcAddress(hinstMAPIDll,
  62. szMAPILogonEx))) {
  63. DebugTrace("Couldn't get Fn addr %s from %s -> %u\n", szMAPILogonEx, szMapiDll, GetLastError());
  64. goto exit;
  65. }
  66. if (! (lpfnMAPIAllocateBuffer = (LPMAPIALLOCATEBUFFER)GetProcAddress(hinstMAPIDll,
  67. szMAPIAllocateBuffer))) {
  68. DebugTrace("Couldn't get Fn addr %s from %s -> %u\n", szMAPIAllocateBuffer, szMapiDll, GetLastError());
  69. goto exit;
  70. }
  71. if (! (lpfnMAPIAllocateMore= (LPMAPIALLOCATEMORE)GetProcAddress(hinstMAPIDll,
  72. szMAPIAllocateMore))) {
  73. DebugTrace("Couldn't get Fn addr %s from %s -> %u\n", szMAPIAllocateMore, szMapiDll, GetLastError());
  74. goto exit;
  75. }
  76. if (! (lpfnMAPIFreeBuffer = (LPMAPIFREEBUFFER)GetProcAddress(hinstMAPIDll,
  77. szMAPIFreeBuffer))) {
  78. DebugTrace("Couldn't get Fn addr %s from %s -> %u\n", szMAPIFreeBuffer, szMapiDll, GetLastError());
  79. goto exit;
  80. }
  81. }
  82. }
  83. exit:
  84. if (! lpfnMAPIInitialize ||
  85. ! lpfnMAPILogonEx ||
  86. ! lpfnMAPIAllocateMore ||
  87. ! lpfnMAPIAllocateBuffer ||
  88. ! lpfnMAPIFreeBuffer) {
  89. // Bad news. Clean up and fail.
  90. if (hinstMAPIDll) {
  91. // unload the dll
  92. FreeLibrary(hinstMAPIDll);
  93. hinstMAPIDll = NULL;
  94. lpfnMAPIInitialize = NULL;
  95. lpfnMAPILogonEx = NULL;
  96. lpfnMAPIAllocateMore = NULL;
  97. lpfnMAPIAllocateBuffer = NULL;
  98. lpfnMAPIFreeBuffer = NULL;
  99. }
  100. if (hResult == hrSuccess) {
  101. hResult = ResultFromScode(MAPI_E_NOT_FOUND);
  102. }
  103. return(hResult);
  104. }
  105. return(lpfnMAPIInitialize(lpMapiInit));
  106. }
  107. HRESULT MAPILogonEx(
  108. ULONG ulUIParam,
  109. LPTSTR lpszProfileName,
  110. LPTSTR lpszPassword,
  111. ULONG ulFlags,
  112. LPMAPISESSION FAR * lppSession
  113. ) {
  114. if (lpfnMAPILogonEx) {
  115. return(lpfnMAPILogonEx(ulUIParam,
  116. lpszProfileName,
  117. lpszPassword,
  118. ulFlags,
  119. lppSession));
  120. } else {
  121. return(ResultFromScode(MAPI_E_NOT_INITIALIZED));
  122. }
  123. }
  124. SCODE MAPIAllocateBuffer(
  125. ULONG cbSize,
  126. LPVOID FAR * lppBuffer
  127. ) {
  128. if (lpfnMAPIAllocateBuffer) {
  129. return(lpfnMAPIAllocateBuffer(cbSize,
  130. lppBuffer));
  131. } else {
  132. return(MAPI_E_NOT_INITIALIZED);
  133. }
  134. }
  135. SCODE MAPIAllocateMore(
  136. ULONG cbSize,
  137. LPVOID lpObject,
  138. LPVOID FAR * lppBuffer
  139. ) {
  140. if (lpfnMAPIAllocateMore) {
  141. return(lpfnMAPIAllocateMore(cbSize,
  142. lpObject,
  143. lppBuffer));
  144. } else {
  145. return(MAPI_E_NOT_INITIALIZED);
  146. }
  147. }
  148. ULONG MAPIFreeBuffer(LPVOID lpBuffer) {
  149. if (lpfnMAPIFreeBuffer) {
  150. return(lpfnMAPIFreeBuffer(lpBuffer));
  151. } else {
  152. return((ULONG)MAPI_E_NOT_INITIALIZED);
  153. }
  154. }