How To Export Directory Information to a File Using BatchExport (200154)



The information in this article applies to:

  • Microsoft Extended Messaging Application Programming Interface (MAPI)
  • Microsoft Exchange Development Kit (EDK) 5.0
  • Microsoft Exchange Development Kit (EDK) 5.5

This article was previously published under Q200154

SUMMARY

Exporting mailbox information about mailboxes on the messaging system may be required. You can do this programmatically using BatchExport() and passing a few parameters. This article contains the code that demonstrates how to use BatchExport() to write out the mailbox information for all users to the file Test.csv. The program takes two parameters: server and organization name.

MORE INFORMATION

The following code should be compiled as a console application using these libraries:
  • Mapi32.lib
  • Dapi.lib


#include <windows.h>
#include <stdio.h>
#include <lmcons.h
#include <dapi.h>

void ReportDAPIEvent(DAPI_EVENT* pDAPIEvent);

void main(int argc, char* argv[])

{
   HRESULT hr = 0;
   BEXPORT_PARMS    BexportParms    = {0};
   LPBEXPORT_PARMS lpBexportParms  = {0};
   DAPI_HANDLE hDAPISession;
   DAPI_EVENT* pDAPIEvent = NULL;
   DAPI_PARMS DAPIParms = {0};
   char szAccount[UNLEN + MAX_COMPUTERNAME_LENGTH + 2];
   DWORD dwAccountLength = UNLEN + MAX_COMPUTERNAME_LENGTH + 1;
   if (2 > argc) {
      printf("\nDAPITest ExchangeServerName /O=Organization");
      return;
   }
   printf("\nExchange Server: %s", argv[1]);
    // start DAPI for this session
   //initialize the the DAPI Parms structure and the DAPI operation session
   DAPIParms.dwDAPISignature = DAPI_SIGNATURE;
   DAPIParms.dwFlags = DAPI_EVENT_ALL|DAPI_MODIFY_REPLACE_PROPERTIES|DAPI_RESTRICT_ACCESS ;
   DAPIParms.pszDSAName = argv[1];
   DAPIParms.pszBasePoint = NULL;
   DAPIParms.pszContainer = NULL;
   DAPIParms.pszNTDomain = argv[1];
   DAPIParms.pszCreateTemplate = NULL;
   DAPIParms.pAttributes = NULL;
   pDAPIEvent = DAPIStart(&hDAPISession, &DAPIParms);  //struct with DAPI params
   if(pDAPIEvent)
   {
        printf("\nDAPIStart() ERROR %08x - check app eventlog", pDAPIEvent->dwDAPIError);
        ReportDAPIEvent(pDAPIEvent);
    }
    else
        printf("\nDAPIStart() was successful");
   lpBexportParms = &BexportParms;
   lpBexportParms->dwDAPISignature = DAPI_SIGNATURE;
   lpBexportParms->dwFlags = DAPI_EXPORT_MAILBOX | DAPI_EXPORT_HIDDEN  | DAPI_EVENT_ALL |DAPI_EXPORT_SUBTREE ;
   lpBexportParms->pszExportFile = "TEST.CSV";
   lpBexportParms->pszBasePoint = argv[2];
   lpBexportParms->pszDSAName = argv[1];
   lpBexportParms->pszHomeServer = NULL;
   lpBexportParms->chColSep = DAPI_DEFAULT_DELIM;
   lpBexportParms->chQuote = DAPI_DEFAULT_QUOTE;
   lpBexportParms->chMVSep = DAPI_DEFAULT_MV_SEP;
   hr = BatchExport(lpBexportParms);
   DAPIEnd(&amp;hDAPISession);
   printf("\nEND PROGRAM");
}
void ReportDAPIEvent(DAPI_EVENT* pDAPIEvent)
{
    HANDLE hDAPIEventSource = RegisterEventSource(NULL, TEXT("MSExchangeDSImp"));
    ReportEvent(
         hDAPIEventSource,
         (WORD)EVENTLOG_ERROR_TYPE,
         0,
         pDAPIEvent->dwDAPIError,
         NULL,
         (WORD)pDAPIEvent->unSubst,
         0,
         pDAPIEvent->rgpszSubst,
         NULL);
    DAPIFreeMemory(pDAPIEvent);
    DeregisterEventSource(hDAPIEventSource);
     )

}

REFERENCES

Another example of BatchExport is in the Dirysnc.c sample found on the Microsoft Developer Network Library.
For additional information, please see the following articles in the Microsoft Knowledge Base:

184268 How To Using DAPIRead
188960 How To Use BatchExport to Specify Which Attributes to Export
186333 How To Programmatically Get List of Exchange Servers in the Org

Modification Type:MinorLast Reviewed:8/25/2005
Keywords:kbhowto kbMsg KB200154 kbAudDeveloper