How to: Connect to a Message Store

Before you can create and manipulate message folders and message items, you must establish a connection to a message store.

To open a connection to a message store

  1. Initialize the MAPI subsystem, and log onto a MAPI session. For more information, see How to: Begin a MAPI Session.

  2. Declare a NULL IMAPITable interface object, and get the message stores table by using IMAPISession::GetMsgStoresTable, as follows:

    hr = pSession->GetMsgStoresTable(0, &pTable);
    
  3. Declare a NULL SRowSet structure, and populate it with property values from one of the message stores by using IMAPITable::QueryRows. The following code retrieves the first (default) message store:

    hr = pTable->QueryRows(1, 0, &pSRowSet);
    
  4. Declare a NULL IMsgStore interface object, and open the message store by using IMAPISession::OpenMsgStore — making use of the SRowSet structure:

    hr = pSession->OpenMsgStore(0, 
                   pSRowSet->aRow[0].lpProps[0].Value.bin.cb, 
                   (ENTRYID *)pSRowSet->aRow[0].lpProps[0].Value.bin.lpb, 
                   NULL, 
                   0, 
                   &pStore);
    
  5. If no longer needed, release the message stores table by calling IUnknown::Release on the table object, and free the memory allocated for the row set structure by calling FreeProws:

    pTable->Release();
    FreeProws(pSRowSet);
    

Example

The following code demonstrates how to connect to a message store.

HRESULT hr;
ICEMAPISession * pSession = NULL;
IMAPITable * pTable = NULL;
SRowSet * pSRowSet = NULL;
IMsgStore * pStore = NULL;

// See the code example in "How to: Begin a MAPI Session" for 
// information on obtaining pSession.
hr = pSession->GetMsgStoresTable(0, &pTable);
if (hr != S_OK) {
    // GetMsgStoresTable failed.
    MessageBox(NULL, 
               _T("GetMsgStoresTable failed."),
               _T("Warning"), 
               MB_OK);
    exit(0);  // Replace with specific error handling.
}

BOOL QRError = FALSE;
hr = pTable->QueryRows(1, 0, &pSRowSet);
if (hr != S_OK) {
    QRError = TRUE;
} else if (pSRowSet->cRows != 1) {
    QRError = TRUE;
} else if ((pSRowSet->aRow[0].cValues < 1) 
          || (pSRowSet->aRow[0].lpProps[0].ulPropTag != PR_ENTRYID)) {
    QRError = TRUE;
}
if (QRError) {
    // QueryRows failed.
    MessageBox(NULL, 
               _T("QueryRows failed."),
               _T("Warning"), 
               MB_OK);
    exit(0);  // Replace with specific error handling.
}

hr = pSession->OpenMsgStore(0, 
               pSRowSet->aRow[0].lpProps[0].Value.bin.cb, 
               (ENTRYID *)pSRowSet->aRow[0].lpProps[0].Value.bin.lpb, 
               NULL, 
               0, 
               &pStore);
if (hr != S_OK) {
    // OpenMsgStore failed.
    MessageBox(NULL, 
               _T("OpenMsgStore failed."),
               _T("Warning"), 
               MB_OK);
    exit(0);  // Replace with specific error handling.
}

pTable->Release();
FreeProws(pSRowSet);
pTable = NULL;
pSRowSet = NULL;

See Also

Messaging

Messaging Overview

How to: Begin a MAPI Session

How to: Create a Message

How to: Send a Message

How to: End a MAPI Session

Messaging Sample Code

Last updated on Friday, April 22, 2005

© 2005 Microsoft Corporation. All rights reserved.

Send feedback on this topic to the authors.