CString str;
char startip[100],endip[100];
startip.GetWindowTextW(str);
if (!WideCharToMultiByte(CP_ACP,0,LPCTSTR(str),-1,startip,100,NULL,NULL)) { return ; }
endip.GetWindowTextW(str);
if (!WideCharToMultiByte(CP_ACP,0,LPCTSTR(str),-1,endip,100,NULL,NULL)) { return ; }
memset(&dest,0,sizeof(dest)); //初始化dest结构
//以上的有问题 注释掉可以完全通过,但运行时会弹出窗口出错
如下
E:\MFC\scanhost\scanhostDlg.cpp(252) : error C2228: left of '.GetWindowTextW' must have class/struct/union type
E:\MFC\scanhost\scanhostDlg.cpp(253) : error C2664: 'WideCharToMultiByte' : cannot convert parameter 3 from 'const char *' to 'const unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\MFC\scanhost\scanhostDlg.cpp(254) : error C2228: left of '.GetWindowTextW' must have class/struct/union type
E:\MFC\scanhost\scanhostDlg.cpp(255) : error C2664: 'WideCharToMultiByte' : cannot convert parameter 3 from 'const char *' to 'const unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.后面还有一部分也贴出来吧帮助理解
//以下没错误
unsigned long startIP,endIP;
dest.sin_family=AF_INET;
dest.sin_addr.s_addr=inet_addr(startip); //填入开始搜索IP
startIP=inet_addr(startip);
end.sin_family=AF_INET;
end.sin_addr.s_addr=inet_addr(endip);
endIP=inet_addr(endip); //填入结束搜索IP地址
HANDLE hThread;
while(htonl(startIP)<=htonl(endIP)) //起始IP比结束IP小
{
if(ThreadNumCounter>ThreadNumLimit) //判断线程数目,如果太多,休眠
{
Sleep(5000);
continue;
}
DWORD ThreadID;
sockaddr_in * pIPAddrTemp=new(sockaddr_in);
if(!pIPAddrTemp)
{
display.ReplaceSel(_T("memory alloc failed"));
return;
}
*pIPAddrTemp=dest;
clock_t start;
start=clock();
hThread=CreateThread(NULL,NULL,FINDIP,(LPVOID)pIPAddrTemp,NULL,&ThreadID);
long i=60000000L;
while(i--);
TerminateThread(hThread,0);
InterlockedDecrement(aa);
memset(&from,0,sizeof(from));
startIP=htonl(htonl(startIP)+1);
dest.sin_addr.s_addr=startIP;
}
for(int i=0;i<open;i++){
display.ReplaceSel(_T("活动主机:")+open_ip[i]+_T("\r\n"));
} display.ReplaceSel(_T("扫描结束。"));
while(ThreadNumCounter!=0)
{
Sleep(2000);
return;
}
// TODO: 在此添加控件通知处理程序代码
}DWORD WINAPI CScanhostDlg::FINDIP(LPVOID pIPAddrTemp)
{
InterlockedIncrement(aa);//线程数目+1
char icmp_data[MAX_PACKET];
memset(icmp_data,0,MAX_PACKET);//数据报初始化
int datasize=DEF_PACKET_SIZE;//数据报报文的缺省长度
datasize+=sizeof(IcmpHeader);//加上icmp头部长度
fill_icmp_data(icmp_data,datasize);//填充包
((IcmpHeader*)icmp_data)->checksum=0;//效验和置零
((IcmpHeader*)icmp_data)->seq=0;//序列号置零
((IcmpHeader*)icmp_data)->checksum=checksum((USHORT*)icmp_data,datasize);
//计算效验和后填人
int bwrote=sendto(sockRaw,icmp_data,datasize,0,(struct sockaddr*)pIPAddrTemp,sizeof(dest));
//发送数据报
int n=0;
if(bwrote==SOCKET_ERROR)
{
if(WSAGetLastError()==WSAETIMEDOUT)
{
display.ReplaceSel(_T("timed out"));
}
display.ReplaceSel(_T("sendto failed:"));
ExitProcess(STATUS_FAILED);
n=1;
}
if (WSAGetLastError()==WSAETIMEDOUT)
{
display.ReplaceSel(_T("timed out"));
ExitProcess(STATUS_FAILED);
n=1;
}
if(bwrote<datasize)
{
display.ReplaceSel(_T("Error!"));
ExitProcess(STATUS_FAILED);
n=1;
}
int bread=recvfrom(sockRaw,recvbuf,MAX_PING_PACKET_SIZE,0,(struct sockaddr*)&from,&fromlen);
//数据包的接收
if(bread==SOCKET_ERROR)
{
if (WSAGetLastError()==WSAETIMEDOUT)
{
display.ReplaceSel(_T("timed out"));
}
display.ReplaceSel(_T("Error!"));
ExitProcess(STATUS_FAILED);
n=1;
}
if(n==0)
decode_resp(recvbuf,bread,&from);
InterlockedDecrement(aa);
return 0;
}
USHORT CScanhostDlg::checksum(USHORT* buffer, int size)
{
unsigned long cksum=0;
while(size>1)
{
cksum+=*buffer++;
size-=sizeof(USHORT);
}
if(size)
{
cksum+=*(UCHAR*)buffer;
}
cksum=(cksum>>16)+(cksum & 0xffff);
cksum+=(cksum>>16);
return (USHORT)(~cksum);
}void CScanhostDlg::decode_resp(char* buf, int bytes, struct sockaddr_in* from)
{
IpHeader *iphdr;
IcmpHeader *icmphdr;
unsigned short iphdrlen;
iphdr=(IpHeader *)buf;
iphdrlen=iphdr->headlen*4;
icmphdr=(IcmpHeader *)(buf+iphdrlen); if(bytes<iphdrlen+ICMP_MIN)return;
if(icmphdr->type!=ICMP_ECHO_REPLY)return;
if(icmphdr->id!=(USHORT)GetCurrentThreadId())return;
CString *str=new CString(inet_ntoa(from->sin_addr));
open_ip[open]=*str;
open++;
}
void CScanhostDlg::fill_icmp_data(char *icmp_data,int datasize)
{
IcmpHeader *icmp_hdr;
char *datapart;
icmp_hdr=(IcmpHeader*)icmp_data;
icmp_hdr->type=ICMP_ECHO;//设置类型信息
icmp_hdr->id=(USHORT)GetCurrentThreadId();//设置其ID号为当前线程ID号
datapart=icmp_data+sizeof(IcmpHeader);//计算ICMP数据报的数据部分
memset(datapart,'A',datasize-sizeof(IcmpHeader));//填入数据
char startip[100],endip[100];
startip.GetWindowTextW(str);
if (!WideCharToMultiByte(CP_ACP,0,LPCTSTR(str),-1,startip,100,NULL,NULL)) { return ; }
endip.GetWindowTextW(str);
if (!WideCharToMultiByte(CP_ACP,0,LPCTSTR(str),-1,endip,100,NULL,NULL)) { return ; }
memset(&dest,0,sizeof(dest)); //初始化dest结构
//以上的有问题 注释掉可以完全通过,但运行时会弹出窗口出错
如下
E:\MFC\scanhost\scanhostDlg.cpp(252) : error C2228: left of '.GetWindowTextW' must have class/struct/union type
E:\MFC\scanhost\scanhostDlg.cpp(253) : error C2664: 'WideCharToMultiByte' : cannot convert parameter 3 from 'const char *' to 'const unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\MFC\scanhost\scanhostDlg.cpp(254) : error C2228: left of '.GetWindowTextW' must have class/struct/union type
E:\MFC\scanhost\scanhostDlg.cpp(255) : error C2664: 'WideCharToMultiByte' : cannot convert parameter 3 from 'const char *' to 'const unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.后面还有一部分也贴出来吧帮助理解
//以下没错误
unsigned long startIP,endIP;
dest.sin_family=AF_INET;
dest.sin_addr.s_addr=inet_addr(startip); //填入开始搜索IP
startIP=inet_addr(startip);
end.sin_family=AF_INET;
end.sin_addr.s_addr=inet_addr(endip);
endIP=inet_addr(endip); //填入结束搜索IP地址
HANDLE hThread;
while(htonl(startIP)<=htonl(endIP)) //起始IP比结束IP小
{
if(ThreadNumCounter>ThreadNumLimit) //判断线程数目,如果太多,休眠
{
Sleep(5000);
continue;
}
DWORD ThreadID;
sockaddr_in * pIPAddrTemp=new(sockaddr_in);
if(!pIPAddrTemp)
{
display.ReplaceSel(_T("memory alloc failed"));
return;
}
*pIPAddrTemp=dest;
clock_t start;
start=clock();
hThread=CreateThread(NULL,NULL,FINDIP,(LPVOID)pIPAddrTemp,NULL,&ThreadID);
long i=60000000L;
while(i--);
TerminateThread(hThread,0);
InterlockedDecrement(aa);
memset(&from,0,sizeof(from));
startIP=htonl(htonl(startIP)+1);
dest.sin_addr.s_addr=startIP;
}
for(int i=0;i<open;i++){
display.ReplaceSel(_T("活动主机:")+open_ip[i]+_T("\r\n"));
} display.ReplaceSel(_T("扫描结束。"));
while(ThreadNumCounter!=0)
{
Sleep(2000);
return;
}
// TODO: 在此添加控件通知处理程序代码
}DWORD WINAPI CScanhostDlg::FINDIP(LPVOID pIPAddrTemp)
{
InterlockedIncrement(aa);//线程数目+1
char icmp_data[MAX_PACKET];
memset(icmp_data,0,MAX_PACKET);//数据报初始化
int datasize=DEF_PACKET_SIZE;//数据报报文的缺省长度
datasize+=sizeof(IcmpHeader);//加上icmp头部长度
fill_icmp_data(icmp_data,datasize);//填充包
((IcmpHeader*)icmp_data)->checksum=0;//效验和置零
((IcmpHeader*)icmp_data)->seq=0;//序列号置零
((IcmpHeader*)icmp_data)->checksum=checksum((USHORT*)icmp_data,datasize);
//计算效验和后填人
int bwrote=sendto(sockRaw,icmp_data,datasize,0,(struct sockaddr*)pIPAddrTemp,sizeof(dest));
//发送数据报
int n=0;
if(bwrote==SOCKET_ERROR)
{
if(WSAGetLastError()==WSAETIMEDOUT)
{
display.ReplaceSel(_T("timed out"));
}
display.ReplaceSel(_T("sendto failed:"));
ExitProcess(STATUS_FAILED);
n=1;
}
if (WSAGetLastError()==WSAETIMEDOUT)
{
display.ReplaceSel(_T("timed out"));
ExitProcess(STATUS_FAILED);
n=1;
}
if(bwrote<datasize)
{
display.ReplaceSel(_T("Error!"));
ExitProcess(STATUS_FAILED);
n=1;
}
int bread=recvfrom(sockRaw,recvbuf,MAX_PING_PACKET_SIZE,0,(struct sockaddr*)&from,&fromlen);
//数据包的接收
if(bread==SOCKET_ERROR)
{
if (WSAGetLastError()==WSAETIMEDOUT)
{
display.ReplaceSel(_T("timed out"));
}
display.ReplaceSel(_T("Error!"));
ExitProcess(STATUS_FAILED);
n=1;
}
if(n==0)
decode_resp(recvbuf,bread,&from);
InterlockedDecrement(aa);
return 0;
}
USHORT CScanhostDlg::checksum(USHORT* buffer, int size)
{
unsigned long cksum=0;
while(size>1)
{
cksum+=*buffer++;
size-=sizeof(USHORT);
}
if(size)
{
cksum+=*(UCHAR*)buffer;
}
cksum=(cksum>>16)+(cksum & 0xffff);
cksum+=(cksum>>16);
return (USHORT)(~cksum);
}void CScanhostDlg::decode_resp(char* buf, int bytes, struct sockaddr_in* from)
{
IpHeader *iphdr;
IcmpHeader *icmphdr;
unsigned short iphdrlen;
iphdr=(IpHeader *)buf;
iphdrlen=iphdr->headlen*4;
icmphdr=(IcmpHeader *)(buf+iphdrlen); if(bytes<iphdrlen+ICMP_MIN)return;
if(icmphdr->type!=ICMP_ECHO_REPLY)return;
if(icmphdr->id!=(USHORT)GetCurrentThreadId())return;
CString *str=new CString(inet_ntoa(from->sin_addr));
open_ip[open]=*str;
open++;
}
void CScanhostDlg::fill_icmp_data(char *icmp_data,int datasize)
{
IcmpHeader *icmp_hdr;
char *datapart;
icmp_hdr=(IcmpHeader*)icmp_data;
icmp_hdr->type=ICMP_ECHO;//设置类型信息
icmp_hdr->id=(USHORT)GetCurrentThreadId();//设置其ID号为当前线程ID号
datapart=icmp_data+sizeof(IcmpHeader);//计算ICMP数据报的数据部分
memset(datapart,'A',datasize-sizeof(IcmpHeader));//填入数据
解决方案 »
- 请教COleControl类中OnDraw和PreTranslateMessage函数调用先后关系
- 请问如何刷新list control控件中的内容,例如新加入数据库中一行,要在列表中显示出来
- 急在线等!!!
- CView类指针问题
- 急急急!!!哪儿寻找MFC042D.dll
- 这里的高手死绝了吗???这个问题都不会??????高分也不要??????
- 如何生成序列号?
- 哪儿有softICE for win2000下载?给出好用的下载链结站点者给分!!!!!!!!!
- 调查:不知大家对算法的熟悉程序怎样,请默写快速排序算法,试试有多少人能写出
- 如何在OnInitDialog()中控制自身的隐藏属性?
- vc++中dbmssocn是什么意思?
- VC延时
startip.GetWindowTextW(str);
---------------
这肯定就错了,你的startip是char[],GetWindowTextW应该是CWnd类型的成员函数WideCharToMultiByte
这个是将宽字符转成多字节,比如将wchar_t[]转成char[]-----------------
上面的都是编译错误,应该很容易解决的