CB6中读取OUTLOOK中的地址簿

类别:编程语言 点击:0 评论:0 推荐:

首先感谢在这方面研究过的前辈们!

express 和 outlook都能读

cpp前面要声明
#pragma link "Outlook_2K_SRVR"
#pragma link "IvDictio"
#pragma link "IvMulti"
typedef HRESULT (WINAPI *fWABOpen)(LPADRBOOK*,LPWABOBJECT*,LPWAB_PARAM,DWORD);

用了bcb的outlook控件的
代码
try{
                FormList->ListView1->Clear();
                _ApplicationPtr pApp;
                _NameSpace* pNS;
                AddressLists* pAddr;
                AddressEntries * pAddrEntries;
                AddressEntry* pEntry;
                AddressList* pList;
                int count,count1;
                pApp = this->OutlookApplication1->get_Application();
                pNS = pApp->GetNamespace(WideString("MAPI"));
                pAddr = pNS->AddressLists;
                count = pAddr->Count;

                WideString email;

                email.SetLength(128);
                //emailname.SetLength(128);

                for(int i = 1;i <= count;i++)
                {
                        pList = pAddr->Item(TVariant(i));
                        pAddrEntries = pList->get_AddressEntries();
                        count1 = pAddrEntries->Count;
                        for(int j = 1;j <= count1;j++)
                        {
                                pEntry = pAddrEntries->Item(TVariant(j));
                                //pEntry->get_Name(&emailname);
                                pEntry->get_Address(&email);
                                //this->Memo1->Lines->Add(AnsiString(emailname.c_bstr()) + "\t" + AnsiString(email.c_bstr()));
                                TListItem * ListItem;
                                ListItem = FormList->ListView1->Items->Add();
                                ListItem->Caption = "";
                                ListItem->SubItems->Add(AnsiString(email.c_bstr()));

                                ListItem->Checked = true;
                        }

                }
                ListView1->SetFocus();
                ListView1->Update();
        }
        catch(...)
        {
                try{
                        HRESULT hRes;
                        //LPADRBOOK lpAdrBook;
                        DelphiInterface<IAddrBook>  lpAdrBook;
                //LPWABOBJECT lpWABObject;
                        DelphiInterface<IWABObject>  lpWABObject;
                //LPWAB_PARAM lpWABParam = NULL;
                DWORD Reserved2 = NULL;

                        char szPath[MAX_PATH];
                        SHGetFolderPathX(szPath);
                        char * s = "\\System\\wab32.dll";
                        strcat(szPath, s);

                HINSTANCE hinstLib;
                hinstLib = LoadLibrary(szPath);

               // hinstLib = LoadLibrary(s.c_str()+"\\System\\wab32.dll");
                fWABOpen procWABOpen;

                if (hinstLib != NULL)
                {
                // 获取"Wab32.dll"内部涵数WABOpen的进程地址
                procWABOpen = (fWABOpen) GetProcAddress(hinstLib, "WABOpen");

                if (procWABOpen != NULL)
                {
                hRes = (procWABOpen)(&lpAdrBook,&lpWABObject,NULL,Reserved2);
                //_ASSERTE(hRes == S_OK);
                if (hRes != S_OK)
                                                exit(1);

                ULONG lpcbEntryID;
                ENTRYID *lpEntryID;
                hRes = lpAdrBook->GetPAB(
                &lpcbEntryID,
                &lpEntryID
                        );
                //_ASSERTE(hRes == S_OK);
                if (hRes != S_OK)
                                                exit(2);

                ULONG ulFlags = MAPI_BEST_ACCESS;
                ULONG ulObjType = NULL;
                //LPUNKNOWN lpUnk = NULL;
                                        DelphiInterface<IABContainer>  lpContainer;
                hRes = lpAdrBook->OpenEntry(
                                                lpcbEntryID,
                lpEntryID,
                NULL,
                ulFlags,
                &ulObjType,
                (LPUNKNOWN FAR *)&lpContainer
                        );

                ulFlags = NULL;

                if (ulObjType == MAPI_ABCONT)
                {

                //IABContainer *lpContainer = static_cast <IABContainer *>(lpUnk);

                LPMAPITABLE lpTable = NULL;
                hRes = lpContainer->GetContentsTable(
                ulFlags,
                &lpTable
                        );
                //_ASSERT(lpTable);
                ULONG ulRows;
                hRes = lpTable->GetRowCount(0,&ulRows);
                //_ASSERTE(hRes == S_OK);
                SRowSet *lpRows;

                hRes = lpTable->QueryRows(
                ulRows,// 获取所有行
                0,
                &lpRows
                );
                ListView1->Clear();
                for(ULONG i=0;i<lpRows->cRows;i++)
                {
                SRow *lpRow = &lpRows->aRow[i];
                AnsiString strTemp;

                for(ULONG j=0;j<lpRow->cValues;j++)
                {
                SPropValue *lpProp = &lpRow->lpProps[j];

                if (lpProp->ulPropTag == PR_EMAIL_ADDRESS_A)
                                                                {
                                                                        TListItem * ListItem;
                                                                        ListItem = FormList->ListView1->Items->Add();
                                                                        ListItem->Caption = "";
                                                                        ListItem->SubItems->Add((char *)lpProp->Value.lpszA);
                //strTemp =(char *)lpProp->Value.lpszA;

                                                                        ListItem->Checked = true;
                                                                }
                }
                //ListEmail->Lines->Add(strTemp);

                lpWABObject->FreeBuffer(lpRow);
                }
                lpWABObject->FreeBuffer(lpRows);
                }
                }
                //FreeLibrary(hinstLib);(装载和释放 dll 文件的地方改成在主程序执行之前装载,结束的时候释放)
                }


                }
                catch(...)
                {
                        MessageBox(Handle,"没有找到地址薄!","提示信息",MB_OK);
                }
        }

本文地址:http://com.8s8s.com/it/it24086.htm