Click here to Skip to main content
Click here to Skip to main content

Accessing the Windows Address Book

By , 1 Jan 2003
 

Introduction

After releasing my first freeware product QuickE numerous request came in, to have the application integrate with the user's address book in their Windows Address Book. On Windows XP, you can find this application by clicking Start->Programs->Accessories->[click] Address Book. I was surprised that anyone was using Windows Address Book, as I was under the naive assumption that everyone uses Outlook or at least Outlook Express (since the latter is at least free), but to my astonishment a lot of people just use the normal Windows Address Book application that comes with most installation of Windows, and if you do use Outlook Express you can actually see that the address book is filled with the same address in Outlook Express. As soon as I started this task, the first thing I did was type in WAB (Windows Address Book) into my MSDN search facility, lo and behold my surprise, at the very small amount of information that was returned to me. I wrote this class in order for other people to have at least a starting point in accessing and using the WAB. I hope you find the source code useful.

System requirements

This code was written and tested in Visual Studio.NET using unmanaged C++ and Win XP Professional. Tony R. tested this code using Visual C++ 6 and was able to get it to run. If anyone else can run this on another platform, let us know so I can post it here, that would be great. Until I get confirmation that this class works in other development environments and on other operating systems, I will list only what I have tested on a first hand basis.

Accessing the WAB

In order to access the WAB you must first load the wab32.dll library and get the address to WAB open which also gives you pointers to the IAdrBook and IWABObject interfaces. Below is a code snippet on how I made this call:

//We're safe the module was initialzised let's do what we need to do
ssWABOpen = (fWABOpen)GetProcAddress(hModule, "WABOpen");
//If not successful throw an error throw the value = 2
if (ssWABOpen == NULL) return 2;
//It is successful call it
HRESULT hr = (ssWABOpen)(&lpAddrBook, &lpWABObject, NULL, 0);

ssWABOpen is a typedef to the WABOpen function. Something I came across when writing this is, finding out that when you typedef this function you must declare the calling mechanism as WINAPI or when compiling you will get a mismatch error.

After this I then call the object's LoadEmails() function. This private function called from within the InitAddrBook function, loads the emails into a UDT of objects within an STL::vector object. I decide to go this route, mainly because of more ignorance on my part, my address book is about 20 names and this method does not soak up too much system resources. If this does not work out for others, I will change it to something more system resource friendly.

Inside the LoadEmails() function is where all the email extraction from the WAB to my UDT vector takes place. You can modify or change this into something that would be more appropriate for your specific use. Or, extract the information into a more useful UDT other than the one I have created.

Usage and special functions

In order to give you a foundation of how to use the class, this is how I use the class in my current beta build of my application. In the main dialog's class I declare a private variable of class CWinAddrBook. I then initialize this variable inside of the dialogs class' OnInitDialog() function, by calling object.InitAddrBook member method. This does two things, first it loads the wab32.dll and then, retrieves from this, the address for the WABOpen() function.

In order to test this class before releasing and not having to build its on application container I created the member method TestAddr(). This basically takes the contents of the vector after you initialized it, and outputs it (TRACE) while running in debug mode to the output window, it then separates the output by carriage line feed pair. This way you are insured that something is actually in your vector and you can look at the output compared to your address book to insure that all the information transferred over correctly.

Request for comments/dilemmas

When creating this class, here are some strange things I came across, maybe someone could shed some light. First thing that caught me off guard is just by using MSDN documents I tried to only include wabapi.h, when in fact it is better to include just wab.h. By going with what is listed on the interface help sections all I saw was WABAPI and for a while I could not get it to compile.

When I first compiled it into my code, I kept getting a

error C2371: 'WCHAR' redfinition; different basic types

inside of wabdefs.h which is included when you specify wab.h. The work around I used was go into this file and around line line 77 I made this change in order to get my code to compile. I do not know why I had to do this or if it is even right, but it worked. If someone knows what this means I would be greatly appreciative of knowing why it does this.

#if !defined(UNIX) && !defined(WIN16) && !defined(WIN32)

LoadEmails() used to be a separate function that was called after InitAddr() however I kept getting an unhandled exception inside the OnInitDialog function. After removing the public function call to LoadEmails, and made it a private function called from within InitAddr() it works fine.

Contributors

Not MSDN! j/k Most of the contributors are included in the header file comments. I will also include here Hirosh for his article on Extracting Email ID's from an address book using Memory mapped file, and another author is anonymous. I got the code snippet from a friend, who said he found it on the Internet, and I have included it in the download file the way I received it. But if anyone knows the author's name or web address please let me know, so I can give him/her the proper credit.

Tony R. for testing out the code in Visual C++ 6 and finding numerous other bugs.

Revisions

  • 1/2/2003: Updated code and changed the extraction process to extract information into separate areas instead of repeatedly extracting all address information to the name field. Changed the structure that holds addresses to use basic_string instead of a fixed sized array of characters to avoid buffer overrun problems I was experiencing.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Code4Food
Web Developer
United States United States
Member
My real name is Sam I live in Monterey, California. I am currently unemployed - long story. I am trying to secure a job as a programmer in the Silicon Valley area of CA but so far no luck. Now with all this extra time I am writing code and learning concepts I have put off because I was too busy working Smile | :)
 
I have completed two of my programming goals: release software in the form of freeware, and write an article for CodeProject!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to add new contact to Address Book by code?membersunooo15 Nov '06 - 20:26 
Generalidentitiesmemberkirans462 Aug '06 - 0:17 
QuestionHow get all new contacts which are created after a given time?membermpprasad22 Jul '06 - 20:51 
QuestionHow to get creation time of a VCard or contactmembermpprasad6 Jul '06 - 20:22 
QuestionHow to create folder in Windows Address Book in code?membersimon.lm5 Jun '06 - 16:41 
GeneralSolving the "error C2371: 'WCHAR' redefinition..." mystery.memberJulberto Danray21 Apr '06 - 18:32 
QuestionHow to get more infomation of mailuser?memberpesalo5 Feb '06 - 22:44 
GeneralWhy is Incorrect TimeValue returned from APImemberDouglas R. Keesler30 May '05 - 6:52 
GeneralRe: Why is Incorrect TimeValue returned from APImemberPuddin Tame31 Aug '05 - 16:04 
GeneralWCHAR solutionsussAnonymous10 Mar '05 - 21:04 
QuestionAccess violation?memberMaggieLuo2 Dec '04 - 4:01 
AnswerRe: Access violation?memberMaggieLuo2 Dec '04 - 15:13 
GeneralRe: Access violation?memberctype15 Jan '08 - 4:04 
GeneralPlease how to get the Emails from wab using c#memberAngel Tsvetkov22 Nov '04 - 0:56 
QuestionHow Can I get the Email Address with C#memberliyang (China)27 May '04 - 18:22 
AnswerRe: How Can I get the Email Address with C#memberyangguotai29 Apr '10 - 22:14 
QuestionHow to add new contact to Address Book by code?memberHuuQuynh17 May '04 - 22:33 
AnswerRe: How to add new contact to Address Book by code?memberMaia20 May '04 - 5:57 
GeneralRe: How to add new contact to Address Book by code?sussDAenil12 Jul '04 - 1:00 
GeneralRe: How to add new contact to Address Book by code?memberSPartha30 Aug '04 - 8:24 
GeneralRe: How to add new contact to Address Book by code?sussAshish Chauhan3 Apr '05 - 23:31 
QuestionRe: How to add new contact to Address Book by code?memberRustam_Rustamov5 Sep '06 - 8:49 
GeneralBugsmemberVattila13 Sep '03 - 12:56 
GeneralRe: BugsmemberVattila23 Sep '03 - 8:34 
GeneralRe: Bugsmemberkido9715 Dec '04 - 21:30 
GeneralRe: Bugs seems to be good but not working properlymemberravjak7 Apr '04 - 7:30 
GeneralRe: Bugs working!!!memberravjak9 Apr '04 - 2:46 
GeneralThis is great man!memberkoteec22 Aug '03 - 1:14 
GeneralRe: This is great man!memberNormski22 Aug '03 - 1:40 
GeneralExport to CSVmemberyuleo30 Jul '03 - 5:18 
GeneralRe: Export to CSVmemberVattila13 Sep '03 - 17:04 
QuestionHow do i use this ?memberJase Jennings16 Jul '03 - 15:16 
AnswerRe: How do i use this ?memberCode4Food16 Jul '03 - 15:47 
AnswerRe: How do i use this ?memberVattila13 Sep '03 - 13:47 
GeneralRe: How do i use this ?memberkido978 Dec '04 - 22:03 
QuestionAddrbook.txt source code author ?memberasalmi15 Jun '03 - 2:12 
AnswerRe: Addrbook.txt source code author ?memberscontapay14 Jul '03 - 6:36 
AnswerRe: Addrbook.txt source code author ?memberbrianegge20 Jul '07 - 20:24 
AnswerRe: Addrbook.txt source code author ?memberbrianegge20 Jul '07 - 20:24 
GeneralFolder namememberKim Burndred10 May '03 - 15:37 
Generalfor further researchmemberRüpel4 Apr '03 - 1:41 
GeneralRe: for further researchmemberhoge15 May '03 - 9:19 
GeneralRe: for further researchmemberscontapay14 Jul '03 - 6:35 
GeneraldelphisussPoaolo2 Mar '03 - 2:28 
GeneralRe: delphimemberfoxit20 Mar '03 - 10:38 
GeneralI want to get Mail user propertiesmemberultt4 Feb '03 - 15:50 
GeneralRe: I want to get Mail user propertiesmemberCode4Food5 Feb '03 - 6:40 
Questionother properties?membermindows24 Jan '03 - 9:49 
AnswerRe: other properties?memberCode4Food27 Jan '03 - 13:31 
GeneralRe: other properties?memberDougSMiller21 Feb '03 - 3:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130516.1 | Last Updated 2 Jan 2003
Article Copyright 2002 by Code4Food
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid