在线程中读取Outlook地址本
在读取的工作类中使用了 std::deque<std::string>,在ActiveX类中使用 std::vector<std::string>存放读取到的编排成xml格式的联系人数据。fireEvent时pop_back.
html叶面中的写法是:
<SCRIPT language="jscript" ID="readerObjScript" FOR="readerObj" EVENT="ReturnContactsXML">
<!--
//初始化数据
try
{
var xmlContent = document.forms[0].readerObj.GetContacts();
if(xmlContent && xmlContent.length > 0)
{
var num = TextBoxXML.innerText;
TextBoxXML.innerText = parseInt(num) + 100;
}
}
catch(e)
{
//alert("发生错误,这可能是由于程序不支持从您默认的邮件客户端导入数据,\n" +
//window.close();
}
//-->
</SCRIPT>
<OBJECT id="readerObj" codeBase="WebABReader.ocx#version=1,0,0,1" height="0" width="0" classid="CLSID:272F256D-5E0B-4AC7-9739-C0E257CAD9E9"
VIEWASTEXT>
<PARAM NAME="_Version" VALUE="65536">
<PARAM NAME="_ExtentX" VALUE="26">
<PARAM NAME="_ExtentY" VALUE="26">
<PARAM NAME="_StockProps" VALUE="0">
</OBJECT>
<textarea id="TextBoxXML" cols=70 rows=18>0</textarea>
<script language="javascript">
<!--
///////////////////////全局数据/////////////////////////
//保存xml数据的文本框编号
var gXmlTextBoxId;
InitGlobal('TextBoxXML');
////////////////////////////////////////////////////////
//初始化全局变量
function InitGlobal(xmlTextBoxId)
{
gXmlTextBoxId = xmlTextBoxId;
window.setTimeout(doLoadContact,1000);
}
function doLoadContact()
{
try
{
document.forms[0].readerObj.DoReadContacts();
//alert('done!!');
//parent.notifyDone();
}
catch(e)
{
alert(e.message);
alert("导入发生错误,这可能是由于您的浏览器设置不允许下载完成此功能的控件");
}
}
-->
</script>
DEFAULT_MAIL_CLIENT GetDefaultMailClient()
{
TCHAR m_szDefMailClient[_MAX_PATH + 1] = _T("SOFTWARE\\Clients\\Mail");
LONG retval;
RegKey_ regKey;
retval = regKey.Open(HKEY_LOCAL_MACHINE, m_szDefMailClient);
DEFAULT_MAIL_CLIENT result = NONE;
if(retval == NO_ERROR)
{
DWORD cbData = _MAX_PATH;
retval = regKey.ReadString(m_szDefMailClient, cbData);
char * pStr = strupr(m_szDefMailClient);
if(! strcmp(pStr,"OUTLOOK EXPRESS") )
result = OUTLOOKEXPRESS; //outlook express
else if(! strcmp(pStr, "MICROSOFT OUTLOOK") )
result = OUTLOOK; //outlook
}
return result;
}
//读取联系人
LRESULT CWebABReaderCtrl::OnReadContactsDone(WPARAM wParam, LPARAM lParam)
{
if(lParam)
{
//m_strContactsXML = new char[XML_BUF_SIZ];
//strcpy(this->m_strContactsXML,(char*)lParam);
m_cs.Lock();
this->m_contactsList.push_back(m_pOutlookABGetter->m_contactsList.front());
m_pOutlookABGetter->m_contactsList.pop_front();
m_cs.Unlock();
FireReturnContactsXML();
//delete m_strContactsXML;
//m_strContactsXML = NULL;
return 0L;
}
return 1L;
}
void CWebABReaderCtrl::FireReturnContactsXML()
{
FireEvent(eventidReturnContactsXML,EVENT_PARAM(VTS_NONE));
//GetContacts();
}
void CWebABReaderCtrl::DoReadContacts()
{
//int nBuffSize = 256*1024; //读256K
//m_strContactsXML = new char[nBuffSize];
//m_strContactsXML[0] = '\0';
DEFAULT_MAIL_CLIENT defaultClient = GetDefaultMailClient();
switch(defaultClient)
{
case OUTLOOKEXPRESS: //outlook express
{
COEABReader oeReader;
//oeReader.GetContacts( m_strContactsXML );
}
break;
case OUTLOOK:
{
//HRESULT hRes;
// 接口列集
//hRes = CoMarshalInterThreadInterfaceInStream(IID_DWebABReader, GetIDispatch(FALSE), &m_pStream);
CWinThread * t = AfxBeginThread(_Apartment, (LPVOID)this);
t->ResumeThread();
//DoReadFromOutlook();
}
break;
default:
MessageBox("默认的邮件客户端程序不是Outlook或者OutlookExpress");
break;
}
//if(strlen(this->m_strContactsXML))
//{
// this->FireReturnContactsXML();
//}
//delete[] m_strContactsXML;
//m_strContactsXML = NULL;
}
void CWebABReaderCtrl::DoReadFromOutlook()
{
m_pOutlookABGetter = new COutlookContactsGetter(this->m_hWnd);
if(m_pOutlookABGetter->IsInitOK())
{
m_pOutlookABGetter->GetContacts();
//strcpy(m_strContactsXML,xml.c_str());
}
}
BSTR CWebABReaderCtrl::GetContacts()
{
CString result = "";
if( ! m_contactsList.empty())
{
m_cs.Lock();
std::string xml = m_contactsList.back();
result = xml.c_str();
//::MessageBox(NULL,xml.c_str(),"",MB_OK);
m_contactsList.pop_back();
m_cs.Unlock();
}
return result.AllocSysString();
}
DWORD CWebABReaderCtrl::Apartment()
{
CoInitialize(NULL);
//HRESULT hRes;
//m_spT.Release();
//接口散集
//if (m_pStream)
//hRes = CoGetInterfaceAndReleaseStream(m_pStream, IID_DWebABReader, (void**)&m_spT);
//m_spT->DoReadFromOutlook();
DoReadFromOutlook();
//m_spT.Release();
CoUninitialize();
return 0;
}
UINT CWebABReaderCtrl::_Apartment(void* pv)
{
CWebABReaderCtrl* pThis = (CWebABReaderCtrl*) pv;
pThis->Apartment();
return 0;
}