ADXMAPIStoreAccessor.Initialize(false) throws error in console application, but not in add-in

Add-in Express™ Support Service
That's what is more important than anything else

ADXMAPIStoreAccessor.Initialize(false) throws error in console application, but not in add-in
 
Scott English




Posts: 27
Joined: 2010-01-12
I have a problem where ADXMAPIStoreAccessor.Initialize(false) throws error from a console application, but not an Add-In Express Outlook add-in.

The code for my console application is:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            AddinExpress.MAPI.ADXMAPIStoreAccessor _mapi = new AddinExpress.MAPI.ADXMAPIStoreAccessor();
            _mapi.Initialize(false);
        }
    }
}


When I run it (whether Outlook is already running or not) I get the following error:

Detailed technical information follows:
---
Date and Time: 6/14/2010 11:24:38 AM
Machine Name: SCOTTSDT
IP Address: 352430272
Current User: INTERNAL\ScottEnglish

Application Domain: ConsoleApplication1.vshost.exe
Assembly Codebase: file:///C:/Documents and Settings/ScottEnglish/Desktop/OutlookAgent/ConsoleApplication1/bin/Debug/ConsoleApplication1.EXE
Assembly Full Name: ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Assembly Version: 1.0.0.0
Assembly Build Date: 6/14/2010 11:21:09 AM

Exception Source: AddinExpress.MAPI
Exception Type: AddinExpress.MAPI.NotInitializedException
Exception Message: MAPI subsystem didn't initialize correctly.
Exception Target Site: MAPIWrapper.Initialize
Object reference not set to an instance of an object.

However, the same call works in an Add-in Express add-in. I create an add-in from the wizard and then add the following event handler:


        private void AddinModule_AddinStartupComplete(object sender, EventArgs e)
        {
            AddinExpress.MAPI.ADXMAPIStoreAccessor _mapi = new AddinExpress.MAPI.ADXMAPIStoreAccessor();
            _mapi.Initialize(false);
        }


This runs without error.

My setup:
Windows XP Pro SP3
Outlook 2007 SP2
Add-in Express 2009 for .NET, Professional
Add-in Express MAPI Accessor for .NET
Posted 14 Jun, 2010 11:39:57 Top
Eugene Astafiev


Add-in Express team


Posts: 8540
Joined: 2007-07-25
Hi Scott,

There is no existing mapi session. You need to pass true to the Initialize method.
.NET, Extended MAPI, Windows Phone & RT
Posted 15 Jun, 2010 04:41:25 Top
Scott English




Posts: 27
Joined: 2010-01-12
I tried that as well. It has no effect.
Posted 15 Jun, 2010 09:00:04 Top
Eugene Astafiev


Add-in Express team


Posts: 8540
Joined: 2007-07-25
Hi Scott,

I have reproduced the issue you described. Thank you for pointing this to me.

FYI I can't reproduce it in a windows forms application.
.NET, Extended MAPI, Windows Phone & RT
Posted 15 Jun, 2010 10:15:09 Top
Scott English




Posts: 27
Joined: 2010-01-12
I have also run into this problem where an add-in uses the MAPI Store Accessor successfully, but dll it calls that uses the accessor encounters this error.
Posted 15 Jun, 2010 10:18:42 Top
Scott English




Posts: 27
Joined: 2010-01-12
You said it worked in a windows forms application, which got me thinking about what the difference might be. If I add the [STAThread] attribute to Program.Main, the it works as expected, so it seems to be an issue with COM threading.

The problem I have is that we need to be able initialize and use the component on a background worker thread, and it doesn't work there. If you create a windows form with a background worker and initialize the MAPI Accessor in the DoWork event handler, it fails. For example,


        private void btnBackground_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            AddinExpress.MAPI.ADXMAPIStoreAccessor _mapi = new AddinExpress.MAPI.ADXMAPIStoreAccessor();
            _mapi.Initialize(false); // <-- throws error that is caught by background worker.
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
                throw e.Error; // <-- Rethrows the error that was caught by the background worker, so that we can see what it was.
        }


In our case, the MAPI Accessor will be used in a low-level class library that, under some circumstances, gets called by background worker threads or by other threads whose creation the library can't control.
Posted 15 Jun, 2010 15:22:58 Top
Eugene Astafiev


Add-in Express team


Posts: 8540
Joined: 2007-07-25
Hi Scott,

Sorry for the delay in my response. I spent a lot of time at a hospital. But now I am back in the army that is called Add-in Express ;-)

I have found the cause of the issue! The detailed info about the issue is listed on the following forum thread. The MAPI_NO_COIN flag did the trick :-)

Tomorrow I will prepare a fresh build of the MAPI Store Accessor with a hotfix for you.
.NET, Extended MAPI, Windows Phone & RT
Posted 01 Jul, 2010 10:43:39 Top
Eugene Astafiev


Add-in Express team


Posts: 8540
Joined: 2007-07-25
Hi Scott,

I created a new build the MAPI Store Accessor which fixes the issue and have just sent you an e-mail with a download link. Please let me know whether it helps.
.NET, Extended MAPI, Windows Phone & RT
Posted 02 Jul, 2010 06:28:24 Top
Scott English




Posts: 27
Joined: 2010-01-12
Thanks. I got your link and downloaded the build, but I won't have time to test it for a day or two. I'll let you know the results when I do.
Posted 02 Jul, 2010 09:07:47 Top
Eugene Astafiev


Add-in Express team


Posts: 8540
Joined: 2007-07-25
Sure:-) You are welcome, Scott.
.NET, Extended MAPI, Windows Phone & RT
Posted 02 Jul, 2010 09:31:39 Top