How To Use Address Type as a Restriction with MAPI (200152)



The information in this article applies to:

  • Microsoft Extended Messaging Application Programming Interface (MAPI)

This article was previously published under Q200152

SUMMARY

Sometimes your application may need to process only a particular type of mailbox (for example, only Custom Recipients or only Exchange Users). This can be done by looking at the PR_DISPLAY_TYPE in the restriction.

This article contains a code sample that demonstrates how to use the PR_DISPLAY_TYPE and the PR_DISPLAY_NAME to retrieve a restricted list of mailboxes.

MORE INFORMATION

  1. Place the following code in a Win32 application project.
  2. On the Link tab, select the Ignore All Default Libraries option.
  3. Use the following libraries to compile the code:

    • Msvcrt.lib
    • Version.lib
    • Mapi32.lib
    • Edkmapi.lib
    • Edkutils.lib
    • Addrlkup.lib
    • Edkdebug.lib
#include <edk.h>

HRESULT GetGALContents (LPMAPISESSION);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR pszCmd, int nCmdShow)

{
   // Load strings, and register window classes.

   LPMAPISESSION psess = NULL;
   HRESULT hr = MAPIInitialize(NULL);
   if (FAILED(hr))
   {
      MessageBox(0L,"Initialize Failed","Error",MB_OK);
      return 1;
   }
    hr = MAPILogonEx(0L,
                     NULL,
                     NULL,
                     MAPI_NEW_SESSION | MAPI_LOGON_UI,
                     &psess);

   if (FAILED(hr))
   {
      MessageBox(0L,"Logon Failed","Error",MB_OK);
      MAPIUninitialize();
      return 1;
   }
   hr = GetGALContents(psess);

   if (FAILED(hr))
   {
      MessageBox(0L,"Restriction Failed","Error",MB_OK);
      MAPIUninitialize();
      return 1;
   }
      MAPIUninitialize();
   return 0;
};

HRESULT GetGALContents (LPMAPISESSION psess)
{
   ULONG             cbeid    = 0L;
   LPENTRYID         lpeid    = NULL;
   HRESULT           hr       = S_OK;
   LPSRowSet         pRow;
   ULONG             ulObjType;
   SRestriction      sres,
                     srlevel1[2];
   LPMAPITABLE       lpContentsTable = NULL;
   LPABCONT          lpGAL           = NULL;
   SPropValue        spvDisplay, spvAddrType;
   LPADRBOOK         m_pAddrBook     = NULL;
   LPFlagList        lpFlagList      = NULL;
   LPADRLIST         pAdrList        = NULL;
   spvDisplay.ulPropTag = PR_DISPLAY_NAME;
   spvDisplay.Value.lpszA = "Enter Display Name";
   spvAddrType.ulPropTag = PR_DISPLAY_TYPE;
   spvAddrType.Value.l = DT_MAILUSER;
   SizedSPropTagArray ( 2, sptCols ) = { 2,
                                    PR_ENTRYID,
                                    PR_DISPLAY_NAME };
   psess->OpenAddressBook(NULL,NULL,AB_NO_DIALOG,&m_pAddrBook);
   if ( FAILED ( hr = HrFindExchangeGlobalAddressList ( m_pAddrBook,
                                                               &cbeid,
                                                               &lpeid ) ) )
         goto Quit;
   if(FAILED(hr = m_pAddrBook->OpenEntry((ULONG) cbeid,
                  (LPENTRYID) lpeid,
                  NULL,
                  MAPI_BEST_ACCESS,
                  &ulObjType,
                 (LPUNKNOWN *)&lpGAL)))
         goto Quit;
   if ( ulObjType != MAPI_ABCONT )
         goto Quit;

   if(FAILED(hr = lpGAL->GetContentsTable(0L, &lpContentsTable)))
         goto Quit;

   //Create Restriction
   sres.rt = RES_AND;
   sres.res.resAnd.cRes = 2;
   sres.res.resAnd.lpRes =srlevel1;
   srlevel1[0].rt = RES_CONTENT;
   srlevel1[0].res.resContent.ulFuzzyLevel = FL_PREFIX|FL_IGNORECASE;
   srlevel1[0].res.resContent.ulPropTag = PR_DISPLAY_NAME;
   srlevel1[0].res.resContent.lpProp = &spvDisplay;

   srlevel1[1].rt = RES_PROPERTY;
   srlevel1[1].res.resProperty.relop = RELOP_EQ;
   srlevel1[1].res.resProperty.ulPropTag = PR_DISPLAY_TYPE;
   srlevel1[1].res.resProperty.lpProp = &spvAddrType;

    if ( FAILED ( hr = HrQueryAllRows (lpContentsTable,(SPropTagArray*) &sptCols, &sres, NULL, 0,&pRow)))
       goto Quit;

   if (pRow->cRows > 0)
      MessageBox(0L,"Names Returned","Information",MB_OK);<BR/>

  Quit:
    if ( NULL != lpGAL)
    {
       lpGAL -> Release ( );
       lpGAL = NULL;
    }
    if ( lpContentsTable )
    {
       lpContentsTable -> Release ( );
       lpContentsTable = NULL;
    }
    return hr;

}

Modification Type:MinorLast Reviewed:8/24/2005
Keywords:kbhowto kbMsg KB200152 kbAudDeveloper