none
MAPI working when accessed from normal application but not from the Service

    Question

  • I posted this question previously in an incorrect forum, so I apologize that it is a repeat.

    I have created an application that accesses the Exchange Server through MAPI. A Tray app is used to start, stop and configure the application. I have put all of the communication and processing logic into a separate library which processes mail on a timer. When running in Debug mode, the library is accessed directly from the Tray app, but when running in release mode, it is being called by a service installed at the User level. See the diagram below.

    enter image description here

    I have everything configured to run using an x64 bit version of Outlook 2010. The issue I am having is that when I build and install in Debug (access the library directly), everything works properly. If I install the Release build and run as a service, the MAPI connection is not initializing.

    I seem to be accessing the COM object, since I am getting a return value from the method I call rather than an exception. I have set the User account to my own login for the time, so I should have permission to access the profile. I am thinking the issue has more to do with permissions. I have tried running a test install under each of the 4 account types associated with a service, and none of them work. Is it possible that my user account does not have access to my mail profile?

    Below is my initialization code:

    // bMultiThreadedNotifications = false
    // bInitAsService = true (when run as a service)
    
    DWORD dwFlags=0;
    	if(bMultiThreadedNotifications) dwFlags|=MAPI_MULTITHREAD_NOTIFICATIONS;
    	if(bInitAsService) dwFlags|=MAPI_NT_SERVICE;
    
    	if(dwFlags) 
    	{
    		MAPIINIT_0 MAPIInit={ MAPI_INIT_VERSION, dwFlags };
    		if(MAPIInitialize(&MAPIInit)!=S_OK) return FALSE;
    	} 
    	else 
    	{
    		if(MAPIInitialize(NULL)!=S_OK) return FALSE;
    	}

    If anyone has any ideas on what I may be doing wrong, I would appreciate the insight.

    Thursday, March 28, 2013 4:48 PM

Answers

  • What is the actual (integer) error code?

    Sounds like you are initializing COM (CoInitialize), and MAPI attempt to do the same. Try to use the MAPI_NO_COINIT flag.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    • Marked as answer by DarthTim Friday, April 05, 2013 11:05 PM
    Friday, April 05, 2013 7:21 PM

All replies

  • Can you be more specific? What exactly do you mean by "the MAPI connection is not initializing"?

    Do you get a particular error? What is it?


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Thursday, March 28, 2013 5:03 PM
  • Thank you for your reply, Dimitry.

    The call to MAPIInitialize returns a false. There are no exceptions thrown that I can see. Unfortunately, I am not the author of the cpp code; it comes from an open source project called MAPIEx. I connect to it using COM, and since I am getting a return value, I am assuming that the COM connection is happening properly, which means the false is coming from MAPIInitialize.

    Any thoughts that you have would be gratefully accepted.

    Thursday, March 28, 2013 5:56 PM
  • Instead of returning true or false, can you check the actual HRESULT returned by MAPIInitialize?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Friday, March 29, 2013 2:34 PM
  • I modified the code to log the HRESULT value, and found that I am getting the message
    Cannot change thread mode after it is set.

    I was passing in bMultiThreadedNotifications as "false", but tried changing it to true when I discovered the HRESULT message, but I still get the same error whether I set it to true or false. I did a search of the solution to confirm that I don't call MAPIInitialize anywhere else except in this method, and I only call this method. once.
    Friday, April 05, 2013 7:15 PM
  • What is the actual (integer) error code?

    Sounds like you are initializing COM (CoInitialize), and MAPI attempt to do the same. Try to use the MAPI_NO_COINIT flag.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    • Marked as answer by DarthTim Friday, April 05, 2013 11:05 PM
    Friday, April 05, 2013 7:21 PM
  • Thank you for the quick reply. That was the problem. For some reason, MAPI_NO_COINIT couldn't be found, but I set it to 0x8, and the service worked. Thank you very much. This will make my weekend a bit smoother.
    Friday, April 05, 2013 11:05 PM