Microsoft KB Archive/285999

= How to build a minimal Exchange Client Extension with VC++ =

Article ID: 285999

Article Last Modified on 2/12/2007

-

APPLIES TO


 * Microsoft Exchange Development Kit 5.5
 * Microsoft Exchange Server 2003 Software Development Kit
 * Microsoft Messaging Application Programming Interface
 * Microsoft Outlook 97 Standard Edition
 * Microsoft Outlook 98 Standard Edition
 * Microsoft Outlook 2000 Standard Edition
 * Microsoft Outlook 2002 Standard Edition
 * Microsoft Office Outlook 2003
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q285999



INTRODUCTION
This article demonstrates the steps required to build a minimal Microsoft Exchange Client Extension dynamic-link library (DLL) with Visual C++.



MORE INFORMATION
Outlook supports Exchange Client Extensions, which are used to host Messaging Application Programming Interface (MAPI), Collaboration Data Objects (CDO), and Outlook Object Model code.

To write a very simple Client Extension DLL, the developer declares a class that inherits from the IExchExt interface and implements the Install method of that interface. The developer also implements the ExchEntryPoint function and the DllMain function. The DLL exports the ExchEntryPoint function, either with a Define (.def) file or by declaration with the __declspec(dllexport) macro.

Step-by-step:
 In Developer Studio, create a new Win32 DLL project. For this example, place all the code in one .cpp file, and save the file as SimplestExt.cpp.  Include the following header files. Note The Exchext.h file contains all of the Client Extension Architecture definitions for your Extension DLL to use. It is installed with Visual C++, and can be found in the Visual C++ Include folder.  On the menu bar, click Project, click Settings, click the Link tab, and then add Mapi32.lib to the list of Object/Library modules.  Add the Extern Declaration for the ExchEntryPoint callback function. extern &quot;C&quot; _declspec(dllexport) LPEXCHEXT CALLBACK ExchEntryPoint(void);   Declare the Extension Object Class. To serve as an Extension Object, this class at minimum must singly inherit from the IExchExt interface, which contains the Install method that Outlook uses to obtain a new instance of the Extension Object Class. Also, because the IExchExt interface inherits from IUnknown, Component Object Model (COM) rules require that this class declare and implement the QueryInterface, AddRef and Release methods.
 * 1) include 
 * 2) include 
 * 3) include 
 * 4) include <MAPIX.H>
 * 5) include <MAPIUTIL.H>
 * 6) include <MAPIFORM.H>
 * 7) include <INITGUID.h>
 * 8) include <EXCHEXT.H>

In this example, the IUnknown methods are implemented inline to demonstrate the technique, although this is not necessary. //Declarations class CMyExt:public IExchExt { public: CMyExt { refcount = 0;}; ~CMyExt{};

//Methods of IUnknown inline STDMETHODIMP QueryInterface(REFIID riid,void** ppvObj) {      *ppvObj=NULL; IUnknown* punk=NULL; if ((IID_IUnknown == riid) || (IID_IExchExt ==riid)) {        punk=(IExchExt*) this; }     else return E_NOINTERFACE;

if (NULL!=punk) {        *ppvObj = punk; AddRef; }     return S_OK; }

inline STDMETHODIMP_(ULONG) AddRef {     refcount++; return refcount; }

inline STDMETHODIMP_(ULONG) Release {     ULONG ulCount = refcount--; if (!ulCount) {        delete this; }     return ulCount; }

//This is one method of IExchExt. STDMETHODIMP Install(     IExchExtCallback *pmecb,      ULONG mcontext,      ULONG ulFlags);

private: ULONG refcount; };                   </li>  Implement the DllMain function. When the program design calls for specific initialization work to happen just after the DLL loads, it occurs in this function. //implementation

BOOL WINAPI DllMain(  HINSTANCE  hinstDLL,   DWORD  fdwReason,      LPVOID  lpvReserved) {  return TRUE; }                   </li>  Implement the ExchEntryPoint function. The sole purpose of ExchEntryPoint is to return a new instance of the Extension Interface to Outlook or Exchange. LPEXCHEXT CALLBACK ExchEntryPoint {  return new CMyExt; }                   </li>  Implement the Install method. Place code here that evaluates the current context. STDMETHODIMP CMyExt::Install(  IExchExtCallback *pmecb,   ULONG mcontext,   ULONG ulFlags) {  char buffer[12]; itoa(mcontext,buffer,10); MessageBox(NULL,buffer,&quot;Test Extension&quot;,MB_OK); return S_OK; }                   </li> Build the DLL and copy it to the same folder as Outlook.exe.</li> Register the DLL under the following registry key:

HKEY_LOCAL_MACHINE\Software\Microsoft\Exchange\Client\Extensions

with a string value named TestExtension and the following string in the Data field:

4.0;SimplestExt.DLL;1

</li> Start Outlook. Note that the extension DLL posts a message box immediately after Outlook loads.</li> To investigate further, perform some normal tasks in Outlook, and observe which tasks cause Outlook to call the Extension Object Install method. As an exercise, compare the context number in the message box with the constant names that are defined in the Exchext.h file.</li> As a debugging aid, Outlook can be specified as the debug executable for the extension project, which allows direct debugging into the extension code.</li></ol>

<div class="references_section">