Calling MAPI from Windows Services
Last modified: October 09, 2010
Applies to: Office 2010 | Outlook 2010 | Visual Studio
To enable MAPI client applications that are written as Windows services to operate with MAPI-compliant service providers, MAPI imposes several limitations and requirements.
MAPI clients have the following limitations:
They cannot allow a user interface.
They can send messages only through a tightly coupled message store and transport provider. In addition, MAPI clients can send and receive messages by using only the Microsoft Exchange Server or another server-based transport provider. Because of identity and security issues between client applications and the MAPI spooler, most transport providers are not supported in a service.
All MAPI client applications, whether they are implemented as Windows services, must call the MAPIInitialize function to initialize the MAPI libraries. A call to the OleInitialize function is also necessary to use the OLE libraries. Both MAPIInitialize and OleInitialize make calls to the CoInitialize function to initialize the Component Object Model (COM) libraries. Clients that are services must set a special flag, MAPI_NT_SERVICE, in the ulFlags member of the MAPIINIT_0 structure that is passed to MAPIInitialize and in the ulFlags parameter that is passed to the MAPILogonEx function to inform MAPI of their special implementation.
MAPI clients that are written as Windows services and written with the MAPI client interface have an additional requirement. They must set the MAPI_NO_MAIL flag in the call to MAPILogonEx. Other types of clients do not have to set a flag for logon because it is automatically set by MAPI.
To handle messages in an initialization thread, a MAPI client that is implemented as a service does the following:
Calls the MsgWaitForMultipleObjects function when the main thread blocks.
Calls the GetMessage, TranslateMessage, and DispatchMessage sequence of Windows functions to handle the message when MsgWaitForMultipleObjects returns the sum of the value of the nCount parameter and the value of WAIT_OBJECT_0, which indicates that a message is in the queue.