943,884 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 7759
  • C RSS
Aug 1st, 2004
0

access Windows Address Book

Expand Post »
Hi !

Could somebody help me how can I import email addresses from Windows Address Bok (wab) file programatically ?

The simplest code would be the best one, as I was able to found several but far too hard code to understand the hole issue.

Many many thanks for anybody concrete help.

Moore
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
coolice is offline Offline
9 posts
since Jul 2004
Aug 2nd, 2004
0

Re: access Windows Address Book

Try this this really good
  1. #include "StdAfx.h"
  2. #include "winaddrbook.h"
  3.  
  4. CWinAddrBook::CWinAddrBook(void)
  5. :lpWABObject(NULL),
  6. lpAddrBook(NULL)
  7. , IsInit(false)
  8. {
  9.  
  10. //Opens the default WAB file in the system and loads it into the object
  11. HINSTANCE hinstWAB=NULL;
  12. HRESULT hr=E_FAIL;
  13. HKEY keyResult;
  14. BYTE keyValue[MAX_PATH];
  15. DWORD dataout=800;
  16.  
  17. RegOpenKeyEx(HKEY_LOCAL_MACHINE, WAB_DLL_PATH_KEY, 0, KEY_ALL_ACCESS, &keyResult); //"Software\\Microsoft\\WAB\\DLLPath"
  18. long result = RegQueryValueEx(keyResult, "", 0, 0, keyValue, &dataout);
  19. RegCloseKey(keyResult);
  20.  
  21. //Get the full path of WAB and store in PathToWAB
  22. strcpy(PathToWAB, (char*)keyValue);
  23.  
  24. //Now let us load the library
  25. hModule = LoadLibrary(PathToWAB);
  26.  
  27. }
  28.  
  29. CWinAddrBook::~CWinAddrBook(void)
  30. {
  31. //Free the library we use to get the WAB file
  32. FreeLibrary(hModule);
  33. IsInit = false;
  34. }
  35.  
  36. // Initialize the address book and basically open the address book
  37. int CWinAddrBook::InitAddrBook(void)
  38. {
  39. //After opening the library go ahead and try and get the values into memory
  40. //maybe implement a memory map file later depending on how big some on the
  41. //performance of this class after release, and on some people's systems (i.e. huge address files)
  42. //
  43. // For now go ahead and store the information in a STL Vector
  44.  
  45. if (hModule!=NULL)
  46. {
  47. //We're safe the module was initialzised let's do what we need to do
  48. ssWABOpen = (fWABOpen)GetProcAddress(hModule, "WABOpen");
  49. //If not successful throw an error throw the value = 2
  50. if (ssWABOpen == NULL) return 2;
  51. //It is successful call it
  52. HRESULT hr = (ssWABOpen)(&lpAddrBook, &lpWABObject, NULL, 0);
  53. }
  54.  
  55. IsInit = true;
  56. LoadEmails(); //Moved here was being weird when called as another function from outsidethe class... hmmmm... go figure?!?
  57.  
  58. //Everything was OK
  59. return 0;
  60. }
  61.  
  62. // Load email addresses into the vector
  63. int CWinAddrBook::LoadEmails(void)
  64. {
  65. HRESULT hr = E_FAIL;
  66.  
  67. if (ssWABOpen==NULL) return 1; //ssWABOpen cannot be NULL
  68. {
  69. ULONG lpcbEntryID;
  70. ENTRYID* lpEntryID;
  71.  
  72. hr = lpAddrBook->GetPAB(&lpcbEntryID, &lpEntryID);
  73. if (hr!=S_OK) return 10; //error opening the darn PAB
  74.  
  75. //Declare variables for MAPI and specific access to the PAB
  76. ULONG ulFlags = MAPI_BEST_ACCESS,
  77. ulObjType = NULL;
  78. LPUNKNOWN lpIUnknown = NULL;
  79. hr = lpAddrBook->OpenEntry(lpcbEntryID,
  80. lpEntryID,
  81. NULL,
  82. ulFlags,
  83. &ulObjType,
  84. &lpIUnknown);
  85.  
  86. ulFlags = NULL; //We are using it again :)
  87.  
  88. if (ulObjType==MAPI_ABCONT) //See if an address book container was passed which we are looking for
  89. {
  90. IABContainer* lpABContainer = static_cast<IABContainer*>(lpIUnknown); //cast the IUnknown pointer returned from previous function to what we need
  91. LPMAPITABLE lpMAPItbl = NULL;
  92. hr = lpABContainer->GetContentsTable(ulFlags, &lpMAPItbl);
  93. ASSERT(lpMAPItbl);
  94.  
  95. ULONG ulRows; //Number of rows in the MAPI table (Addresses)
  96. hr = lpMAPItbl->GetRowCount(0, &ulRows);
  97. if (hr!=S_OK) return 11; //Return 11 to tell them we where unable to get the row count
  98.  
  99. SRowSet* lpRowSet;
  100. hr = lpMAPItbl->QueryRows( //Return all the rows found in the address book
  101. ulRows,
  102. 0,
  103. &lpRowSet);
  104.  
  105. //Grow variable
  106. AddrMemBook.resize(lpRowSet->cRows);
  107. //char Match[200]; strset(Match, 0);
  108.  
  109. for (ULONG x = 0; x < lpRowSet->cRows; x++)
  110. {
  111. //Loop through all the rows and add it to our address book
  112. //vector variable
  113. EMAILS thisAddr;
  114.  
  115. SRow* lpRow = &lpRowSet->aRow[x]; //Get this specific row
  116. for (ULONG y = 0; y < lpRow->cValues; y++)
  117. {
  118. //Loop through the fields in the address book and assign
  119. //to our variable and put it in the address book variable
  120. SPropValue* lPropVal = &lpRow->lpProps[y];
  121. switch (lPropVal->ulPropTag)
  122. {
  123. case PR_DISPLAY_NAME_A:
  124. thisAddr.DisplayName = lPropVal->Value.lpszA;
  125. //strcpy(thisAddr->DisplayName, lPropVal->Value.lpszA);
  126. break;
  127.  
  128. case PR_EMAIL_ADDRESS_A:
  129. thisAddr.EmailAddr = lPropVal->Value.lpszA;
  130. if (strchr(thisAddr.EmailAddr.c_str(), '@')==NULL) //replace with empty string
  131. thisAddr.EmailAddr='\0';
  132. //strcpy(thisAddr->EmailAddr, lPropVal->Value.lpszA);
  133. break;
  134.  
  135. case PR_NICKNAME_A:
  136. thisAddr.NickName = lPropVal->Value.lpszA;
  137. //strcpy(thisAddr->NickName, lPropVal->Value.lpszA);
  138. break;
  139.  
  140. default:
  141. break;
  142. }
  143.  
  144. }//End of cycling through fields
  145. //Add the information to the vector variable
  146. if (!thisAddr.EmailAddr.empty())
  147. AddrMemBook.push_back(thisAddr);
  148.  
  149. lpWABObject->FreeBuffer(lpRow);
  150.  
  151. }//End of cycling through rows
  152. lpWABObject->FreeBuffer(lpRowSet);
  153. }
  154. }
  155.  
  156. return 0;
  157. }
  158.  
  159. // Test mostly for debug purposes so you can step through and ensure that all the information is present for the address book vector
  160. void CWinAddrBook::TestAddrBook(void)
  161. {
  162. for (int x =0; x < (int)AddrMemBook.size(); x++)
  163. {
  164. //Set break here maybe later do a trace command instead
  165. EMAILS thisAddr = AddrMemBook[x];
  166. TRACE(thisAddr.DisplayName.c_str()); TRACE("\r\n");
  167. TRACE(thisAddr.EmailAddr.c_str()); TRACE("\r\n");
  168. TRACE(thisAddr.NickName.c_str()); TRACE("\r\n");
  169. }
  170. }
Team Colleague
Reputation Points: 55
Solved Threads: 3
Junior Poster
meabed is offline Offline
139 posts
since May 2004
Aug 21st, 2004
0

Re: access Windows Address Book

Hi !

Many thanks, but this one I had found few weeks ago at Code project, and the trouble is that after it was able to compile, it runs perfectly, but as soon as I close the application it generates error.

I am 100% sure that it has bugs with freeing memory, but I wasnt able to find it.

Could you help me to compile and locate the problem ?

I had really tried all other possibilities and I got lost.

I had attached the complete project ZIPed for BCB, which I had been cleared and organised, so it run and compile perfectly, but this bloody memory issue.

Many thanks in advance,

Moore
Attached Files
File Type: zip address_book.zip (5.6 KB, 81 views)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
coolice is offline Offline
9 posts
since Jul 2004
Aug 30th, 2004
0

Re: access Windows Address Book

Quote originally posted by coolice ...
Hi !

Many thanks, but this one I had found few weeks ago at Code project, and the trouble is that after it was able to compile, it runs perfectly, but as soon as I close the application it generates error.

I am 100% sure that it has bugs with freeing memory, but I wasnt able to find it.

Could you help me to compile and locate the problem ?

I had really tried all other possibilities and I got lost.

I had attached the complete project ZIPed for BCB, which I had been cleared and organised, so it run and compile perfectly, but this bloody memory issue.

Many thanks in advance,

Moore
=======================================================

Hi,

I had the same problem with the code. I replaced the memory management calls with more appropriate ones (I guess they are ...)

>>>>>>>>>
// Add the information to the vector variable
if (!thisAddr.EmailAddr.empty())
this->AddrMemBook.push_back(thisAddr);

// lpWABObject->FreeBuffer(lpRow);

} // End of cycling through rows

// the destroys an SRowSet structure and frees associated memory i.e. all the rows
FreeProws(lpRowSet); // ##############
// lpWABObject->FreeBuffer(lpRowSet);

if (lpABContainer != NULL) // ############
lpABContainer->Release();
} // if (ulObjType == MAPI_ABCONT)

<<<<<<<<<<<

... also release the interface ...

and released passed interfaces in the destructor

>>>>>>>>>>>

CWinAddrBook::~CWinAddrBook(void)
{
// Free the library we use to get the WAB file


// Causes execption when freeing the library
// Look for using DLLs in AX Components !!!
this->lpAddrBook->Release(); // +++++++++++++++++++
this->lpWABObject->Release(); // ++++++++++++++++++

if (hModule != NULL)
FreeLibrary(hModule);

IsInit = false;
}

<<<<<<<<<<<

I hope thad you have already fixed minor bug with 'Growing the vector'
which caused empty entries in the vector ...
and either commented it out or even added

AddrMemBook.clear();

Myself, I created an ActiveX Dll and used this code in it without any problem ( ... so far ) ...

... hope it helps
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ivco39 is offline Offline
1 posts
since Aug 2004
Aug 31st, 2004
0

Re: access Windows Address Book

Quote originally posted by ivco39 ...
=======================================================
I hope thad you have already fixed minor bug with 'Growing the vector'
which caused empty entries in the vector ...
and either commented it out or even added

AddrMemBook.clear();

Myself, I created an ActiveX Dll and used this code in it without any problem ( ... so far ) ...

... hope it helps

Hi !

Extremely thanks for your post, as I got lost completely. Could you do me a big big favour and post the complete BCB code zipped or send it to my email please : coolice@freemail.hu

Many many thanks in advance,

Moore
Reputation Points: 10
Solved Threads: 0
Newbie Poster
coolice is offline Offline
9 posts
since Jul 2004
Sep 25th, 2009
0

Re: access Windows Address Book

Hello.
Please, say, how can I obtain a contact's Birthdate? The standart export from Addres Book does not export it.

If I'm writing a code, there is a property PR_BIRTHDATE.
How do I convert it to the text string and write anywhere, like
Edit3.Text = lpProp->Value.(something here) ?;

Besides, why does MSDN say "Do not use" about all that WAB stuff like WABOpen()?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Enigmo is offline Offline
2 posts
since Sep 2009
Sep 25th, 2009
0

Re: access Windows Address Book

Click to Expand / Collapse  Quote originally posted by Enigmo ...
Hello.
Please, say, how can I obtain a contact's Birthdate? The standart export from Addres Book does not export it.

If I'm writing a code, there is a property PR_BIRTHDATE.
How do I convert it to the text string and write anywhere, like
Edit3.Text = lpProp->Value.(something here) ?;

Besides, why does MSDN say "Do not use" about all that WAB stuff like WABOpen()?
Don't reply to a 5 year old thread, start your own.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Segmentation fault caused by pointers
Next Thread in C Forum Timeline: parallel computing help pls





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC