How to open a message using Outlook object model with Entry ID obtained from MAPI in Visual C++ 6.0 (304894)



The information in this article applies to:

  • Microsoft Extended Messaging Application Programming Interface (MAPI)
  • Microsoft Outlook 2000
  • Microsoft Outlook 2000, Service Release 1 (SR-1)
  • Microsoft Outlook 2002
  • Microsoft Visual Studio, Enterprise Edition 6.0

This article was previously published under Q304894

INTRODUCTION

This article demonstrates how to use the PR_ENTRYID property obtained from extended MAPI to open and display an item using the Outlook object model.

MORE INFORMATION

To create and build this sample project in Microsoft Visual C++, follow these steps:
  1. Open Visual Studio, create a new Win32 Application named "OOMEntryID", and then select A Simple Win32 Application.
  2. Open the OOMEntryID.cpp file and replace its entire contents with the code below. Make sure that you change the path to Mso.dll and Msoutl.olb to point to their respective locations for your computer.
    #include "stdafx.h"
    #include "edk.h"
    
    // Import Outlook object model.
    #define IMPPROPS rename_namespace("OL")
    // Define this according to the Outlook object model 
    // version you are compiling under.
    
    //#define OL2000 // Outlook 2000
    #define OL2002 // Outlook 2002 (2002)
    
    #if defined(OL2000)
      // TODO: Verify that the path to the file is correct.
      #import "c:\Program Files\Microsoft Office\Office\mso9.dll" IMPPROPS 
      #import "c:\Program Files\Microsoft Office\Office\msoutl9.olb" IMPPROPS
    #elif defined(OL2002)
      // TODO: Verify that the path to the file is correct.
      #import "c:\Program Files\Common Files\Microsoft Shared\Office10\mso.dll" IMPPROPS 
      #import "c:\Program Files\Microsoft Office\Office10\\msoutl.olb" IMPPROPS
    #endif
    
    struct StartOle 
    {
       StartOle() { CoInitialize(NULL); }
       ~StartOle() { CoUninitialize(); }
    } _inst_StartOle;
    
    
    void dump_com_error(_com_error &e)
    {
        _tprintf(_T("Oops - hit an error!\n"));
        _tprintf(_T("\a\tCode = %08lx\n"), e.Error());
        _tprintf(_T("\a\tCode meaning = %s\n"), e.ErrorMessage());
        _bstr_t bstrSource(e.Source());
        _bstr_t bstrDescription(e.Description());
        _tprintf(_T("\a\tSource = %s\n"), (LPCTSTR) bstrSource);
        _tprintf(_T("\a\tDescription = %s\n"), (LPCTSTR) bstrDescription);
    }
    
    using namespace OL;
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
      // MAPI session pointer.
      LPMAPISESSION  pSession = NULL; 
      HRESULT hRes = S_OK;
      
      // Please add error checking yourself.
    
      // Initialize MAPI.
      hRes = MAPIInitialize(NULL);
    
      // Log on to MAPI and get a session pointer.
      hRes = MAPILogonEx(0, NULL, NULL, MAPI_LOGON_UI, &pSession);
    
      // Open message store.
      ULONG      cbEIDStore = 0;
      LPENTRYID  lpEIDStore = NULL;
      IMsgStore  *pMsgStore;
    
      hRes = HrMAPIFindDefaultMsgStore(pSession, 
                      &cbEIDStore, 
                      &lpEIDStore);
    
      hRes = pSession->OpenMsgStore(NULL, 
                      cbEIDStore, 
                      lpEIDStore, 
                      NULL, 
                      MAPI_BEST_ACCESS, 
                      &pMsgStore);  
    
      // Open Inbox.
      ULONG        ulObjType;
      ULONG        cbEntryID;
      LPENTRYID    lppEntryID;
      IMAPIFolder  *lpFolder;
    
      hRes = pMsgStore->GetReceiveFolder(NULL, 
                         NULL, 
                         &cbEntryID, 
                         &lppEntryID, 
                         NULL);
    
      hRes = pMsgStore->OpenEntry(cbEntryID, 
                    lppEntryID, 
                    NULL, 
                    MAPI_BEST_ACCESS, 
                    &ulObjType, 
                    (LPUNKNOWN *)&lpFolder);
    
    
      // Get Inbox items.
      LPMAPITABLE  lpContentsTable = NULL;
      LPSRowSet    pRows = NULL;
      enum {ePR_SUBJECT, ePR_ENTRYID,  NUM_COLS};
      static SizedSPropTagArray(NUM_COLS,sptCols) 
      = {NUM_COLS, PR_SUBJECT, PR_ENTRYID};
      
    
      hRes = lpFolder->GetContentsTable(0, 
                        &lpContentsTable);
    
      hRes = HrQueryAllRows(lpContentsTable, 
                  (LPSPropTagArray) &sptCols, 
                  NULL, 
                  NULL, 
                  0, 
                  &pRows);
      
      if (pRows->cRows > 0) // Make sure there is at least one message.
      {
        // Get the first item in the Inbox and get its entry ID.
        IMessage    *lpMessage;
        LPSPropValue  pspvEID = NULL;
    
        hRes = pMsgStore->OpenEntry(pRows->aRow[0].lpProps[ePR_ENTRYID].Value.bin.cb, 
                                 (LPENTRYID)pRows->aRow[0].lpProps[ePR_ENTRYID].Value.bin.lpb, 
                                 NULL, 
                                 MAPI_BEST_ACCESS, 
                                 &ulObjType, 
                                 (LPUNKNOWN *)&lpMessage);
        
        hRes = HrGetOneProp(lpMessage, 
                  PR_ENTRYID, 
                  &pspvEID);
        
        char szEID[1001];
        HexFromBin(pspvEID[0].Value.bin.lpb, 
                   pspvEID[0].Value.bin.cb, 
                   szEID);
    
        // Open Outlook item using the entry ID we got from MAPI.
    
        try{
          OL::_ApplicationPtr  pOutlook("Outlook.Application");
          OL::_NameSpacePtr    pNameSpace = pOutlook->GetNamespace("MAPI");
          OL::_MailItemPtr     pMailItem = NULL;
    
          pMailItem = pNameSpace->GetItemFromID(szEID, vtMissing);
          if (pMailItem)
          {
            // Display message.
            pMailItem->Display(vtMissing);
          }
        } 
    
        catch (_com_error &e)
        {
          dump_com_error(e);
        }
    
        UlRelease(lpMessage);
      }
      else
      {
        MessageBox(NULL, 
               "There are no messages in the inbox", 
               "Open Item", 
               MB_OK);
      }
    
      pSession->Logoff(NULL, 0L, 0);
    
      FreeProws(pRows);
      UlRelease(lpContentsTable);
      UlRelease(lpFolder);
      UlRelease(pMsgStore);
      UlRelease(pSession);
    
      return 0;
    } 
    					
  3. On the Project menu, click Settings. Click the Link tab, and then select Input as the category.

    For the Object/Library modules section, add the following.
    MSVCRT.LIB addrLkup.lib edkguid.lib edkdebug.lib edkmapi.lib edkutils.lib mapi32.lib
    						
    For the Ignore Libraries section, add libcd.lib and libc.lib.

    Close the Project Settings dialog box.
  4. Build and then run the project.
  5. The first message in the Inbox will be displayed.

REFERENCES

For more information, click the following article number to view the article in the Microsoft Knowledge Base:

259298 How to use an Outlook Object Model from Visual C++ by using a #import statement


Modification Type:MinorLast Reviewed:1/13/2006
Keywords:kbemail kbopenfile kbProgramming kbhowto kbMsg KB304894