定义的变量
public:
CEdit m_ediAddress;



  CListBox m_lboxRecord;
afx_msg void OnPingbutton();
afx_msg void OnStopbutton();
  afx_msg LRESULT    OnPing(WPARAM wParam, LPARAM lParam);
  CString m_strAddress;
  CString m_strPing;
  CString m_strReply;
  CString m_strCome;
  CString m_strStatistics; CWnd *m_pMainWnd;
CWinThread *m_pThread;
bool m_bExitThread;
UINT m_iBagSize;
UINT m_iTimeOut;
UINT m_iTryTimes;
private:

};
struct INFORMATION {
CString *S_pstrPing;
CString *S_pstrReply;
CString *S_pstrCome;
CString *S_pstrStatistics;
UINT     *S_piTimeout;
UINT     *S_piBagSize;
UINT     *S_piTryTimes;
CString  *S_pstrAddress;
CWnd *S_pMainWnd;
bool *S_pbExitThread;
};下面是对onpingbutton的响应和线程函数
void CMyPingDlg::OnPingbutton()
{
UpdateData(); m_ediAddress.GetWindowText(m_strAddress);
::MessageBox(NULL,m_strAddress.GetBuffer(),NULL,0);
void *p;
INFORMATION Struct_information;
Struct_information.S_pMainWnd = (CWnd*)this;
Struct_information.S_pstrAddress =&m_strAddress; 
Struct_information.S_piTryTimes = &m_iTryTimes;
Struct_information.S_piTimeout = &m_iTimeOut;
Struct_information.S_piBagSize = &m_iBagSize;
Struct_information.S_pstrPing = &m_strPing;
Struct_information.S_pstrReply = &m_strReply;
Struct_information.S_pstrCome = &m_strCome;
Struct_information.S_pstrStatistics = &m_strStatistics;
Struct_information.S_pbExitThread = &m_bExitThread;
p  = &Struct_information; m_pThread = AfxBeginThread(&CMyPingDlg::MyThread,p);
if(!m_pThread)
return;
CButton *button = (CButton*)GetDlgItem(IDC_PINGBUTTON);
if (button) button->EnableWindow(FALSE);
//注释这个会出现问题的可能性好大
//不注释这个出现的可能性比较小
Sleep(1);
//button->EnableWindow(TRUE);
}void CMyPingDlg::OnStopbutton()
{
// TODO: 在此添加控件通知处理程序代码
m_bExitThread = true;
}

解决方案 »

  1.   

    UINT CMyPingDlg::MyThread(LPVOID pParam)
    {
    INFORMATION *pInfor = (INFORMATION*)pParam;

    CString *pstrAddress = pInfor->S_pstrAddress;
    UINT     *piTimeOut = pInfor->S_piTimeout;
    UINT    *piTryTimes = pInfor->S_piTryTimes ;
    UINT     *piBagSize  = pInfor->S_piBagSize;
    CString *pstrReply = pInfor->S_pstrReply;
    CString *pstrCome = pInfor->S_pstrCome ;
    CString *pstrStatistics = pInfor->S_pstrStatistics ;
    CString *pstrPing = pInfor->S_pstrPing;
    bool *pbExitThread = pInfor->S_pbExitThread;
    CMyPingDlg* pMainWnd = (CMyPingDlg*)pInfor->S_pMainWnd;//CMyPingDlg* pMainWnd = (CMyPingDlg*)pParam;
    //CEdit *pediAddress = &pMainWnd->m_ediAddress;
    //UINT     *piTimeOut  = &pMainWnd->m_iTimeOut;
    //UINT    *piTryTimes  = &pMainWnd->m_iTryTimes;
    //UINT     *piBagSize  = &pMainWnd->m_iBagSize;
    //CString *pstrReply  = &pMainWnd->m_strReply;
    //CString *pstrCome    = &pMainWnd->m_strCome;
    //CString *pstrStatistics = &pMainWnd->m_strStatistics;
    //CString *pstrPing    = &pMainWnd->m_strPing;
    //bool *pbExitThread = &pMainWnd->m_bExitThread;
    if( !pstrAddress)
    {
    ::MessageBox(NULL,_T("addr出错"),NULL,0);
    return 0;
    }
    if(!piTimeOut)
    {
    ::MessageBox(NULL,_T("out出错"),NULL,0);
    return 0;
    }
    if(!piTryTimes)
    {
    ::MessageBox(NULL,_T("times出错"),NULL,0);
    return 0;
    }
    if(!piBagSize)
    {
    ::MessageBox(NULL,_T("size出错"),NULL,0);
    return 0;
    }
    if(!pstrReply)
    {
    ::MessageBox(NULL,_T("reply出错"),NULL,0);return 0;
    }

    if(!pstrCome)
    {
    ::MessageBox(NULL,_T("come出错"),NULL,0);return 0;
    }
    if(!pstrStatistics)
    {
    ::MessageBox(NULL,_T("sta出错"),NULL,0);return 0;
    }
    if(!pstrPing)
    {
    ::MessageBox(NULL,_T("Ping出错"),NULL,0);return 0;
    }
    if(!pbExitThread)
    {
    ::MessageBox(NULL,_T("thread出错"),NULL,0);return 0;
    }
    if(!pMainWnd)
    {
    ::MessageBox(NULL,_T("main出错"),NULL,0);return 0; }
    // TODO: 在此添加控件通知处理程序代码
    HMODULE hModule = ::LoadLibrary(_T("Iphlpapi.dll"));
    _IcmpCreateFile IcmpCreateFile;
    _IcmpCloseHandle IcmpCloseHandle;
    _IcmpSendEcho IcmpSendEcho;
    IcmpCreateFile =EXPORT_DLL_FUNC(hModule,_,IcmpCreateFile); /*(_IcmpCreateFile)GetProcAddress(hModule, "IcmpCreateFile");*/
    if(!IcmpCreateFile)
    {
    //cout<<"creates"<<GetLastError()<<endl;
    FreeLibrary(hModule) ;
    hModule = NULL;
    exit(0);
    } IcmpSendEcho = EXPORT_DLL_FUNC(hModule,_,IcmpSendEcho);
    if(!IcmpSendEcho)
    {
    //cout<<"send"<<GetLastError()<<endl;
    FreeLibrary(hModule) ;
    hModule = NULL;
    exit(0); } IcmpCloseHandle = EXPORT_DLL_FUNC(hModule,_,IcmpCloseHandle);
    if(!IcmpCloseHandle)
    {
    //cout<<"close"<<GetLastError()<<endl;
    FreeLibrary(hModule) ;
    hModule = NULL;
    exit(0); }
    //MessageBox(_T("a"));
    HANDLE hIcmpFile = IcmpCreateFile();
    int iRecvd = 0;
    int iLost  = 0;
    in_addr inaddr;
    char charAddress[1024]={0};
        //8.56
    /*CString strAddress;


    pediAddress->GetWindowTextW(strAddress);*/
    //strAddress

    if(pstrAddress->IsEmpty())
    {
    ::MessageBox(NULL,_T("目标地址不能空"),_T("地址错误"),0);
    return false;
    }
    wcstombs(charAddress,pstrAddress->GetBuffer(),wcslen(pstrAddress->AllocSysString()));//"58.63.236.238"Address.c_str() IPAddr ulTarAddr = inet_addr(charAddress); if(ulTarAddr == INADDR_NONE)
    {
    WSADATA wsa;
    if(WSAStartup(MAKEWORD(2,2),&wsa))
    return 0;
    int index = pstrAddress->GetAt(pstrAddress->GetLength() - 1); if(index > '0' && index < '9')
    {
    ::MessageBox(NULL,_T("顶级域名不能有数字"),_T("域名错误"),0);
    return 0;
    }
    /// ///
    hostent *hostname;
    wcstombs(charAddress,pstrAddress->GetBuffer(),wcslen(pstrAddress->GetBuffer()));
    hostname =gethostbyname(charAddress);//(char*)strAddress.GetBuffer() 这样直接用在gethostbyname出错
    if(!hostname)
    return 0; //WSADATA wsaData;
    //struct in_addr addr;
    ////int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
    //if (NULL ==  gethostbyname(charAddress))
    //{
    // ::MessageBox(NULL,_T("a顶级域名不能有数字"),_T("域名错误"),0);
    // //m_OutPut += L"解析域名失败!";
    // //SetDlgItemText(IDC_OUTPUT, m_OutPut);
    // return false;
    //}
    //struct hostent *remoteHost = gethostbyname(charAddress);
    //addr.s_addr = *(u_long*)remoteHost->h_addr_list[0]; //////////////////////////////////////////////////////////////////////////// //char *p_addr;
    //p_addr = inet_ntoa(addr);
    //int i = sizeof(p_addr);
    //struct hostent *remoteHost1 = gethostbyaddr(p_addr,i+1,AF_INET);
    //if (remoteHost1 == NULL)
    //{
    // ::MessageBox(NULL,_T("b顶级域名不能有数字"),_T("域名错误"),0);
    // //m_OutPut += L"解析域名失败!";
    // //SetDlgItemText(IDC_OUTPUT, m_OutPut);
    // return false;
    //} ulTarAddr = *(u_long*)hostname->h_addr_list[0];
    WSACleanup();
    }
    char *szRequest;
    //从对话框关联的变量中获取数据
    szRequest = new char[*piBagSize];
    if(!szRequest)
    return 0; memset(szRequest,0,*piBagSize); //* 分配8个数组元素的空间
    DWORD dwReplySize = 8 * (sizeof(ICMP_ECHO_REPLY) + *piBagSize) + 8;
    void* pReplyBuffer = (VOID*)malloc(dwReplySize);
    if (!pReplyBuffer) return 0; int iTimes = 0;
    bool bforver =false;
    bool bPingOnce = false;
    if(0 == *piTryTimes)
    bforver = true;
    while(( iTimes<*piTryTimes ||bforver ) && !(*pbExitThread))
    { int iRetVal = IcmpSendEcho(hIcmpFile, ulTarAddr, szRequest, *piBagSize, NULL, 
    pReplyBuffer, dwReplySize, *piTimeOut); PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)pReplyBuffer; //检查崩溃

    //* 输出pEchoReply指向的第一个元素的内容 IP_OPTION_INFORMATION IpInformation;
    IpInformation = pEchoReply->Options;
    inaddr.S_un.S_addr= ulTarAddr;
    char *p = inet_ntoa(inaddr); if(!bPingOnce)
    {
    char Pingsentence[1024];
    strcpy(Pingsentence,PACKET_PING);
    strcat(Pingsentence,p);
    strcat(Pingsentence,PACKET_PINGS);
    {
    WChartoMChar wtom;
    wchar_t *pwText = wtom.__MultiByteToWideChar(Pingsentence);
    if(!pwText)
    return 0;
    pstrPing->SetString(pwText);
    }
    //memset(sentence,0,sizeof(sentence)); //发送正在ping xxxx
    ::SendMessage(pMainWnd->m_hWnd,WM_PING,0,0);
    //::PostMessageA(pMainWnd->m_hWnd,WM_PING,0,0);
    bPingOnce =true; } if(iRetVal)
    {
    char sentence[1024]={0};
    char ttl[10];
    char timeLength[10] ;
    char BypeSize[10]; _itoa(pEchoReply->DataSize,BypeSize,10); _itoa(pEchoReply->RoundTripTime,timeLength,10); _itoa((unsigned)IpInformation.Ttl,ttl,10); strcpy(sentence,PACKET_FROM);
    strcat(sentence,p);
    strcat(sentence,PACKET_REPLY);
    strcat(sentence,PACKET_BAGSIZE);
    strcat(sentence,BypeSize);
    strcat(sentence,PACKET_RUNTIME);
    strcat(sentence,timeLength);
    strcat(sentence,PACKET_TTL);
    strcat(sentence,ttl);
    {
    WChartoMChar wtom;
    wchar_t *pwText = wtom.__MultiByteToWideChar(sentence);
    if(!pwText)
    return 0;
    pstrReply->SetString(pwText);
    } //发送消息
    ::SendMessage(pMainWnd->m_hWnd,WM_PING,0,0);
    //::PostMessageA(pMainWnd->m_hWnd,WM_PING,0,0);
    iRecvd++;
    }
    else
    { //wchar_t timeout[]={};
    pstrReply->SetString(PACKET_TIMEOUT);
    ::SendMessage(pMainWnd->m_hWnd,WM_PING,0,0);
    //::PostMessageA(pMainWnd->m_hWnd,WM_PING,0,0);
    iLost++;
    }
    iTimes++;
    memset(pReplyBuffer,0,sizeof(pReplyBuffer)); //设置下延迟不然往列表视图加信息时太快
    Sleep(1);
    }
    //* 关闭句柄
    IcmpCloseHandle(hIcmpFile);
    hIcmpFile = NULL;
    free(pReplyBuffer); //重新设置线程控制变量
    *pbExitThread = false; char statistics[1024]={0};
    char information[100]={0};
    char *address = inet_ntoa(inaddr);
    char *infor = " 的 Ping 统计信息";
    char times[10];
    char recvd[10];
    char lost[10];
    char bracket[10]={" ("};
    char percent[10];
    int ipercent = (double)iLost/iTimes*100; _itoa(ipercent,percent,10);
    _itoa(iTimes,times,10);
    _itoa(iRecvd,recvd,10);
    _itoa(iLost,lost,10);
    strcpy(statistics,PACKET_SENT);
    strcat(statistics,times);
    strcat(statistics,PACKET_RECV);
    strcat(statistics,recvd);
    strcat(statistics,PACKET_LOST);
    strcat(statistics,lost);
    strcat(statistics,bracket);
    strcat(statistics,percent);
    strcat(statistics,PACKET_LOSTPERCENT);
    strcpy(information,address);
    strcat(information,infor);
    {
    WChartoMChar wtom;
    wchar_t *pwText = wtom.__MultiByteToWideChar(information);
    if(!pwText)
    return 0;
    pstrCome->SetString(pwText);
    } ::SendMessage(pMainWnd->m_hWnd,WM_PING,0,0);
    //::PostMessageA(pMainWnd->m_hWnd,WM_PING,0,0); {
    WChartoMChar wtom;
    wchar_t *pwText = wtom.__MultiByteToWideChar(statistics);
    if(!pwText)
    return 0;
    pstrStatistics->SetString(pwText);
    }
    //给主窗口发送消息;
    ::SendMessage(pMainWnd->m_hWnd,WM_PING,0,0);
    //::PostMessageA(pMainWnd->m_hWnd,WM_PING,0,0);
    return 0;
    }问题是:在线程函数里老是出现变量指针为null
    另一问题就是:在onpingbutton中我加入了 sleep函数。如果出现注释了这个函数。出现null的情况比较大,不出现的话问题比较少。我的代码工程放了在我的下载资源。希望大虾们帮我看看。困扰好久了。。谢谢。。分不多。
      

  2.   

    你的代码中Struct_information为局部变量,在函数执行完后就释放了,而你线程中的其他代码还在执行,可以new一个指针给线程使用,线程退出前delete该指针,或将该变量放到全局中去