Microsoft KB Archive/259570

= XCLN: How to Retrieve Last Logon Time of Exchange Users Using Extended MAPI =

Article ID: 259570

Article Last Modified on 2/27/2007

-

APPLIES TO


 * Microsoft Exchange 2000 Server Standard Edition
 * Microsoft Exchange Server 5.5 Standard Edition
 * Microsoft Exchange Development Kit 5.5
 * Microsoft Exchange Server 2003 Software Development Kit
 * Microsoft Messaging Application Programming Interface

-



This article was previously published under Q259570



SUMMARY
You may want to retrieve information about mailboxes, such as the last logon time and last logoff time. You can accomplish this by logging on to the private information store with a privileged account, looping through the list of mailboxes, and opening PR_LAST_LOGON_TIME, PR_LAST_LOGOFF_TIME on each mailbox table row.



MORE INFORMATION
The following code sample illustrates how to retrieve the last logon time of each mailbox on an Exchange Server 5.5 or an Exchange 2000 Server computer. HrGetServerDN is called to obtain the Distinguished Name (DN) of an Exchange Server 5.5 computer or the legacy Exchange Server DN (legacyExchangeDN) of an Exchange 2000 Server computer. The DN is then passed to IExchangeManageStore::GetMailboxTable to obtain a list of mailbox tables.

Compile this code sample by using the Ignore all default libraries option on the Link tab on the project Settings dialog box. You must also use the following libraries:
 * Version.lib
 * Exchsdk.lib
 * Mapi32.lib
 * Edkutils.lib
 * Edkmapi.lib
 * Msvcrt.lib

Sample Code

 * 1) include 
 * 2) include 

HRESULT OpenMailbox(LPMAPISESSION lpMAPISession,                   LPSTR pszExchangeServerName);

void main {  HRESULT hr = S_OK;

hr = MAPIInitialize(NULL); if(FAILED(hr)) {     printf(&quot;Failed to initialize MAPI\n&quot;); }

char pszExchangeServerName[500]; LPMAPISESSION lpSess = NULL; LPMDB  lpMDB = NULL;

// Get the Exchange Server name from the user. //   printf(&quot;\nPlease enter the name of your Exchange System ?  &quot;); gets(pszExchangeServerName); printf(&quot;\n\n&quot;);

// TO DO: Need to logon using a profile for the service account or  //        an Exchange Admin. hr = MAPILogonEx(0, &quot;&quot;, NULL,          MAPI_LOGON_UI | MAPI_NEW_SESSION |  MAPI_EXPLICIT_PROFILE,           &lpSess); if (FAILED(hr)) {     printf(&quot;MAPI Logon failed\n&quot;); }

if(SUCCEEDED(hr)&& lpSess) {     printf(&quot;Created MAPI session\n&quot;);

hr = OpenMailbox(lpSess, pszExchangeServerName);

if(FAILED(hr)) printf(&quot;Failed to Run\n&quot;); else printf(&quot;Opened users mailboxes\n\n&quot;);

}

if(lpSess) lpSess->Release;

}

HRESULT OpenMailbox(LPMAPISESSION lpMAPISession, LPSTR pszServerName) {  HRESULT      hr            = S_OK; LPMAPITABLE lpMailBoxTable = NULL; LPSRowSet   lpRows         = NULL; LPMDB       lpMDB         = NULL; LPEXCHANGEMANAGESTORE lpIManageStore = NULL;

SYSTEMTIME sLogonSystemTime     =   {0}; SYSTEMTIME sLogonLocalTime      =   {0};

BOOL       fSucceeded      =   FALSE;

// need to pass this array to HrQueryAllRows on lpMailBoxTable SizedSPropTagArray(12, Columns) = {      12,       {           PR_DISPLAY_NAME, PR_EMAIL_ADDRESS, PR_NT_USER_NAME, PR_CONTENT_COUNT, PR_LOCALE_ID, PR_MESSAGE_SIZE, PR_MESSAGE_SIZE_EXTENDED, PR_ASSOC_CONTENT_COUNT, PR_LAST_LOGON_TIME, PR_LAST_LOGOFF_TIME, PR_STORAGE_LIMIT_INFORMATION, PR_INSTANCE_KEY }  };    LPMAPIPROP      pmp = NULL; LPSPropValue            pVal   = NULL; ULONG          ulObjType      = NULL;

if (FAILED(hr = HrOpenExchangePrivateStore(lpMAPISession, &lpMDB))) {     printf(&quot;Message Store Not Available\n&quot;); return MAPI_E_NOT_FOUND; }

if (FAILED(hr = lpMDB->QueryInterface(IID_IExchangeManageStore,                           (void **) &lpIManageStore))) {     printf(&quot;QueryInterace Failed\n&quot;); return MAPI_E_NOT_FOUND; }

LPSTR pszServerDN;

// we need to use Exchange 2000 legacyExchangeDN if (FAILED(hr = HrGetServerDN(lpMAPISession, &pszServerDN))) {      printf(&quot;HrGetServerDN Failed\n&quot;); return MAPI_E_NOT_FOUND; }

printf(&quot;Server DN: %s\n&quot;, pszServerDN);

if (FAILED(hr = lpIManageStore->GetMailboxTable(pszServerDN,                                   &lpMailBoxTable,0))) {     printf(&quot;Mailbox Table Not Available\n&quot;); return MAPI_E_NOT_FOUND; }

// Get a list of Mailboxes taking up resources. hr = HrQueryAllRows(lpMailBoxTable, (LPSPropTagArray)&Columns, NULL, NULL, 0, &lpRows);

if(SUCCEEDED(hr)) {     if (lpRows->cRows > 0) {        for (UINT i=0; i < lpRows->cRows; i++) {           printf(&quot;\n\nNumber of properties: %d\n&quot;, lpRows->aRow[i].cValues);

LPSPropValue lpDNspv = PpropFindProp(                               lpRows->aRow[i].lpProps,                                lpRows->aRow[i].cValues,                                PR_DISPLAY_NAME ); printf(&quot;Display Name: %s\n&quot;, lpDNspv->Value.lpszA);

LPSPropValue lpspv = PpropFindProp(                               lpRows->aRow[i].lpProps,                                lpRows->aRow[i].cValues,                                PR_EMAIL_ADDRESS ); printf(&quot;Email Address: %s\n&quot;, lpspv->Value.lpszA);

LPSPropValue lpNTUSERspv = PpropFindProp(                               lpRows->aRow[i].lpProps,                                lpRows->aRow[i].cValues,                                PR_NT_USER_NAME ); printf(&quot;NT User: %s\n&quot;, lpNTUSERspv->Value.lpszA);

LPSPropValue lpLastLogonTimespv = PpropFindProp(                               lpRows->aRow[i].lpProps,                                lpRows->aRow[i].cValues,                                PR_LAST_LOGON_TIME );

fSucceeded = FileTimeToSystemTime(&(lpLastLogonTimespv->Value.ft), &sLogonSystemTime); if (fSucceeded) {               TIME_ZONE_INFORMATION tzi; GetTimeZoneInformation(&tzi); SystemTimeToTzSpecificLocalTime(&tzi, &sLogonSystemTime, &sLogonLocalTime); printf(&quot;PR_LAST_LOGON_TIME : %2.2d/&quot;, sLogonLocalTime.wMonth); printf(&quot;%2.2d/&quot;, sLogonLocalTime.wDay); printf(&quot;%2.2d &quot;, sLogonLocalTime.wYear); printf(&quot;%2.2d:&quot;, sLogonLocalTime.wHour); printf(&quot;%2.2d\n&quot;, sLogonLocalTime.wMinute); }

LPSPropValue lpLocaleIDspv = PpropFindProp(                               lpRows->aRow[i].lpProps,                                lpRows->aRow[i].cValues,                                PR_LOCALE_ID ); printf(&quot;PR_LOCALE_ID: %u\n&quot;, lpLocaleIDspv->Value.ul);

LPSPropValue lpMessageSizespv = PpropFindProp(                               lpRows->aRow[i].lpProps,                                lpRows->aRow[i].cValues,                                PR_MESSAGE_SIZE ); printf(&quot;PR_MESSAGE_SIZE: %u\n&quot;, lpMessageSizespv->Value.ul);

LPSPropValue lpMessageSizeExtendedspv = PpropFindProp(                               lpRows->aRow[i].lpProps,                                lpRows->aRow[i].cValues,                                PR_MESSAGE_SIZE_EXTENDED ); printf(&quot;PR_MESSAGE_SIZE_EXTENDED: not known\n&quot;);//, lpDNspv->Value.lpszA);

LPSPropValue lpAssocContentCountspv = PpropFindProp(                               lpRows->aRow[i].lpProps,                                lpRows->aRow[i].cValues,                                PR_ASSOC_CONTENT_COUNT ); printf(&quot;PR_ASSOC_CONTENT_COUNT: %u\n&quot;, lpAssocContentCountspv->Value.ul);

LPSPropValue lpStorageLimitInfospv = PpropFindProp(                               lpRows->aRow[i].lpProps,                                lpRows->aRow[i].cValues,                                PR_STORAGE_LIMIT_INFORMATION ); printf(&quot;PR_STORAGE_LIMIT_INFORMATION: %u\n&quot;, lpStorageLimitInfospv->Value.ul);

LPSPropValue lpCountspv = PpropFindProp(                               lpRows->aRow[i].lpProps,                                lpRows->aRow[i].cValues,                                PR_CONTENT_COUNT ); printf(&quot;Content Count: %u\n&quot;, lpCountspv->Value.ul);

}     }   }

if(lpRows) {     FreeProws(lpRows); }

if(lpMailBoxTable) {     lpMailBoxTable->Release; }

if (FAILED(hr)) return MAPI_E_NOT_FOUND; else return S_OK; } Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and the tools that are used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For additional information about the support options available from Microsoft, visit the following Microsoft Web site:

http://support.microsoft.com/default.aspx?scid=fh;[LN;CNTACTMS]

Additional query words: MAPI GetMailboxTable PR_LAST_LOGON_TIME

Keywords: kbinfo KB259570

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.