This topic has not yet been rated - Rate this topic

How to: Link to MAPI Functions

Previously, calling MAPI functions in a messaging application involved linking to the Mapi32.lib library. This includes routing MAPI calls to the Mapi32.dll OS component, which then forwards the calls to the default MAPI client implementation at runtime.

Because the default MAPI client now supports on-demand installation using Windows Installer (MSI), you can develop messaging applications directly on top of the implementation instead of going through Mapi32.lib. The following shows you how to call MAPI functions using explicit linking.

For more information about explicit linking, see Linking Explicitly.

To call MAPI APIs without the MAPI32.lib library

  1. In your program file, create a global array of function pointers for each MAPI API you are using.

    The following example illustrates this step.

    //Global MAPI function pointers
    LPMAPIINITIALIZE pfnMAPIInitialize = NULL;
    LPMAPIUNINITIALIZE pfnMAPIUninitialize = NULL;
    
  2. Create a function that initializes MAPI functions to link to the MAPI DLL (Mapi32.dll). In this function, do the following:

    1. Call the function FGetComponentPath to get the path and DLL name that implements the MAPI subsystem. For more information see How to: Choose a Specific Version of MAPI to Load.

    2. Load the DLL by calling the function LoadLibrary.

    3. Initialize the MAPI function pointer array by calling the function GetProcAddress.

    The following example illustrates the previous steps:

    void InitializeMapiFunctions()
    {
      //Get the DLL path and name of the actual MAPI implementation
      FGetComponentPath(g_szMapiComponentGUID, NULL, szMAPIDLL, MAX_PATH);
    
      //Load the DLL
      hMod = LoadLibrary(szMAPIDLL);
    
      //Initialize MAPI functions
      pfnMAPIInitialize = GetProcAddress(hMod, “MAPIInitialize”);
      pfnMAPIUninitialize = GetProcAddress(hMod, “MAPIUninitialize”);
    }
    
  3. Finally, call the function you created in step 2 in your messaging application before you make calls to MAPI APIs.

    Security noteSecurity Note:

    You must uninitialize the MAPI subsystem before closing your application.

    The following example illustrates this step:

    int main()
    {
      HRESULT hr;
    
      InitializeMapiFunctions();
    
      //Initialize the MAPI subsystem
      hr = (*pfnMAPIInitialize)(NULL);
      if (hr!= S_OK)
      {
        //Handle the error case
      }
      //Here’s where you make calls to other MAPI APIs
    
      //Uninitialize the MAPI subsystem
      (*pfnMAPIUninitialize)();
    return (0);
    }
    

See Also

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.