How To Read a Rules Table in Exchange (200162)



The information in this article applies to:

  • Microsoft Exchange Development Kit (EDK) 5.0
  • Microsoft Exchange Development Kit (EDK) 5.5

This article was previously published under Q200162

SUMMARY

Rules are stored in a table. Reading the rules table may be necessary to determine how messages are processed. This table is accessed using OpenProperty with the PR_RULES_TABLE option.

MORE INFORMATION

The code below is placed in a Win32 application project and the Ignore all default libraries option on the Link tab should be selected. The following libraries are used to compile the code:

Msvcrt.lib, Version.lib, Mapi32.lib, Edkmapi.lib, Edkutils.lib, Addrlkup.lib, Edkdebug.lib
#define EDK_USES_IID
#include <edk.h>
#include <rulecls.h>

void GetRuleList(long,LPSRowSet);

LPMAPISESSION lpSession = NULL;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, 
               LPSTR pszCmd, int nCmdShow)
{
   LPMDB                 lpStore        = NULL;
   CHAR                  szFolder[MAX_PATH + 1] = {0};
   LPMAPIFOLDER          lpFolder       = NULL;
   LPSTR FAR *           lppszProviders = NULL;
   ULONG                 cbEIDFolder    = 0;
   LPENTRYID             lpEIDFolder    = NULL;
   ULONG                 cProviders     = 0;
   LPFOLDERRULES         lpFRulesLst    = NULL;
   LPMAPITABLE           m_lpMapiTbl    = NULL;
   LPEXCHANGEMODIFYTABLE m_lpExchTbl    = NULL;
   ULONG                 ulFlagsTable   = 0;
   ULONG                 lpulCount      = NULL;
   LPSRowSet             lppRows        = NULL;

   HRESULT hr = MAPIInitialize(NULL);
   if (FAILED(hr))
   {
      MessageBox(0L,"Initialize Failed","Error",MB_OK);
      return 1;
   }

   hr = MAPILogonEx(0, NULL, NULL,
              MAPI_LOGON_UI | MAPI_NEW_SESSION |  MAPI_EXPLICIT_PROFILE,
              &lpSession);
   if (FAILED(hr))
   {
      MessageBox(NULL,"MAPI Logon failed",NULL,MB_OK);
      goto cleanup;
   }

   hr = HrOpenExchangePrivateStore(lpSession, &lpStore); 
   if (FAILED(hr))
   {
      MessageBox(NULL,"Message Store Not Opened",NULL,MB_OK);
      goto cleanup;
   }

   strcpy(szFolder, "Top of Information Store\\Inbox");
   hr = HrMAPIFindFolderEx(lpStore, '\\', szFolder,
                           &cbEIDFolder, &lpEIDFolder);
   if (FAILED(hr))
   {
      MessageBox(NULL,"Inbox Not Found",NULL,MB_OK);
      goto cleanup;
   }

   hr = HrMAPIOpenFolderEx(lpStore, '\\', szFolder, &lpFolder);
   if (FAILED(hr))
   {
      MessageBox(NULL,"Inbox not Opened",NULL,MB_OK);
      goto cleanup;
   }

   hr = HrFolderRulesGetProviders(lpStore, cbEIDFolder, lpEIDFolder,
                                  &cProviders, &lppszProviders);
   if (FAILED(hr))
   {
      MessageBox(NULL,"No Providers",NULL,MB_OK);
      goto cleanup;
   }

   hr = HrFolderRulesOpen(lpStore, cbEIDFolder, lpEIDFolder,
                          *lppszProviders, &lpFRulesLst);
   if (FAILED(hr))
   {
      MessageBox(NULL,"No Rules Found",NULL,MB_OK);
      goto cleanup;
   }

   // Creating MAPITable to get the number of rules
   hr = lpFolder->OpenProperty(PR_RULES_TABLE,
                        (LPGUID)&IID_IExchangeModifyTable, 0,
                        MAPI_DEFERRED_ERRORS,
                        (LPUNKNOWN FAR *)&m_lpExchTbl);
   if (FAILED(hr))
      goto cleanup;

   // Open a MAPI table on the RULE table property. This table can be
   // read to determine what the rules table looks like.
   hr = m_lpExchTbl->GetTable(0, &m_lpMapiTbl);
   if (FAILED(hr))
   {
      hr = HR_LOG(E_FAIL);
      goto cleanup;
   }

   hr = m_lpMapiTbl->GetRowCount(ulFlagsTable,&lpulCount);
   if (FAILED(hr))
   {
      goto cleanup;
   }
    
   hr = HrQueryAllRows(m_lpMapiTbl,NULL, NULL, NULL, lpulCount,&lppRows);
   if (FAILED(hr))
   {
      MessageBox(NULL,"Rules not returned",NULL,MB_OK);
      goto cleanup;
   }

   GetRuleList(lpulCount,lppRows);

cleanup:

   if (lpSession)
   {
      lpSession->Logoff(0, 0, 0);
      ULRELEASE(lpSession);
   }
   MAPIUninitialize();
   return 0;
}

void GetRuleList(long lulRows, LPSRowSet lppRows)
{

   HRESULT        hr;
   LPSTR          p            = 0, 
                  pResString, 
                  pActString;
   long           lState       = 0, 
                  lLevel       = 0, 
                  lPos         = 0;
   LPSRestriction pRestriction = 0;
   LPACTIONS      lpActions    = 0;
   int            lCnt         = 0;

   while(lPos < lulRows)
   {
      pRestriction = 0;
      lpActions = 0;
      pResString = 0;
      pActString = 0;
      pRestriction = 
           (LPSRestriction) lppRows->aRow[lPos].lpProps[5].Value.x;
      lpActions = (LPACTIONS) lppRows->aRow[lPos].lpProps[6].Value.x;

      if (FAILED(hr = HrRestrictionToString(pRestriction, &pResString)))
         break;

      if (lpActions && lpActions->lpAction)
         HrActionToString(lpSession, lpActions->lpAction, &pActString);

      if (pActString)
      {
         char szRule [256];
         sprintf(szRule,"\n%s\n",pActString);
         OutputDebugString(szRule);
      }
      lPos ++;
   }
   MAPIFreeBuffer(pResString);
   MAPIFreeBuffer(pActString);
   MAPIFreeBuffer(pRestriction);
   MAPIFreeBuffer(lpActions);
}
				

Modification Type:MinorLast Reviewed:7/2/2004
Keywords:kbhowto kbMsg KB200162