大家好。我是vc的初学者,刚写一个匿名ftp扫描器,可以实现扫描(如图),但是扫描的过程中我点取消结束不了程序(点取消它显示没有响应),我用的是系统生成的void CScanFtpDlg::OnCancel() ,请各位高手如何才能在扫描的过程中结束程序?在下在这里先谢谢大家了。

解决方案 »

  1.   


    void CScanFtpDlg::OnScanFtpServer() 
    {
    int first, secnod, third, forth;
    char buffer[20];
        _ConnectionPtr pConn = connect_sqlserver();
    GetDlgItem(IDC_SCANNING_STATIC)->SetWindowText("正在扫描,请稍后……");
    for (first = m_StartIP1; (m_StartIP1 <= m_EndIP1); m_StartIP1++)
    {
    for (secnod = m_StartIP2; (m_StartIP2 <= m_EndIP2); m_StartIP2++)
    {
    for (third = m_StartIP3; (m_StartIP3 <= m_EndIP3); m_StartIP3++)
    {
    for (forth = m_StartIP4; (m_StartIP4 <= m_EndIP4); m_StartIP4++)
    {
    m_bConnectionAttempted = true;
    CString IP;
    m_pInetSession = new CInternetSession(AfxGetAppName(), 1, 
    PRE_CONFIG_INTERNET_ACCESS);
    try
    {
    CString IPString2 = _itoa(m_StartIP2, buffer, 10);
    CString IPString3 = _itoa(m_StartIP3, buffer, 10);
    CString IPString4 = _itoa(m_StartIP4, buffer, 10);
    IP = CString(_itoa(m_StartIP1, buffer, 10)) + "."
    + IPString2 + "." + IPString3 + "." + IPString4;
    m_pFtpConnection = m_pInetSession->GetFtpConnection(IP);
    }
    catch(CInternetException *pEx)
    {
    //AfxMessageBox("连接ftp服务器时出现异常!");
    m_pFtpConnection = NULL;
    }
    if (m_pFtpConnection != NULL)
    {
    writein_site(IP,pConn);
    }
    }
    }
    }
    }
    GetDlgItem(IDC_SCANNING_STATIC)->SetWindowText("");
    AfxMessageBox("扫描完毕,匿名站点已添加至数据库中!");
    }vc的初学者,代码写得很烂,大家将就这看吧,请大家给我指导下,谢谢大家了。
      

  2.   

    呵呵,这就很明白啦
    你这个函数还在for循环里跑,你点鼠标它怎么会有反应嘛.
    它不会因为你点button就跳出for循环跑到你的oncancel里去执行的.学习一下多线程技术和事件相应.
      

  3.   

    如果你还不能在短时间内接受线程和事件的相关技术,那还有1个比较简单的办法.OnTimer()用记时器去实现.这个办法比较适合你学习.记时器没执行1次扫描1个目标,记时器的间隔设置短点.不过这种方式效率不会太高地.好学:)处理WM_TIMER消息就行了oninitdialog加上SetTimer(...)就ok了.
      

  4.   


    DWORD WINAPI ThreadProc( LPVOID lpParameter )
    {
    CScanFtpDlg* pDlg = (CScanFtpDlg*)lpParameter;
    int first, secnod, third, forth;
    char buffer[20];
        _ConnectionPtr pConn = pDlg->connect_sqlserver();
    //GetDlgItem(IDC_SCANNING_STATIC)->SetWindowText("正在扫描,请稍后……");
    for (first = pDlg->m_StartIP1; (pDlg->m_StartIP1 <= pDlg->m_EndIP1); pDlg->m_StartIP1++)
    {
    for (secnod = pDlg->m_StartIP2; (pDlg->m_StartIP2 <= pDlg->m_EndIP2); pDlg->m_StartIP2++)
    {
    for (third = pDlg->m_StartIP3; (pDlg->m_StartIP3 <= pDlg->m_EndIP3); pDlg->m_StartIP3++)
    {
    for (forth = pDlg->m_StartIP4; (pDlg->m_StartIP4 <= pDlg->m_EndIP4); pDlg->m_StartIP4++)
    {
    pDlg->m_bConnectionAttempted = true;
    CString IP;
    pDlg->m_pInetSession = new CInternetSession(AfxGetAppName(), 1, 
    PRE_CONFIG_INTERNET_ACCESS);
    try
    {
    CString IPString2 = _itoa(pDlg->m_StartIP2, buffer, 10);
    CString IPString3 = _itoa(pDlg->m_StartIP3, buffer, 10);
    CString IPString4 = _itoa(pDlg->m_StartIP4, buffer, 10);
    IP = CString(_itoa(pDlg->m_StartIP1, buffer, 10)) + "."
    + IPString2 + "." + IPString3 + "." + IPString4;
    pDlg->m_pFtpConnection = pDlg->m_pInetSession->GetFtpConnection(IP);
    }
    catch(CInternetException *pEx)
    {
    //AfxMessageBox("连接ftp服务器时出现异常!");
    pDlg->m_pFtpConnection = NULL;
    }
    if (pDlg->m_pFtpConnection != NULL)
    {
    pDlg->writein_site(IP,pConn);
    }
    if (pDlg->m_ThreadExit)
    {
    ExitThread(0);
    }
    }
    }
    }
    }
    return 0; }
    void CScanFtpDlg::OnScanFtpServer() 
    {
    UpdateData();
    DWORD ThreadID;
    GetDlgItem(IDC_SCANNING_STATIC)->SetWindowText("正在扫描,请稍后……");
    m_hTread = CreateThread(NULL,0,ThreadProc,(LPVOID)this,0,&ThreadID);//产生新的线程,扫描IP
    GetDlgItem(IDC_SCANNING_STATIC)->SetWindowText("");
    AfxMessageBox("扫描完毕,匿名站点已添加至数据库中!");
    }void CScanFtpDlg::OnStop() 
    {
    m_ThreadExit = TRUE;
    }
    大家好。我今天猛看了一天的多线程编程,不管懂不懂,还是凑成了一段代码,但是不知道为什么OnScanFtpServer() 并没有产生新的进程。那位高手帮在下看下吧,星期五就要交这个小项目了,在下万分感谢。
      

  5.   

    MFC要用AfxBeginThread创建线程,否则线程中不能正常使用MFC的类
      

  6.   

    OnScanFtpServer生成的是个线程,怎么会有进程?笔误?没看出来原因,还是要调试着看了
      

  7.   

    线程和进程是不同的,CreateThread就是创建线程,你可以在线程函数的入口处设置断点来调试,应该是可以进入的。