// WebReloadThread.cpp : implementation file // #include "stdafx.h" #include "CheckServer.h" #include "WebReloadThread.h" #include "MainFrm.h" ////////////////////////////////////////////////////////////////////// // MSHTML ÆÄ¼­ #include #include #pragma warning(disable : 4146) //see Q231931 for explaintation #import no_auto_exclude ////////////////////////////////////////////////////////////////////// #include // MFC socket extensions #include #include #pragma comment(lib,"Wininet.lib") #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CWebReloadThread UINT CWebReloadThread::tm_CWebReloadThreadOnDoContinuous = ::RegisterWindowMessage( "CWebReloadThread OnDoContinuous" ); IMPLEMENT_DYNCREATE(CWebReloadThread, CWinThread) CWebReloadThread::CWebReloadThread() { m_pCMainFrame = NULL; m_hStopEvent = ::CreateEvent( NULL, TRUE, FALSE, NULL ); m_strURL[0] = _T(""); m_strURL[1] = _T(""); m_strNews[0] = _T(""); m_strNews[1] = _T(""); m_bFirst[0] = TRUE; m_bFirst[1] = TRUE; m_strError = _T(""); } CWebReloadThread::~CWebReloadThread() { m_pCMainFrame = NULL; } BOOL CWebReloadThread::InitInstance() { // TODO: perform and per-thread initialization here AfxOleInit(); return TRUE; } int CWebReloadThread::ExitInstance() { // TODO: perform any per-thread cleanup here return CWinThread::ExitInstance(); } BEGIN_MESSAGE_MAP(CWebReloadThread, CWinThread) //{{AFX_MSG_MAP(CWebReloadThread) // NOTE - the ClassWizard will add and remove mapping macros here. ON_REGISTERED_THREAD_MESSAGE( tm_CWebReloadThreadOnDoContinuous, OnDoContinuous ) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CWebReloadThread message handlers //DEL int CWebReloadThread::Run() //DEL { //DEL // TODO: Add your specialized code here and/or call the base class //DEL //DEL return CWinThread::Run(); //DEL } //void CMyThread::OnDoTest( WPARAM wParam, LPARAM lParam ) //{ // ASSERT( m_pCheckServerView ); // make sure we have the dialog to send it back to. // char* pchar = (char*) wParam; // CString s; // s.Format( "%d", atol( pchar ) + 1 ); // delete pchar; // pchar = NULL; // pchar = new char[ s.GetLength( ) + 1 ]; // strcpy( pchar, s ); // // //m_pCheckServerView->TestCompleted( pchar ); // // CStatic *p = (CStatic*)m_pCheckServerView->GetDlgItem(IDC_MY_TEXT); // p->SetWindowText(pchar); //} void CWebReloadThread::OnDoContinuous( WPARAM wParam, LPARAM lParam ) { ASSERT( m_pCMainFrame ); // make sure we have the dialog to send it back to. char* pchar = (char*) wParam; CString s; s.Format( "%d\n", atol( pchar ) + 1 ); delete pchar; TRACE( s ); m_strNews[0] = _T(""); m_strNews[1] = _T(""); m_bFirst[0] = TRUE; m_bFirst[1] = TRUE; char *location1; char *location2; LPTSTR pppp1 = m_strURL[0].GetBuffer(m_strURL[0].GetLength()); location1 = pppp1; LPTSTR pppp2 = m_strURL[1].GetBuffer(m_strURL[1].GetLength()); location2 = pppp2; if (_strnicmp(location1, "http://",7)==0 && _strnicmp(location2, "http://",7)==0) { HINTERNET hInternet=InternetOpen("Daemon", INTERNET_OPEN_TYPE_PRECONFIG , NULL, NULL, NULL); if (hInternet) { while( ::WaitForSingleObject( m_hStopEvent, 100 ) == WAIT_TIMEOUT ) { WebReload( hInternet, location1, m_bFirst[0], m_strNews[0] ); WebReload( hInternet, location2, m_bFirst[1], m_strNews[1] ); } } else { m_strError = "The sURL location must contain 'http://' string"; } InternetCloseHandle( hInternet ); } ::ResetEvent( m_hStopEvent ); } BOOL CWebReloadThread::WebReload( HINTERNET hInternet, char * szUrl, BOOL &bFirst, CString &strNews ) { // HINTERNET file_handle=InternetOpenUrl( hInternet, // szUrl, NULL, 0, // INTERNET_FLAG_KEEP_CONNECTION & INTERNET_FLAG_DONT_CACHE & INTERNET_FLAG_RELOAD, 0); HINTERNET file_handle=InternetOpenUrl(hInternet, szUrl, NULL, 0, INTERNET_FLAG_RELOAD, 0); if (file_handle) { #ifdef _DEBUG // Beep(200, 200); #endif memset(m_szPageData, NULL, sizeof(m_szPageData)); char *pBuff = m_szPageData; DWORD bytes_read=0; DWORD bytes_read_tot=0; BOOL bSuccess = TRUE; while(bSuccess) { bSuccess = InternetReadFile(file_handle, pBuff, 1024, &bytes_read); if( bytes_read == 0 ) break; bytes_read_tot += bytes_read; if( bytes_read_tot < PAGE_READMAX ) pBuff += bytes_read; } m_szPageData[bytes_read_tot] = 0; //in szPageData am &bytes_read bytes de pe internet //tre sa bag szPageData in sBody // FILE *fp; // if ((fp=fopen("C:\\HTTPFILE.TXT","wb")) !=NULL) // { // fwrite(m_szPageData, bytes_read, 1, fp); // fclose(fp); // } // CString strBody = CString (m_szPageData); // strBody = strBody.Left(bytes_read_tot); // long lll= strBody.GetLength(); PageParser( m_szPageData, bFirst, strNews ); //Sleep(0); //AfxMessageBox("GetPage: "+ sURL + " "+ sBody); } else { m_strError = "Is not possible to connect to internet"; return FALSE; } ::InternetCloseHandle( file_handle ) ; return TRUE; } BOOL CWebReloadThread::PageParser(char * szBody, BOOL &bFirst, CString &strNews) { BOOL bRet = TRUE; try { //declare our MSHTML variables and create a document MSHTML::IHTMLDocument2Ptr pDoc; MSHTML::IHTMLDocument3Ptr pDoc3; MSHTML::IHTMLElementCollectionPtr pCollection; MSHTML::IHTMLElementPtr pElement; HRESULT hr = CoCreateInstance(CLSID_HTMLDocument, NULL, CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, (void**)&pDoc); if (SUCCEEDED(hr)) { //put the code into SAFEARRAY and write it into document SAFEARRAY* psa = SafeArrayCreateVector(VT_VARIANT, 0, 1); VARIANT *param; bstr_t bsData = (LPCTSTR)szBody; hr = SafeArrayAccessData(psa, (LPVOID*)¶m); param->vt = VT_BSTR; param->bstrVal = (BSTR)bsData; hr = pDoc->write(psa); hr = pDoc->close(); SafeArrayDestroy(psa); //I'll use IHTMLDocument3 to retrieve tags. Note it is available only in IE5+ //If you don't want to use it, u can just run through all tags in HTML //(IHTMLDocument2->all property) pDoc3 = pDoc; CString NewNews = ""; //display HREF parameter of every link (A tag) in ListBox pCollection = pDoc3->getElementsByTagName(L"A"); for(long i=0; ilength; i++) { pElement = pCollection->item(i, (long)0); if(pElement != NULL) { //second parameter says that you want to get text inside attribute as is bstr_t bstrTag = pElement->getAttribute("href", 2); // m_wndLinksList.AddString( (LPCTSTR)bstrTag ); if( strncmp( (LPCTSTR)bstrTag, "javascript:viewCrp", 18 ) == 0 ) // ȸ»ç¸í. { CComBSTR bstrText; pElement->get_innerText( &bstrText ); NewNews = bstrText; NewNews += "@"; } else if( strncmp( (LPCTSTR)bstrTag, "javascript:viewDoc", 18 ) == 0 ) // °ø½Ã³»¿ë. { if( NewNews != "" ) { CComBSTR bstrText; pElement->get_innerText( &bstrText ); NewNews += CString(bstrText); if( bFirst ) { bFirst = FALSE; strNews = NewNews; break; } #ifdef _DEBUG TRACE2( "%s, %s\n", strNews, NewNews ); #endif if( strNews != NewNews ) { strNews = NewNews; Beep(2000,200); char * pchar = new char[ NewNews.GetLength( ) + 1 ]; strcpy( pchar, NewNews ); m_pCMainFrame->WebNewsFinded( pchar ); return TRUE; } } return FALSE; } } } } } catch( _com_error e ) { TRACE("CWebReloadThread::PageParser catch(_com_error)\n"); } catch(...) { TRACE("CWebReloadThread::PageParser catch(...)\n"); } return FALSE; } /* 21:02   ÇѸ¶À½»óÈ£ÀúÃàÀºÇà   °¨»çº¸°í¼­Á¦Ãâ   ÇѸ¶À½»óÈ£ÀúÃàÀºÇà 2004.09.18 */