因为做一个PIM项目, 所以需要向outlook express的address book加记录 , 网上大多是获取addressbook的代码, 添加记录的代码有些不能用,所以根据msdn写了一段代码,和大家分享一下, 下面的代码可以直接使用。
HRESULT hr;
LPADRBOOK lppAdrBook= NULL;
ULONG ulEntryID= 0;
LPENTRYID lpEntryID= NULL;
LPABCONT lpContainer= NULL;
ULONG ulObjType= 0;
LPUNKNOWN lpUnk= NULL;
//Opens the default WAB file in the system and loads it into the object
HINSTANCE hinstWAB=NULL;
hr = E_FAIL;
HKEY keyResult;
BYTE keyValue[MAX_PATH];
DWORD dataout=800;
fWABOpen ssWABOpen;
RegOpenKeyEx(HKEY_LOCAL_MACHINE, WAB_DLL_PATH_KEY, 0, KEY_ALL_ACCESS, &keyResult); //"Software/Microsoft/WAB/DLLPath"
long result = RegQueryValueEx(keyResult, "", 0, 0, keyValue, &dataout);
RegCloseKey(keyResult);
//Get the full path of WAB and store in PathToWAB
char PathToWAB[255];
strcpy(PathToWAB, (char*)keyValue);
LPWABOBJECT lpWABObject;
//Now let us load the library
HMODULE hModule = LoadLibrary(PathToWAB);
if (hModule!=NULL)
{
//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)
{
ASSERT(ssWABOpen != NULL);
}
//It is successful call it
HRESULT hr = (ssWABOpen)(&lppAdrBook, &lpWABObject, NULL, 0);
ASSERT(ssWABOpen != NULL);
}
ULONG lpcbEntryID; //ENTRYID* lpEntryID;
hr = lppAdrBook->GetPAB(&lpcbEntryID, &lpEntryID);
ASSERT(hr != S_OK); //error opening the darn PAB
//Declare variables for MAPI and specific access to the PAB
ULONG ulFlags = MAPI_BEST_ACCESS;
ulObjType = NULL;
LPUNKNOWN lpIUnknown = NULL;
hr = lppAdrBook->OpenEntry(lpcbEntryID,
lpEntryID,
NULL,
ulFlags,
&ulObjType,
&lpIUnknown);
lpContainer = (LPABCONT)lpIUnknown;
//´¦ÀíÊôÐÔÊý¾Ý
char szDisplayName[128] = "shenzf";
char szGivenName[128]= "Jeff";
char szAddrType[]= "SMTP";
char szMailAddr[128]= "Jeff.net";
SPropValue arrProp[4];
memset(arrProp, 0, sizeof(SPropValue)*4);
arrProp[0].ulPropTag = PR_ADDRTYPE;
arrProp[0].Value.lpszA = szAddrType;
arrProp[1].ulPropTag = PR_DISPLAY_NAME;
arrProp[1].Value.lpszA = szDisplayName;
arrProp[2].ulPropTag = PR_EMAIL_ADDRESS;
arrProp[2].Value.lpszA = szMailAddr;
arrProp[3].ulPropTag = PR_SURNAME;
arrProp[3].Value.lpszA = szGivenName;
//create new address book
LPMAPIPROP lpProp = NULL;
hr = lpContainer->CreateEntry(0, NULL, 0, &lpProp);
if(hr == S_OK)
{
hr = lpProp->SetProps(4, arrProp, NULL);
ASSERT(hr == S_OK);
hr = lpProp->SaveChanges(0);
lpProp->Release();
}
else
{
LPMAPIERROR lpError= NULL;
lpContainer->GetLastError(hr, 0, &lpError);
if(lpError != NULL)
{
CString strErr;
strErr = "Error:";
if(lpError->lpszError != NULL)
strErr += lpError->lpszError;
strErr += " Content:";
if(lpError->lpszComponent != NULL)
strErr += lpError->lpszComponent;
MessageBox(strErr, "Add Error", MB_OK|MB_ICONINFORMATION);
}
}
lpContainer->Release();
MAPIFreeBuffer(lpEntryID);
lppAdrBook->Release();
LPADRBOOK lppAdrBook= NULL;
ULONG ulEntryID= 0;
LPENTRYID lpEntryID= NULL;
LPABCONT lpContainer= NULL;
ULONG ulObjType= 0;
LPUNKNOWN lpUnk= NULL;
//Opens the default WAB file in the system and loads it into the object
HINSTANCE hinstWAB=NULL;
hr = E_FAIL;
HKEY keyResult;
BYTE keyValue[MAX_PATH];
DWORD dataout=800;
fWABOpen ssWABOpen;
RegOpenKeyEx(HKEY_LOCAL_MACHINE, WAB_DLL_PATH_KEY, 0, KEY_ALL_ACCESS, &keyResult); //"Software/Microsoft/WAB/DLLPath"
long result = RegQueryValueEx(keyResult, "", 0, 0, keyValue, &dataout);
RegCloseKey(keyResult);
//Get the full path of WAB and store in PathToWAB
char PathToWAB[255];
strcpy(PathToWAB, (char*)keyValue);
LPWABOBJECT lpWABObject;
//Now let us load the library
HMODULE hModule = LoadLibrary(PathToWAB);
if (hModule!=NULL)
{
//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)
{
ASSERT(ssWABOpen != NULL);
}
//It is successful call it
HRESULT hr = (ssWABOpen)(&lppAdrBook, &lpWABObject, NULL, 0);
ASSERT(ssWABOpen != NULL);
}
ULONG lpcbEntryID; //ENTRYID* lpEntryID;
hr = lppAdrBook->GetPAB(&lpcbEntryID, &lpEntryID);
ASSERT(hr != S_OK); //error opening the darn PAB
//Declare variables for MAPI and specific access to the PAB
ULONG ulFlags = MAPI_BEST_ACCESS;
ulObjType = NULL;
LPUNKNOWN lpIUnknown = NULL;
hr = lppAdrBook->OpenEntry(lpcbEntryID,
lpEntryID,
NULL,
ulFlags,
&ulObjType,
&lpIUnknown);
lpContainer = (LPABCONT)lpIUnknown;
//´¦ÀíÊôÐÔÊý¾Ý
char szDisplayName[128] = "shenzf";
char szGivenName[128]= "Jeff";
char szAddrType[]= "SMTP";
char szMailAddr[128]= "Jeff.net";
SPropValue arrProp[4];
memset(arrProp, 0, sizeof(SPropValue)*4);
arrProp[0].ulPropTag = PR_ADDRTYPE;
arrProp[0].Value.lpszA = szAddrType;
arrProp[1].ulPropTag = PR_DISPLAY_NAME;
arrProp[1].Value.lpszA = szDisplayName;
arrProp[2].ulPropTag = PR_EMAIL_ADDRESS;
arrProp[2].Value.lpszA = szMailAddr;
arrProp[3].ulPropTag = PR_SURNAME;
arrProp[3].Value.lpszA = szGivenName;
//create new address book
LPMAPIPROP lpProp = NULL;
hr = lpContainer->CreateEntry(0, NULL, 0, &lpProp);
if(hr == S_OK)
{
hr = lpProp->SetProps(4, arrProp, NULL);
ASSERT(hr == S_OK);
hr = lpProp->SaveChanges(0);
lpProp->Release();
}
else
{
LPMAPIERROR lpError= NULL;
lpContainer->GetLastError(hr, 0, &lpError);
if(lpError != NULL)
{
CString strErr;
strErr = "Error:";
if(lpError->lpszError != NULL)
strErr += lpError->lpszError;
strErr += " Content:";
if(lpError->lpszComponent != NULL)
strErr += lpError->lpszComponent;
MessageBox(strErr, "Add Error", MB_OK|MB_ICONINFORMATION);
}
}
lpContainer->Release();
MAPIFreeBuffer(lpEntryID);
lppAdrBook->Release();
下一篇会写向outlook 2003以后版本 添加记录的方法。 和outlook express 不同。