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.

241 lines
5.3 KiB

  1. #include "faxmapip.h"
  2. #pragma hdrstop
  3. LIST_ENTRY ProfileListHead;
  4. CRITICAL_SECTION CsProfile;
  5. DWORD MapiServiceThreadId;
  6. VOID
  7. MapiServiceThread(
  8. LPVOID EventHandle
  9. );
  10. BOOL
  11. MyInitializeMapi(
  12. );
  13. BOOL
  14. InitializeEmail(
  15. VOID
  16. )
  17. {
  18. HANDLE MapiThreadHandle;
  19. HANDLE MapiInitEvent;
  20. InitializeListHead( &ProfileListHead );
  21. InitializeCriticalSection( &CsProfile );
  22. //
  23. // create an event for the service thread to set after it has initialized mapi
  24. //
  25. MapiInitEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
  26. if (!MapiInitEvent) {
  27. DebugPrint(( TEXT("InitializeEmailRouting(): CreateEvent() failed: err = %d\n"), GetLastError ));
  28. return FALSE;
  29. }
  30. MapiThreadHandle = CreateThread(
  31. NULL,
  32. 1024*100,
  33. (LPTHREAD_START_ROUTINE) MapiServiceThread,
  34. (LPVOID) MapiInitEvent,
  35. 0,
  36. &MapiServiceThreadId
  37. );
  38. if (MapiThreadHandle == NULL) {
  39. DebugPrint(( TEXT("InitializeEmailRouting(): Cannot create MapiServiceThread thread") ));
  40. return FALSE;
  41. }
  42. CloseHandle( MapiThreadHandle );
  43. //
  44. // wait for MAPI to initialize
  45. //
  46. if (WaitForSingleObject( MapiInitEvent, MILLISECONDS_PER_SECOND * 60) != WAIT_OBJECT_0) {
  47. DebugPrint(( TEXT("InitializeEmailRouting(): WaitForSingleObject failed - ec = %d"), GetLastError() ));
  48. return FALSE;
  49. }
  50. CloseHandle( MapiInitEvent);
  51. return TRUE;
  52. }
  53. PPROFILE_INFO
  54. FindProfileByName(
  55. LPCTSTR ProfileName
  56. )
  57. {
  58. PLIST_ENTRY Next;
  59. PPROFILE_INFO ProfileInfo = NULL;
  60. if (ProfileName == NULL) {
  61. return NULL;
  62. }
  63. EnterCriticalSection( &CsProfile );
  64. Next = ProfileListHead.Flink;
  65. if (Next) {
  66. while (Next != &ProfileListHead) {
  67. ProfileInfo = CONTAINING_RECORD( Next, PROFILE_INFO, ListEntry );
  68. Next = ProfileInfo->ListEntry.Flink;
  69. if (_tcscmp( ProfileInfo->ProfileName, ProfileName ) == 0) {
  70. LeaveCriticalSection( &CsProfile );
  71. return ProfileInfo;
  72. }
  73. }
  74. }
  75. LeaveCriticalSection( &CsProfile );
  76. return NULL;
  77. }
  78. extern "C"
  79. LPCWSTR WINAPI
  80. GetProfileName(
  81. IN LPVOID ProfileInfo
  82. )
  83. {
  84. return ((PPROFILE_INFO)ProfileInfo)->ProfileName;
  85. }
  86. extern "C"
  87. LPVOID WINAPI
  88. AddNewMapiProfile(
  89. LPCTSTR ProfileName,
  90. BOOL UseMail,
  91. BOOL ShowPopUp
  92. )
  93. {
  94. PPROFILE_INFO ProfileInfo = NULL;
  95. if (!MapiIsInitialized) {
  96. return NULL;
  97. }
  98. if (ProfileName == NULL) {
  99. return NULL;
  100. }
  101. ProfileInfo = FindProfileByName( ProfileName );
  102. if (ProfileInfo) {
  103. return ProfileInfo;
  104. }
  105. EnterCriticalSection( &CsProfile );
  106. ProfileInfo = (PPROFILE_INFO) MemAlloc( sizeof(PROFILE_INFO) );
  107. if (ProfileInfo) {
  108. // put the profile name into ProfileInfo and create an event for
  109. // DoMapiLogon to set when its call to MapiLogonEx has completed
  110. _tcscpy( ProfileInfo->ProfileName, ProfileName );
  111. ProfileInfo->EventHandle = CreateEvent( NULL, FALSE, FALSE, NULL );
  112. ProfileInfo->UseMail = UseMail;
  113. if(ProfileInfo->EventHandle != NULL) {
  114. // post a message to the mapi service thread
  115. PostThreadMessage( MapiServiceThreadId, WM_MAPILOGON, 0, (LPARAM) ProfileInfo );
  116. // wait for the logon to complete
  117. if (WaitForSingleObject( ProfileInfo->EventHandle, INFINITE) != WAIT_OBJECT_0) {
  118. DebugPrint(( TEXT("AddNewMapiProfile - WaitForSingleObject failed - ec = %d"), GetLastError() ));
  119. ProfileInfo->Session = NULL;
  120. }
  121. CloseHandle( ProfileInfo->EventHandle );
  122. if (!ProfileInfo->Session) {
  123. DebugPrint(( TEXT("DoMapiLogon() failed: [%s] 0x%08x"), ProfileName, GetLastError() ));
  124. MemFree( ProfileInfo );
  125. ProfileInfo = NULL;
  126. if (ShowPopUp) {
  127. ServiceMessageBox(
  128. GetString( IDS_NO_MAPI_LOGON ),
  129. MB_OK | MB_ICONEXCLAMATION | MB_SETFOREGROUND,
  130. TRUE,
  131. NULL,
  132. ProfileName[0] ? ProfileName : GetString( IDS_DEFAULT )
  133. );
  134. }
  135. } else {
  136. InsertTailList( &ProfileListHead, &ProfileInfo->ListEntry );
  137. }
  138. }
  139. }
  140. LeaveCriticalSection( &CsProfile );
  141. return ProfileInfo;
  142. }
  143. VOID
  144. MapiServiceThread(
  145. LPVOID EventHandle
  146. )
  147. /*++
  148. Routine Description:
  149. Initializes MAPI and services messages. MAPI/OLE create windows under the
  150. covers.
  151. Arguments:
  152. EventHandle - Event to set once MAPI is initialized.
  153. Return Value:
  154. NONE
  155. --*/
  156. {
  157. BOOL Result;
  158. Result = MyInitializeMapi();
  159. SetEvent((HANDLE) EventHandle);
  160. if (!Result) {
  161. return;
  162. }
  163. while (TRUE) {
  164. MSG msg;
  165. Result = GetMessage( &msg, NULL, 0, 0 );
  166. if (Result == (BOOL) -1) {
  167. DebugPrint(( TEXT("GetMessage returned an error - ec = %d"), GetLastError() ));
  168. return;
  169. }
  170. if (Result) {
  171. if (msg.message == WM_MAPILOGON) {
  172. DoMapiLogon( (PPROFILE_INFO) msg.lParam );
  173. } else {
  174. DispatchMessage( &msg );
  175. }
  176. }
  177. }
  178. return;
  179. }