XCLN: How to Retrieve Last Logon Time of Exchange Users Using Extended MAPI (259570)
The information in this article applies to:
- Microsoft Exchange 2000 Server
- Microsoft Exchange Server 5.5
- Microsoft Exchange Development Kit (EDK) 5.0
- Microsoft Exchange Development Kit (EDK) 5.5
- Microsoft Extended Messaging Application Programming Interface (MAPI)
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
#include <stdio.h>
#include <edk.h>
HRESULT OpenMailbox(LPMAPISESSION lpMAPISession,
LPSTR pszExchangeServerName);
void main()
{
HRESULT hr = S_OK;
hr = MAPIInitialize(NULL);
if(FAILED(hr))
{
printf("Failed to initialize MAPI\n");
}
char pszExchangeServerName[500];
LPMAPISESSION lpSess = NULL;
LPMDB lpMDB = NULL;
// Get the Exchange Server name from the user.
//
printf("\nPlease enter the name of your Exchange System ? ");
gets(pszExchangeServerName);
printf("\n\n");
// TO DO: Need to logon using a profile for the service account or
// an Exchange Admin.
hr = MAPILogonEx(0, "", NULL,
MAPI_LOGON_UI | MAPI_NEW_SESSION | MAPI_EXPLICIT_PROFILE ,
&lpSess);
if (FAILED(hr))
{
printf("MAPI Logon failed\n");
}
if(SUCCEEDED(hr)&& lpSess)
{
printf("Created MAPI session\n");
hr = OpenMailbox(lpSess, pszExchangeServerName);
if(FAILED(hr))
printf("Failed to Run\n");
else
printf("Opened users mailboxes\n\n");
}
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("Message Store Not Available\n");
return MAPI_E_NOT_FOUND;
}
if (FAILED(hr = lpMDB->QueryInterface(IID_IExchangeManageStore,
(void **) &lpIManageStore)))
{
printf("QueryInterace Failed\n");
return MAPI_E_NOT_FOUND;
}
LPSTR pszServerDN;
// we need to use Exchange 2000 legacyExchangeDN
if (FAILED(hr = HrGetServerDN(lpMAPISession, &pszServerDN)))
{
printf("HrGetServerDN Failed\n");
return MAPI_E_NOT_FOUND;
}
printf("Server DN: %s\n", pszServerDN);
if (FAILED(hr = lpIManageStore->GetMailboxTable(pszServerDN,
&lpMailBoxTable,0)))
{
printf("Mailbox Table Not Available\n");
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("\n\nNumber of properties: %d\n", lpRows->aRow[i].cValues);
LPSPropValue lpDNspv = PpropFindProp(
lpRows->aRow[i].lpProps,
lpRows->aRow[i].cValues,
PR_DISPLAY_NAME );
printf("Display Name: %s\n", lpDNspv->Value.lpszA);
LPSPropValue lpspv = PpropFindProp(
lpRows->aRow[i].lpProps,
lpRows->aRow[i].cValues,
PR_EMAIL_ADDRESS );
printf("Email Address: %s\n", lpspv->Value.lpszA);
LPSPropValue lpNTUSERspv = PpropFindProp(
lpRows->aRow[i].lpProps,
lpRows->aRow[i].cValues,
PR_NT_USER_NAME );
printf("NT User: %s\n", 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("PR_LAST_LOGON_TIME : %2.2d/", sLogonLocalTime.wMonth);
printf("%2.2d/", sLogonLocalTime.wDay);
printf("%2.2d ", sLogonLocalTime.wYear);
printf("%2.2d:", sLogonLocalTime.wHour);
printf("%2.2d\n", sLogonLocalTime.wMinute);
}
LPSPropValue lpLocaleIDspv = PpropFindProp(
lpRows->aRow[i].lpProps,
lpRows->aRow[i].cValues,
PR_LOCALE_ID );
printf("PR_LOCALE_ID: %u\n", lpLocaleIDspv->Value.ul);
LPSPropValue lpMessageSizespv = PpropFindProp(
lpRows->aRow[i].lpProps,
lpRows->aRow[i].cValues,
PR_MESSAGE_SIZE );
printf("PR_MESSAGE_SIZE: %u\n", lpMessageSizespv->Value.ul);
LPSPropValue lpMessageSizeExtendedspv = PpropFindProp(
lpRows->aRow[i].lpProps,
lpRows->aRow[i].cValues,
PR_MESSAGE_SIZE_EXTENDED );
printf("PR_MESSAGE_SIZE_EXTENDED: not known\n");//, lpDNspv->Value.lpszA);
LPSPropValue lpAssocContentCountspv = PpropFindProp(
lpRows->aRow[i].lpProps,
lpRows->aRow[i].cValues,
PR_ASSOC_CONTENT_COUNT );
printf("PR_ASSOC_CONTENT_COUNT: %u\n", lpAssocContentCountspv->Value.ul);
LPSPropValue lpStorageLimitInfospv = PpropFindProp(
lpRows->aRow[i].lpProps,
lpRows->aRow[i].cValues,
PR_STORAGE_LIMIT_INFORMATION );
printf("PR_STORAGE_LIMIT_INFORMATION: %u\n", lpStorageLimitInfospv->Value.ul);
LPSPropValue lpCountspv = PpropFindProp(
lpRows->aRow[i].lpProps,
lpRows->aRow[i].cValues,
PR_CONTENT_COUNT );
printf("Content Count: %u\n", 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:
Modification Type: | Major | Last Reviewed: | 12/15/2005 |
---|
Keywords: | kbinfo KB259570 |
---|
|