Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

INFO: MAPI_NO_COINIT Flag To Make MAPIInitialize Not Call CoInit


View products that this article applies to.

This article was previously published under Q239853

↑ Back to the top


Summary

If CoInitializeEx is called with the COINIT_MULTITHREADED flag before MAPIInitialize is called, you will get error 0x80010106 (E_INVALID_FLAGS or RPC_E_CHANGED_MODE). To prevent this error, set MAPI_NO_COINIT (value of 0x8) in the ulFlags member of the MAPIINIT_0 structure passed in MAPIInitialize.

Note The MAPI_NO_COINIT flag was added to MAPI in Microsoft Exchange Server 5.5 SP1.

↑ Back to the top


More information

MAPI, by default, will try to initialize COM with a call to CoInitialize. This initializes COM with a single threaded apartment model. Since COM has already been initialized with a multithreaded model and the threading model cannot be changed, MAPIInitialize will fail and return RPC_E_CHANGED_MODE.

If a MAPIINIT_0 structure is passed into MAPIInitialize with ulFlags set to MAPI_NO_COINIT, MAPI will assume that COM has already been initialized and bypass the call to CoInitialize.

As stated before, this flag was added to MAPI in Exchange 5.5 SP1. Attempting to set this flag on earlier versions of MAPI will result in MAPI_E_UNKNOWN_FLAGS, as this flag is not known to previous versions of MAPI.

Steps to Reproduce Behavior

The following code will show the error code and how to use MAPI_NO_COINIT to avoid it:
   //Link in MAPI32.LIB to build this code.
   #define _WIN32_DCOM
   #include <stdio.h>
   #include <conio.h>
   #include <mapix.h>

struct StartOle {
StartOle() { 
      CoInitializeEx(NULL,
         COINIT_MULTITHREADED); 
   }
   ~StartOle() { 
      CoUninitialize(); 
   }
} _inst_StartOle;

   #define MAPI_NO_COINIT 8

void main()
{
   HRESULT hRes;

   MAPIINIT_0 MAPIInit;
   MAPIInit.ulFlags = 
//      MAPI_NO_COINIT | 
      0;
   MAPIInit.ulVersion = MAPI_INIT_VERSION;
   hRes = MAPIInitialize(&MAPIInit);

   MAPIUninitialize();
   if (FAILED(hRes))
   {
      printf("Failed with hRes of %x\n",hRes);		
   }
   printf("Hit any key to continue\n");
   while(!_kbhit()){ Sleep(500);};
	
}
				
Uncomment the line with MAPI_NO_COINIT will clear the error.

↑ Back to the top


References

For additional information about a related fix, please click the article number below in the Microsoft Knowledge Base:
179116� FIX: MAPIInitialize() Fails with MAPI_E_INVALID_FLAGS

↑ Back to the top


Keywords: KB239853, kbmsg, kbinfo

↑ Back to the top

Article Info
Article ID : 239853
Revision : 5
Created on : 10/15/2009
Published on : 10/15/2009
Exists online : False
Views : 618