void CMyscanDlg::OnButton1Start()
{
// TODO: Add your control notification handler code here
CString btnTxt,IP;
UINT nSinglePort;
BYTE f1,f2,f3,f4;
TCHAR temp[10]="\0";
m_cProgress.SetPos(0);
m_cResult.DeleteAllItems();//清空所有条目
POSITION p=m_pStatusList->GetHeadPosition();
while(p)
{
POSITION temp=p;
DATA* pNode=(DATA*)m_pStatusList->GetNext(p);
m_pStatusList->RemoveAt(temp);
if (pNode)//pNode!=null
delete pNode;
}
//判断IP是否为空
if(m_cIP.IsBlank())
{
MessageBox("请输入主机IP地址");
return;
}
if(m_cIP.GetAddress(f1,f2,f3,f4)<4)//判断输入IP是否有误
{
MessageBox("请输入有效的IP地址");
return;
}
IP=_itoa(f1,temp,10);
IP+=_T('.');
IP+=_itoa(f2,temp,10);
IP+=_T('.');
IP+=_itoa(f3,temp,10);
IP+=_T('.');
IP+=_itoa(f4,temp,10);
m_cBtnStop.EnableWindow();//停止按键默认允许
m_cBtnScan.EnableWindow(false);//扫描按键被禁止
if(m_bSinglePort)
{
CString port;
m_cSinglePort.GetWindowText(port);
//最大端口等于最小端口视为单个端口扫描
m_minPort=m_maxPort=nSinglePort=atoi(port);
}
else
{
CString port1,port2;
m_cPortFrom.GetWindowText(port1);
m_cPortTo.GetWindowText(port2);
m_minPort=atoi(port1);
m_maxPort=atoi(port2);
//进度条
m_cProgress.SetRange32(0,m_maxPort-m_minPort+1);
m_cProgress.SetStep(1);
}
if (m_maxPort<m_minPort)
{
MessageBox(_T("您输入的端口范围有误,请重新输入"));
return;
}
UINT m_nMaxAttempts=GetDlgItemInt(IDC_EDIT_ATTEMPTS);
for(m_nCounter=m_minPort;m_nCounter<=m_maxPort;m_nCounter++)
{
BOOL bIsOpen=false;
UINT nAttempt=1; //尝试次数
while(nAttempt<=m_nMaxAttempts && !bIsOpen)
{
TCHAR temp[10]="\0";
CString str=_T("尝试连接端口:");
str+=itoa(m_nCounter,temp,10);
str+=_T(", IP地址为=");
str+=IP;
str+=_T(", 尝试次数为=");
str+=itoa(nAttempt,temp,10);
CStatic* pStatic=(CStatic*)GetDlgItem(IDC_STATIC_STATUS);
pStatic->SetWindowText(str);//(也有用这个函数的SetStatusBarText)
str.Empty();
bIsOpen=TestConnection(IP,m_nCounter);
if(bIsOpen)
{
DATA* pNode=new DATA;
ASSERT(pNode);
strcpy(pNode->IPAddress,IP.GetBuffer(IP.GetLength()));
strcpy(pNode->port,_itoa(m_nCounter,temp,10));
pNode->bStatus=1; //打开
pNode->nAttempts=nAttempt;
m_pStatusList->AddTail(pNode);
}
nAttempt++;
}
if (!bIsOpen)
{
DATA* pNode=new DATA;
ASSERT(pNode);
strcpy(pNode->IPAddress,IP.GetBuffer(IP.GetLength()));
strcpy(pNode->port,_itoa(m_nCounter,temp,10));
pNode->bStatus=0; //关闭
pNode->nAttempts=nAttempt-1;
m_pStatusList->AddTail(pNode);
}
MSG message;
if (::PeekMessage(&message,NULL,0,0,PM_REMOVE))
{
::TranslateMessage(&message);
::DispatchMessage(&message);
}
m_cProgress.StepIt();
}
//设定状态栏
CStatic* pStatic=(CStatic*)GetDlgItem(IDC_STATIC_STATUS);
pStatic->SetWindowText((CString)_T("当前扫描状态"));
m_cBtnScan.EnableWindow();
m_cBtnStop.EnableWindow(false);
//填充报表视图
UINT nIndex=0;
POSITION pos=m_pStatusList->GetHeadPosition();
while(pos)
{
DATA* pNode=(DATA*)m_pStatusList->GetNext(pos);
/*/////////////////////////////////////(自己尝试的另一种输出列表,无视无视)
m_cResult.InsertItem(nIndex,NULL);
m_cResult.SetItemText(nIndex,0,pNode->IPAddress);
m_cResult.SetItemText(nIndex,1,pNode->port);*/
AddItem(nIndex,0,pNode->IPAddress,0);
AddItem(nIndex,1,pNode->port,0);
if (pNode->bStatus)
{
//m_cResult.SetItemText(nIndex,2,"开");
//m_cResult.SetItemText(nIndex,4,"get");
AddItem(nIndex,2,_T("开"),0);
AddItem(nIndex,4,_T("⊙▽⊙"),0);
}
else
{
//m_cResult.SetItemText(nIndex,2,"关");
//m_cResult.SetItemText(nIndex,4,"no way");
AddItem(nIndex,2,_T("关"),0);
AddItem(nIndex,4,_T("T T"),0);
}
//m_cResult.SetItemText(nIndex++,3,itoa(pNode->nAttempts,temp,10));
AddItem(nIndex++,3,_itoa(pNode->nAttempts,temp,10),0);
}
}
网上应该很多这个代码,我运行这个代码之后就出现![图片说明](https://img-ask.csdn.net/upload/201501/02/1420206303_65279.jpg)
list_s.cpp文件的171行
POSITION CStringList::AddTail(const CString& newElement)
{
ASSERT_VALID(this);
CNode* pNewNode = NewNode(m_pNodeTail, NULL);
pNewNode->data = newElement;
if (m_pNodeTail != NULL)
m_pNodeTail->pNext = pNewNode;
else
m_pNodeHead = pNewNode;
m_pNodeTail = pNewNode;
return (POSITION) pNewNode;
}
研究了下应该是
//增加一列标题栏字符
void CMyscanDlg::AddHeader(LPTSTR hdr)
{
if(m_pColumns)
m_pColumns->AddTail(hdr);
}
这个函数出问题了,但是不知道怎么改,就把添加标题栏的方式换了一种。
结果窗口出来了,但是开始扫描之后就显示已停止工作_(:3」∠)_
debug检查弹出的窗口显示unhandled exception in *.exe (MFC42D.DLL):0xC0000005:Access Violation
已经不知道怎么改的我,求大神指点