// WAB.cpp: implementation of the CWAB class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "WabTools.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
static const IID IID_IDistList={0x0002030E, 0, 0, {0xC0, 0, 0, 0, 0, 0, 0, 0x46}};
static const IID IID_IMAPITable={0x00020301, 0, 0, {0xC0, 0, 0, 0, 0, 0, 0, 0x46}};
static const IID IID_IMessage={0x00020307, 0, 0, {0xC0, 0, 0, 0, 0, 0, 0, 0x46}};
/************************************************************************/
/* Copyright (c) 2007 by wgm001. ALL RIGHTS RESERVED. */
/************************************************************************/
// 默认构造函数
//
CWAB::CWAB()
{
// 在这里初始代wab成员对象
m_lpAdrBook = NULL;
m_lpWABObject = NULL;
m_bInitialized = FALSE;
m_lpPropArray = NULL;
m_ulcValues = 0;
m_SB.cb = 0;
m_SB.lpb = NULL;
}
// 析构函数
//
CWAB::~CWAB()
{
if(m_SB.lpb)
LocalFree(m_SB.lpb);
if(m_bInitialized)
{
if(m_lpPropArray)
m_lpWABObject->FreeBuffer(m_lpPropArray);
if(m_lpAdrBook)
m_lpAdrBook->Release();
if(m_lpWABObject)
m_lpWABObject->Release();
if(m_hinstWAB)
FreeLibrary(m_hinstWAB);
}
}
// 装入记录到ListCtrl控件
//
HRESULT CWAB::LoadWABContents(CListCtrl *pListView)
{
ULONG ulObjType = 0;
LPMAPITABLE lpAB = NULL;
LPSRowSet lpRowAB = NULL;
LPABCONT lpContainer = NULL;
int cNumRows = 0;
ULONG ulCount = 0;
HRESULT hr = E_FAIL;
ULONG lpcbEID;
LPENTRYID lpEID = NULL;
// 得到PAB根容器
//
hr = m_lpAdrBook->GetPAB( &lpcbEID, &lpEID);
ulObjType = 0;
// 打开所有在PAB根容器的WAB
//
hr = m_lpAdrBook->OpenEntry(lpcbEID,
(LPENTRYID)lpEID,
NULL,
0,
&ulObjType,
(LPUNKNOWN *)&lpContainer);
m_lpWABObject->FreeBuffer(lpEID);
lpEID = NULL;
if(HR_FAILED(hr))
goto exit;
// 在WABs根容器里的得到一个所有的容器表
//
hr = lpContainer->GetContentsTable( 0, &lpAB);
if(HR_FAILED(hr))
goto exit;
hr =lpAB->SetColumns( (LPSPropTagArray)&ptaEid, 0 );
if(HR_FAILED(hr))
goto exit;
// 从头开始读
//
hr = lpAB->SeekRow( BOOKMARK_BEGINNING, 0, NULL );
if(HR_FAILED(hr))
goto exit;
hr = lpAB->GetRowCount(0, &ulCount);
// 读取表的所有行
//
do {
hr = lpAB->QueryRows(1, 0, &lpRowAB);
if(HR_FAILED(hr))
break;
if(lpRowAB)
{
cNumRows = lpRowAB->cRows;
if (cNumRows)
{
// 读取表的各项数据
//
LPTSTR lpsz = lpRowAB->aRow[0].lpProps[ieidPR_DISPLAY_NAME].Value.lpszA;
LPTSTR lpszMobilePhone = lpRowAB->aRow[0].lpProps[ieidPR_MOBILE_TELEPHONE_NUMBER].Value.lpszA;
LPTSTR lpszOfficePhone = lpRowAB->aRow[0].lpProps[ieidPR_OFFICE_TELEPHONE_NUMBER].Value.lpszA;
LPTSTR lpszHomePhone = lpRowAB->aRow[0].lpProps[ieidPR_HOME_TELEPHONE_NUMBER].Value.lpszA;
//int i = lpRowAB->aRow[0].lpProps[ieidPR_NNN].Value.i;
//CString lpszGivenName;
//lpszGivenName.Format("%s", i==2 ? "男" : i==1 ? "女" : "");
LPENTRYID lpEID = (LPENTRYID) lpRowAB->aRow[0].lpProps[ieidPR_ENTRYID].Value.bin.lpb;
ULONG cbEID = lpRowAB->aRow[0].lpProps[ieidPR_ENTRYID].Value.bin.cb;
if(lpRowAB->aRow[0].lpProps[ieidPR_OBJECT_TYPE].Value.l == MAPI_MAILUSER) //MAPI_MAILUSER
{
LPSBinary lpSB = NULL;
m_lpWABObject->AllocateBuffer(sizeof(SBinary), (LPVOID *) &lpSB);
if (lpSB)
{
m_lpWABObject->AllocateMore(cbEID, lpSB, (LPVOID *) &(lpSB->lpb));
if (!lpSB->lpb)
{
m_lpWABObject->FreeBuffer(lpSB);
continue ;
}
CopyMemory(lpSB->lpb, lpEID, cbEID);
lpSB->cb = cbEID;
LV_ITEM lvi = {0};
lvi.mask = LVIF_TEXT | LVIF_PARAM;
lvi.iItem = pListView->GetItemCount();
lvi.iSubItem = 0;
lvi.pszText = lpsz;
lvi.lParam = (LPARAM) lpSB;
// 添加到列表
pListView->InsertItem(&lvi);
pListView->SetItemText(lvi.iItem, 1, lpszMobilePhone);
pListView->SetItemText(lvi.iItem, 2, lpszOfficePhone);
pListView->SetItemText(lvi.iItem, 3, lpszHomePhone);
}
}
}
FreeProws(lpRowAB );
}
}while ( SUCCEEDED(hr) && cNumRows && lpRowAB) ;
exit:
if ( lpContainer )
lpContainer->Release();
if ( lpAB )
lpAB->Release();
return hr;
}
// 释放所占用资源
//
void CWAB::FreeProws(LPSRowSet prows)
{
ULONG irow;
if (!prows)
return;
for (irow = 0; irow < prows->cRows; ++irow)
m_lpWABObject->FreeBuffer(prows->aRow[irow].lpProps);
m_lpWABObject->FreeBuffer(prows);
}
// 清除一个条目的数据
HRESULT CWAB::ClearEntry(ADDRESS_BOOK *pAddressEntry)
{
pAddressEntry->m_iGender = -1;
memset((void*)& pAddressEntry->m_stBirthday, 0, sizeof(pAddressEntry->m_stBirthday));
pAddressEntry->m_strDepartment =
pAddressEntry->m_strDispName =
pAddressEntry->m_strHomeFaxNumber =
pAddressEntry->m_strHomePhoneNumber =
pAddressEntry->m_strMobilePhoneNumber =
pAddressEntry->m_strNickName =
pAddressEntry->m_strOfficeFaxNumber =
pAddressEntry->m_strOfficePhoneNumber =
pAddressEntry->m_strOrganization =
pAddressEntry->m_strPosition =
pAddressEntry->m_strRegion = "";
return S_OK;
}
HRESULT CWAB::GetCountEntry(ULONG &uiCountEntry)
{
ULONG ulObjType = 0;
LPMAPITABLE lpAB = NULL;
LPABCONT lpContainer = NULL;
HRESULT hr = E_FAIL;
ULONG lpcbEID;
LPENTRYID lpEID = NULL;
// 得到PAB根容器
//
hr = m_lpAdrBook->GetPAB( &lpcbEID, &lpEID);
if (HR_FAILED(hr))
return hr;
ulObjType = 0;
// 打开所有在PAB根容器的WAB
//
hr = m_lpAdrBook->OpenEntry(lpcbEID,
(LPENTRYID)lpEID,
NULL,
0,
&ulObjType,
(LPUNKNOWN *)&lpContainer);
if (HR_FAILED(hr))
return hr;
m_lpWABObject->FreeBuffer(lpEID);
lpEID = NULL;
if(HR_FAILED(hr))
goto exit;
// 在WABs根容器里的得到一个所有的容器表
//
hr = lpContainer->GetContentsTable( 0, &lpAB);
if(HR_FAILED(hr))
goto exit;
hr =lpAB->SetColumns( (LPSPropTagArray)&ptaEid, 0 );
if(HR_FAILED(hr))
goto exit;
// 定位到起始位置
//
hr = lpAB->SeekRow( BOOKMARK_BEGINNING, 0, NULL );
if(HR_FAILED(hr))
goto exit;
// 得到行数
hr = lpAB->GetRowCount(0, &uiCountEntry);
exit:
if ( lpContainer )
lpContainer->Release();
if ( lpAB )
lpAB->Release();
return hr;
}
// 装入一个联系人资料到结构,
//
HRESULT CWAB::LoadAddressEntry(ADDRESS_BOOK &pAddressEntry, const int nIndex)
{
ULONG ulObjType = 0;
LPMAPITABLE lpAB = NULL;
LPSRowSet lpRowAB = NULL;
LPABCONT lpContainer = NULL;
int cNumRows = 0;
ULONG ulCount = 0;
HRESULT hr = E_FAIL;
ULONG lpcbEID;
LPENTRYID lpEID = NULL;
// 得到PAB根容器
//
hr = m_lpAdrBook->GetPAB( &lpcbEID, &lpEID);
ulObjType = 0;
// 打开所有在PAB根容器的WAB
//
hr = m_lpAdrBook->OpenEntry(lpcbEID,
(LPENTRYID)lpEID,
NULL,
0,
&ulObjType,
(LPUNKNOWN *)&lpContainer);
m_lpWABObject->FreeBuffer(lpEID);
lpEID = NULL;
if(HR_FAILED(hr))
goto exit;
// 在WABs根容器里的得到一个所有的容器表
//
hr = lpContainer->GetContentsTable( 0, &lpAB);
if(HR_FAILED(hr))
goto exit;
hr =lpAB->SetColumns( (LPSPropTagArray)&ptaEid, 0 );
if(HR_FAILED(hr))
goto exit;
// 从头开始读
//
hr = lpAB->S