void CXhDlg::OnButton1() 
{
// TODO: Add your control notification handler code here
start.SetEvent();
return;
}void CXhDlg::OnButton2() 
{
// TODO: Add your control notification handler code here
stop.SetEvent();
return;

}UINT CXhDlg::ThreadProc(LPVOID lParam)
{
WaitForSingleObject(start,INFINITE);
CInternetSession session;
CHttpFile *myHttpFile = NULL;

_ConnectionPtr m_pConnection;
m_pConnection.CreateInstance(__uuidof(Connection));
CString conn;
conn.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb");
m_pConnection->Open(_bstr_t(conn),"","",adModeUnknown);
_variant_t RecordsAffected;

int type=0;
while( type++ < 80 )
{
bool flag = true;
int page=1;
while(flag)
{
WaitForSingleObject(stop,0);
CString m_SiteName;
m_SiteName.Format("%s%d%s%d","http://mmsg.qq.com/cgi-bin/gddylist?Type=",type,"&Sort=1&Page=",page++);

CString m_SiteInfo;
CString myData;
try
{
myHttpFile = (CHttpFile*)session.OpenURL(m_SiteName);
}
catch(CInternetException* m_pException)
{
delete myHttpFile;
m_pException->Delete();
return 0;
}
if ( myHttpFile )
{
while( myHttpFile->ReadString( myData ) )
{
m_SiteInfo+=myData;
}
}

int x1 = m_SiteInfo.Find("<div align=\"center\">");
int x2 = m_SiteInfo.Find("</div>",x1);
int x3 = m_SiteInfo.Find("/",x1);
CString s1 = m_SiteInfo.Mid(x1+20,x3-x1-20);
CString s2 = m_SiteInfo.Mid(x3+1,x2-x3-1);
if (s1 =="0" )
break;
if (s1 == s2)
{
flag=false;
} int t1 = m_SiteInfo.Find("&gt;&gt;",x1-100)+8;
int t2 = m_SiteInfo.Find("</td>",t1);
CString xtype = m_SiteInfo.Mid(t1,t2-t1); int find=0,find1=m_SiteInfo.Find("<td width=\"35%\" bgcolor=",x1)+20;
m_SiteInfo = m_SiteInfo.Right(m_SiteInfo.GetLength()-find1);
while( (find=m_SiteInfo.Find( "<td width=\"35%\" bgcolor=" , 1)) > 0 )
{
int find1 = m_SiteInfo.Find( ">",find)+1;
int find2 = m_SiteInfo.Find( "</td>" , find1);
CString content=m_SiteInfo.Mid( find1,find2-find1 );//主体
content.Replace("'","''");
content.Replace("\"","\"\"");

m_SiteInfo = m_SiteInfo.Right(m_SiteInfo.GetLength()-find2); CString sql;
sql.Format("insert into xh (xtype,content) values ('%s','%s')",xtype,content);
m_pConnection->Execute(_bstr_t(sql), &RecordsAffected, adCmdText);
::Sleep(200);
}
}
}
myHttpFile->Close();
session.Close();
MessageBox("完成");
return 0;
}

解决方案 »

  1.   

    在这个函数里启动的线程
    BOOL CXhDlg::OnInitDialog()
    {
    CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range.
    ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
    ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE);
    if (pSysMenu != NULL)
    {
    CString strAboutMenu;
    strAboutMenu.LoadString(IDS_ABOUTBOX);
    if (!strAboutMenu.IsEmpty())
    {
    pSysMenu->AppendMenu(MF_SEPARATOR);
    pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
    }
    } // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE); // Set big icon
    SetIcon(m_hIcon, FALSE); // Set small icon

    // TODO: Add extra initialization here
    HWND hWnd=GetSafeHwnd();
    AfxBeginThread(ThreadProc,hWnd);
    return TRUE;  // return TRUE  unless you set the focus to a control
    }
      

  2.   

    错误提示:
    --------------------Configuration: xh - Win32 Release--------------------
    Compiling...
    xhDlg.cpp
    D:\VC\xh\xhDlg.cpp(123) : error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)'
    Error executing cl.exe.xh.exe - 1 error(s), 0 warning(s)
      

  3.   

    UINT CXhDlg::ThreadProc(LPVOID lParam)

      CoInitialize(NULL);
      {
        .....
      } CoUninitialize();
    }
      

  4.   

    static WINAPI DWORD ThreadProc(LPVOID lParam);
      

  5.   

    CXhDlg::ThreadProc应该是个静态函数
      

  6.   

    static UINT CXhDlg::ThreadProc(LPVOID lParam)
    {
    WaitForSingleObject(((CXhDlg *)lParam)->start,INFINITE);
    CoInitialize(NULL);
    CInternetSession session;
    CHttpFile *myHttpFile = NULL;

    _ConnectionPtr m_pConnection;
    m_pConnection.CreateInstance(__uuidof(Connection));
    CString conn;
    conn.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db1.mdb");
    m_pConnection->Open(_bstr_t(conn),"","",adModeUnknown);
    _variant_t RecordsAffected;

    int type=0;
    while( type++ < 80 )
    {
    bool flag = true;
    int page=1;
    while(flag)
    {
    WaitForSingleObject(((CXhDlg *)lParam)->stop,0);
    CString m_SiteName;
    m_SiteName.Format("%s%d%s%d","http://mmsg.qq.com/cgi-bin/gddylist?Type=",type,"&Sort=1&Page=",page++);

    CString m_SiteInfo;
    CString myData;
    try
    {
    myHttpFile = (CHttpFile*)session.OpenURL(m_SiteName);
    }
    catch(CInternetException* m_pException)
    {
    delete myHttpFile;
    m_pException->Delete();
    return 0;
    }
    if ( myHttpFile )
    {
    while( myHttpFile->ReadString( myData ) )
    {
    m_SiteInfo+=myData;
    }
    }

    int x1 = m_SiteInfo.Find("<div align=\"center\">");
    int x2 = m_SiteInfo.Find("</div>",x1);
    int x3 = m_SiteInfo.Find("/",x1);
    CString s1 = m_SiteInfo.Mid(x1+20,x3-x1-20);
    CString s2 = m_SiteInfo.Mid(x3+1,x2-x3-1);
    if (s1 =="0" )
    break;
    if (s1 == s2)
    {
    flag=false;
    } int t1 = m_SiteInfo.Find("&gt;&gt;",x1-100)+8;
    int t2 = m_SiteInfo.Find("</td>",t1);
    CString xtype = m_SiteInfo.Mid(t1,t2-t1); int find=0,find1=m_SiteInfo.Find("<td width=\"35%\" bgcolor=",x1)+20;
    m_SiteInfo = m_SiteInfo.Right(m_SiteInfo.GetLength()-find1);
    while( (find=m_SiteInfo.Find( "<td width=\"35%\" bgcolor=" , 1)) > 0 )
    {
    int find1 = m_SiteInfo.Find( ">",find)+1;
    int find2 = m_SiteInfo.Find( "</td>" , find1);
    CString content=m_SiteInfo.Mid( find1,find2-find1 );//&Ouml;÷&Igrave;&aring;
    content.Replace("'","''");
    content.Replace("\"","\"\"");

    m_SiteInfo = m_SiteInfo.Right(m_SiteInfo.GetLength()-find2); CString sql;
    sql.Format("insert into xh (xtype,content) values ('%s','%s')",xtype,content);
    m_pConnection->Execute(_bstr_t(sql), &RecordsAffected, adCmdText);
    ::Sleep(200);
    }
    }
    }
    myHttpFile->Close();
    session.Close();
    CoUninitialize();
    return 0;
    }改成上面的代码,错误提示为:
    --------------------Configuration: xh - Win32 Release--------------------
    Compiling...
    xhDlg.cpp
    D:\VC\xh\xhDlg.cpp(192) : error C2724: 'ThreadProc' : 'static' should not be used on member functions defined at file scope
    Error executing cl.exe.xh.exe - 1 error(s), 0 warning(s)
      

  7.   

    XhDlg.hclass CXhDlg:public CDialog
    {
      
      public:
         static WINAPI UINT ThreadProc1(LPVOID lParam);
         }XhDlg.cpp
    BOOL CXhDlg::OnInitDialog()
    {
    CDialog::OnInitDialog();        ......
    //HWND hWnd=GetSafeHwnd();
    AfxBeginThread(ThreadProc1,(LPVOID)this);
    return TRUE;  // return TRUE  unless you set the focus to a control
    }UINT CXhDlg::ThreadProc1(LPVOID lParam)

      CXhDlg *p=(CXhDlg*)lParam;
      CoInitialize(NULL);
      {
          p->ThreadProc();
      }
      CoUninitialize();
      return 0;
    }UINT CXhDlg::ThreadProc(LPVOID lParam)
    {
       你的代码。。
    }
      

  8.   

    zfive5(醉马不肖 之 [孤舟蓑笠翁, 独钓寒江雪]) 
    你好,你的代码我还是调不通,您调过的吗?