典型的做法可以参见徐景周的文章《获取本机通讯薄的内容》,其中定位通讯录的方式是:
// 获取默认Outlook中联系人文件夹大多数状况,这样的确可以满足我们的需求。然后实际上我还发现,很多人重新装机前都会备份自己的outlook文件(默认位于: "C:\Documents and Settings\foolbear\Local Settings\Application Data\Microsoft\Outlook\Outlook.pst"),装机后再添加到outlook中,以免资料的损失。
pFolder=pApp->GetNamespace(_bstr_t("MAPI"))->GetDefaultFolder(olFolderContacts);
不仅如此用户为了清晰管理通讯录,可以做多层级的目录。
上面的这两种状况用前面的方法就不能解决了,而msdn上也找不到任何的资料,我只能按照惯例尝试。
解决第一个问题,我们必须找到整个outlook资料的根文件夹:
_Explorer olExplorer;
olExplorer=olApp.ActiveExplorer();
_Folders olFolders;
olFolders=olNs.GetFolders();
printf("%d folders.\n", olFolders.GetCount());
之后枚举出有多少个outlook pst文件,并找到其中联系人的根文件夹:
MAPIFolder top_folder = olFolders.GetFirst();其中函数GetContacts就是解决第二个问题--目录树的遍历,这里偷懒就使用了递归的方法:
while(top_folder!=NULL) {
printf("\tFolder %s, type %d.\n", top_folder.GetName(), top_folder.GetDefaultItemType());
_Folders folders = top_folder.GetFolders();
if (folders == NULL)
break;
printf("\t%d Folders.\n", folders.GetCount());
MAPIFolder contactfolder = folders.GetFirst();
while (contactfolder!=NULL) {
int itype = contactfolder.GetDefaultItemType();
if (itype == 2/*olContactItem*/) {
printf("\t\tFolder %s.\n", contactfolder.GetName());
int count = GetContacts(contactfolder);
printf("total contacts is %d.\n", count);
break;
}
contactfolder = folders.GetNext();
}
top_folder = olFolders.GetNext();
}
int GetContacts(MAPIFolder folder)下面是最后枚举出单个文件夹中的所有联系人:
{
int count = 0;
_Folders sub_folders;
MAPIFolder sub_folder;
if (folder == NULL)
return 0;
count = ProcessContacts(folder);
sub_folders = folder.GetFolders();
if (sub_folders == NULL)
return count;
sub_folder = sub_folders.GetFirst();
while (sub_folder != NULL)
{
count += GetContacts(sub_folder);
sub_folder = sub_folders.GetNext();
}
return count;
}
int ProcessContacts(MAPIFolder folder)这样就大功告成了!
{
int count = 0;
_Items contacts;
_ContactItem contact;
printf("folder name = %s.\n", folder.GetAddressBookName());
contacts = folder.GetItems();
if (contacts == NULL)
return 0;
contact = contacts.GetFirst();
while (contact != NULL)
{
count++;
printf("\tcontact name = %s.\n", contac.GetFullName());
contact = contacts.GetNext();
}
return count;
}
没有评论 :
发表评论