Using CDO and MAPI in the Same Modules

Topic Last Modified: 2006-06-11

The C programming language does not support a namespace mechanism. Therefore, all linkage symbols for a particular executable or dynamic-link library (DLL) must be unique; if they are not, the linker will raise an error at link time. You must make sure that type names used in various headers are not the same; if they are, you must perform a little compiler magic to make things work.

One such name class occurs if you attempt to write a C program that uses both MAPI and CDO for Exchange 2000 Server (CDOEX). The MAPI header file mapidefs.h and the CDOEX header file cdoex.h both contain a definition for an interface called IMessage. Of course, the MAPI IMessage interface is quite different from the definition for CDOEX. If you must write C code that uses both MAPI and CDOEX in the same module, you need to re-map names as shown in the following example:

#define IMessage ICDOMessage 
#define IMessageVtbl ICDOMessageVtbl 
#define IID_IMessage IID_ICDOMessage 
#include "cdoex.h" 
#include "cdoex_i.c" 

#undef IID_IMessage 
#undef IMessageVtbl 
#undef IMessage 

#include "mapidefs.h" 
#include "mapiguid.h" 

Then, when you refer to the CDO IMessage interface, you must refer to it as ICDOMessage and refer to its IID as IID_ICDOMessage, as shown in the following example:

#pragma comment(lib,"uuid.lib")
#pragma comment(lib,"ole32.lib")
#pragma comment(lib,"oleaut32.lib")

int main() {

  MAPIInitialize(...);

 ICDOMessage* pMsg = NULL;
 HRESULT hr = CoCreateInstance(
     CLSID_Message,
     NULL,
     CLSCTX_INPROC_SERVER,
     IID_ICDOMessage,
     (void**)&pMsg
     );

 MAPILogonEx(...); // for brevity
 // ...
 MAPIUninitialize();
}