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.

258 lines
6.1 KiB

  1. /*---------------------------------------------------------------------------
  2. File: ExMapiProf.cpp
  3. Comments: implementation of TMapiProfile and TAddressBook classes
  4. (c) Copyright 1995-1998, Mission Critical Software, Inc., All Rights Reserved
  5. Proprietary and confidential to Mission Critical Software, Inc.
  6. REVISION LOG ENTRY
  7. Revision By: Christy Boles
  8. Revised on 09-Sep-98 09:38:21
  9. ---------------------------------------------------------------------------
  10. */
  11. #include "stdafx.h"
  12. #include "Common.hpp"
  13. #include "Ustring.hpp"
  14. #include <MapiUtil.h> // For HrQueryAllRows
  15. //#include <edkmdb.h>
  16. //#include <proptag.h>
  17. #include <emsabtag.h>
  18. #include <mspab.h>
  19. #include "edk2.hpp"
  20. #include "MAPIProf.hpp"
  21. HWND gHWND = NULL;
  22. HINSTANCE hMapi = NULL;
  23. LPMAPIALLOCATEBUFFER pMAPIAllocateBuffer = NULL;
  24. LPMAPIFREEBUFFER pMAPIFreeBuffer = NULL;
  25. LPMAPIINITIALIZE pMAPIInitialize = NULL;
  26. LPMAPIUNINITIALIZE pMAPIUninitialize = NULL;
  27. LPMAPILOGONEX pMAPILogonEx = NULL;
  28. LPMAPIADMINPROFILES pMAPIAdminProfiles = NULL;
  29. LPFREEPADRLIST pFreePadrlist = NULL;
  30. LPFREEPROWS pFreeProws = NULL;
  31. LPSCDUPPROPSET pScDupPropset = NULL;
  32. LPHRQUERYALLROWS pHrQueryAllRows = NULL;
  33. LPULRELEASE pUlRelease = NULL;
  34. BOOL LoadMAPI()
  35. {
  36. BOOL success = TRUE;
  37. if ( ! hMapi )
  38. {
  39. hMapi = LoadLibrary(_T("MAPI32.DLL"));
  40. if ( hMapi )
  41. {
  42. do { // once (only show one error message)
  43. pMAPIAllocateBuffer=(LPMAPIALLOCATEBUFFER)GetProcAddress(hMapi,"MAPIAllocateBuffer");
  44. if ( ! pMAPIAllocateBuffer )
  45. {
  46. success = FALSE;
  47. break;
  48. }
  49. pMAPIFreeBuffer = (LPMAPIFREEBUFFER)GetProcAddress(hMapi,"MAPIFreeBuffer");
  50. if ( ! pMAPIFreeBuffer )
  51. {
  52. success = FALSE;
  53. break;
  54. }
  55. pMAPIInitialize = (LPMAPIINITIALIZE)GetProcAddress(hMapi,"MAPIInitialize");
  56. if ( ! pMAPIInitialize )
  57. {
  58. success = FALSE;
  59. break;
  60. }
  61. pMAPIUninitialize = (LPMAPIUNINITIALIZE)GetProcAddress(hMapi,"MAPIUninitialize");
  62. if ( ! pMAPIUninitialize )
  63. {
  64. success = FALSE;
  65. break;
  66. }
  67. pMAPILogonEx = (LPMAPILOGONEX)GetProcAddress(hMapi,"MAPILogonEx");
  68. if ( ! pMAPILogonEx )
  69. {
  70. success = FALSE;
  71. break;
  72. }
  73. pMAPIAdminProfiles = (LPMAPIADMINPROFILES)GetProcAddress(hMapi,"MAPIAdminProfiles");
  74. if ( ! pMAPIAdminProfiles )
  75. {
  76. success = FALSE;
  77. break;
  78. }
  79. pFreePadrlist = (LPFREEPADRLIST)GetProcAddress(hMapi,"FreePadrlist@4");
  80. if ( ! pFreePadrlist )
  81. {
  82. success = FALSE;
  83. break;
  84. }
  85. pFreeProws = (LPFREEPROWS)GetProcAddress(hMapi,"FreeProws@4");
  86. if ( ! pFreeProws )
  87. {
  88. success = FALSE;
  89. break;
  90. }
  91. pScDupPropset = (LPSCDUPPROPSET)GetProcAddress(hMapi,"ScDupPropset@16");
  92. if ( ! pScDupPropset )
  93. {
  94. success = FALSE;
  95. break;
  96. }
  97. pHrQueryAllRows = (LPHRQUERYALLROWS)GetProcAddress(hMapi,"HrQueryAllRows@24");
  98. if ( ! pHrQueryAllRows )
  99. {
  100. success = FALSE;
  101. break;
  102. }
  103. pUlRelease = (LPULRELEASE)GetProcAddress(hMapi,"UlRelease@4");
  104. if ( ! pUlRelease )
  105. {
  106. success = FALSE;
  107. break;
  108. }
  109. }
  110. while (FALSE);
  111. }
  112. else
  113. {
  114. success = FALSE;
  115. }
  116. if ( hMapi && !success )
  117. {
  118. ReleaseMAPI();
  119. }
  120. }
  121. return (hMapi!=NULL && success);
  122. }
  123. void ReleaseMAPI()
  124. {
  125. if ( hMapi )
  126. {
  127. FreeLibrary(hMapi);
  128. hMapi = NULL;
  129. pMAPIAllocateBuffer = NULL;
  130. pMAPIFreeBuffer = NULL;
  131. pMAPIInitialize = NULL;
  132. pMAPIUninitialize = NULL;
  133. pMAPILogonEx = NULL;
  134. pMAPIAdminProfiles = NULL;
  135. pFreePadrlist = NULL;
  136. pFreeProws = NULL;
  137. pScDupPropset = NULL;
  138. pHrQueryAllRows = NULL;
  139. pUlRelease = NULL;
  140. }
  141. }
  142. DWORD TMapiProfile::Initialize()
  143. {
  144. DWORD rc = 0;
  145. MAPIINIT_0 MAPIINIT = { 0, MAPI_MULTITHREAD_NOTIFICATIONS};
  146. if ( ! m_bMapiInitialized )
  147. {
  148. rc = (*pMAPIInitialize)(&MAPIINIT);
  149. m_bMapiInitialized = TRUE;
  150. }
  151. return rc;
  152. }
  153. DWORD TMapiProfile::Uninitialize()
  154. {
  155. if ( m_bMapiInitialized )
  156. {
  157. (*pMAPIUninitialize)();
  158. m_bMapiInitialized = FALSE;
  159. }
  160. return 0;
  161. }
  162. DWORD TMapiProfile::Logon(BOOL bMarkForDelete)
  163. {
  164. // start a new session
  165. HRESULT hr;
  166. DWORD rc = 0;
  167. BOOL error = FALSE;
  168. Initialize();
  169. FLAGS fLogonOptions = MAPI_NO_MAIL | MAPI_EXTENDED | MAPI_NEW_SESSION;
  170. WCHAR pszMapiProfile[200] = L"";
  171. if ( m_profileName )
  172. {
  173. safecopy(pszMapiProfile,m_profileName);
  174. fLogonOptions |= MAPI_EXPLICIT_PROFILE;
  175. }
  176. else
  177. {
  178. fLogonOptions |= MAPI_USE_DEFAULT;
  179. }
  180. hr = (*pMAPILogonEx)(0, (WCHAR *)pszMapiProfile, NULL, fLogonOptions | MAPI_UNICODE, &m_session);
  181. if (FAILED(hr))
  182. {
  183. error = TRUE;
  184. }
  185. if ( error )
  186. {
  187. rc = (DWORD)hr;
  188. m_session = NULL;
  189. }
  190. else
  191. {
  192. m_bLoggedOn = TRUE;
  193. }
  194. return rc;
  195. }
  196. DWORD TMapiProfile::Logoff()
  197. {
  198. if ( m_session )
  199. {
  200. // HRESULT hr = m_session->Logoff(0,0,0);
  201. m_session->Logoff(0,0,0);
  202. m_bLoggedOn = FALSE;
  203. m_session=NULL;
  204. }
  205. Uninitialize();
  206. return 0;
  207. }
  208. DWORD
  209. TMapiProfile::SetProfile(TCHAR * profile, BOOL create)
  210. {
  211. DWORD rc=0;
  212. safecopy(m_profileName,profile);
  213. return rc;
  214. }
  215. TMapiProfile::~TMapiProfile()
  216. {
  217. if ( m_bLoggedOn )
  218. {
  219. Logoff();
  220. }
  221. Uninitialize();
  222. }