• Sign in
 
  •  
  • MSDN Blogs
  • Microsoft Blog Images
  • More ...

  • Blog Home
  • About
  • Email Blog Author
  • Share this
  • RSS for posts
  • Atom
  • RSS for comments
  • CDO (25)
  • Code Snippet (43)
  • Custom Providers (17)
  • Debugging (7)
  • DevMsgTeam (303)
  • Documentation (111)
  • DST (8)
  • EWS (7)
  • Exchange (110)
  • Gotchas (98)
  • Hotfix (28)
  • MAPI (241)
  • MAPI Download (55)
  • MFCMAPI (102)
  • MSDN (59)
  • Non Dev (11)
  • OOM (17)
  • Outlook (171)
  • Outlook 2007 Auxiliary Reference (45)
  • Outlook Integration API (12)
  • Protocol Docs (20)
  • PST/OST (23)
  • Referrals (8)
  • Vista (12)
  • WrapPST (18)
Links:
  • Download MFCMAPI
  • MFCMAPI on Facebook
  • Troubleshooting Outlook Crashes
  • Office Update Center
  • Developer Messaging Team Blog
This site is provided "AS IS" with no warranties, and confers no rights. Use of included code samples are subject to the terms specified in the Terms of Use.
Archives
  • May 2013 (5)
  • April 2013 (1)
  • March 2013 (2)
  • February 2013 (2)
  • January 2013 (2)
  • December 2012 (4)
  • November 2012 (2)
  • October 2012 (2)
  • September 2012 (1)
  • August 2012 (3)
  • June 2012 (2)
  • May 2012 (1)
  • April 2012 (3)
  • March 2012 (3)
  • February 2012 (3)
  • January 2012 (1)
  • December 2011 (3)
  • November 2011 (1)
  • October 2011 (3)
  • September 2011 (1)
  • August 2011 (1)
  • July 2011 (4)
  • June 2011 (3)
  • May 2011 (3)
  • April 2011 (3)
  • March 2011 (5)
  • February 2011 (1)
  • January 2011 (2)
  • December 2010 (1)
  • November 2010 (4)
  • October 2010 (1)
  • September 2010 (3)
  • August 2010 (5)
  • July 2010 (3)
  • June 2010 (3)
  • May 2010 (1)
  • April 2010 (3)
  • March 2010 (3)
  • February 2010 (3)
  • January 2010 (2)
  • December 2009 (3)
  • November 2009 (5)
  • October 2009 (4)
  • September 2009 (5)
  • August 2009 (5)
  • July 2009 (11)
  • June 2009 (6)
  • May 2009 (5)
  • April 2009 (3)
  • March 2009 (18)
  • February 2009 (10)
  • January 2009 (3)
  • December 2008 (2)
  • November 2008 (2)
  • October 2008 (5)
  • September 2008 (4)
  • August 2008 (10)
  • July 2008 (6)
  • June 2008 (8)
  • May 2008 (2)
  • April 2008 (4)
  • March 2008 (2)
  • February 2008 (2)
  • January 2008 (5)
  • December 2007 (3)
  • November 2007 (2)
  • October 2007 (3)
  • September 2007 (1)
  • August 2007 (4)
  • July 2007 (5)
  • June 2007 (3)
  • May 2007 (4)
  • April 2007 (1)
  • March 2007 (6)
  • February 2007 (3)
  • January 2007 (2)
  • December 2006 (4)
  • November 2006 (3)
  • October 2006 (1)
  • August 2006 (1)
  • June 2006 (5)
  • May 2006 (5)
  • December 2005 (1)
  • November 2005 (4)
  • October 2005 (2)
  • September 2005 (1)
  • April 2005 (3)
  • December 2004 (2)
  • September 2004 (2)
  • August 2004 (3)
  • July 2004 (3)

MAPI Multithreading Rules

MSDN Blogs > SGriffin's MAPI Internals > MAPI Multithreading Rules

MAPI Multithreading Rules

Stephen Griffin - MSFT
24 Sep 2004 2:53 PM
  • Comments 34

I wrote these rules out while debugging a crash in another MS product:

  1. All threads which use MAPI must call MAPIInitialize before doing any MAPI.
  2. All threads which use MAPI must call MAPIUninitialize before ending.
  3. No thread should ever call MAPIUninitialize if it didn't already call MAPIInitialize.
  4. If MAPI_MULTITHREAD_NOTIFICATIONS is not used, the first thread to call MAPIInitialize should live longer than all other MAPI threads and should be the last to call MAPIUninitialize.

I won't name the app, but it violated all 4 rules.

Known consequences of violating these rules:

  1. MAPI just can't work on a thread that hasn't been initialized. All MAPI calls will fail.
  2. Memory leak.
  3. Either a crash or 'mysterious' errors. Thread A initializes MAPI and starts doing some work. Thread B then calls MAPIUninitialize. If no other thread has initialized MAPI, then MAPI assumes everyone is done with it and cleans up. Depending on the timing, thread A will either crash or start getting failures in MAPI calls.
  4. When MAPI_MULTITHREAD_NOTIFICATIONS is not used, MAPI assumes it can tie the notification thread to the thread on which MAPIInitialize was first called. When this thread goes away, a pointer kept by the notification thread is invalidated, leading to a crash the next time we process a notification.
  • 34 Comments
Exchange, Outlook, MAPI, Gotchas, DevMsgTeam
Comments
  • David Lowndes
    20 Nov 2009 8:57 AM

    Someone - who? We know our code and can't think of anything the worker threads do that would automatically get MAPI initialised.

    I don't think luck comes into it. We're not being very lucky with this project. ;)

    Can you elaborate on the contention we're seeing inside the MAPI calls on our worker threads? Is that normal in any multi-threaded application's heavy use of MAPI?

  • David Lowndes
    23 Nov 2009 6:45 AM

    Hi Stephen,

    In doubting my sanity over this issue I've subsequently run our gateway under debug with a breakpoint set on _MAPIInitialize@4. It's hit once - from the service initialisation message processing loop. I'm as certain as I can be that there are no per-thread calls to MAPIInitialize. Nevertheless we don't experience any issues making MAPI calls from the worker threads.

    Are there perhaps significant differences between the threading/MAPIInitialize requirements of the server version (Exchange 2003) of MAPI and the client version of MAPI?

    Also, we've tried explicitly calling MAPIInitialize from the worker threads and while it hasn't caused any issues, it's not had any noticable effect on the contention we see inside MAPI calls. BTW, the parallel stacks debugging view in VS2010 is very illuminating for these situations.

  • Stephen Griffin - MSFT
    23 Nov 2009 7:02 AM

    The contention has nothing to do with initializing MAPI (though you should still initialize it on every thread). The contention is because many operations in MAPI and in the ems providers are serialized.

  • Renald
    18 Dec 2009 7:25 AM

    Hi Stephen,

    First, good job with this blog.

    I have a problem with my MAPI program (writen in C++.NET).

    My program is a service, so I initialize MAPI with MAPI_NO_COINIT and MAPI_NT_SERVICE flags.

    But MAPIInitialize returns me the error 0x80004005.

    I wrote a second program (console) that make a MAPIInitialize but with MAPI_NO_COINIT flag only.

    MAPI has initialized with no problem.

    Do you know if it could be a problem of multi processor?

    And if I remove the flag MAPI_NT_SERVICE in my service program, what could be the consequently?

    Environment (new computer, office installed only):

    XP SP2

    Office 2003 v11.0.8173.0

    Administrator of my computer

    Thank you

    Renald

Page 3 of 3 (34 items) 123
Leave a Comment
  • Please add 3 and 4 and type the answer here:
  • Post
  • © 2013 Microsoft Corporation.
  • Terms of Use
  • Trademarks
  • Privacy & Cookies
  • Report Abuse
  • 5.6.426.415