Share via


Connecting to a Message Store

Send Feedback

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);
    

Code Example

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

Note   To make the following code example easier to read, security checking and error handling are not included. This code example should not be used in a release configuration unless it has been modified to include them.

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

How to Create a Messaging Application | Messaging Application Development for Windows Mobile-based Devices | Messaging | 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

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.