使用QT5.7.0,执行Powershell命令New-NetIPAddress,waitForFinished()然后使用icmpSendEcho2返回0,我在调用GetLastError 0时得到返回值0,会导致这种行为吗?
我正在使用64位Windows 10操作系统执行.exe,我尝试ping的地址是可以访问的。当我更改接口的IP地址时,我会“随机”获得上述错误,并且我不时会从下一个路由器得到答案 - 当我点击正确的界面时,目标无法访问或ping成功。
从文档MSDN IcmpSendEcho2我读到,返回0后,我应该检查GetLastError。从文档Error Codes 0 - 499返回值0表示没有错误。
在一些线程中提到了另一个窗口进程可以在此期间执行。这可能是GetLastError返回0的情况吗?
当我更改IP并在之后ping时,会出现此问题。当IP已经设置时,我ping而不改变IP地址,行为就像我认为的那样。
代码按此顺序执行,简化:
if (ipIsSet()==false){ //check if the IP address is already configured
changeIP(interface,IPAddress,maskPrefix);
ping();
} else {
ping();
}
其中changeIP函数包含以下内容:
int ipconfig::changeIP(QString interfaceNew, QString newIPAddress, QString newSubnet){
QProcess IPProcess;
QStringList commands;
commands.clear();
commands <<"New-NetIPAddress"<<"-InterfaceAlias"<<"\""+interfaceNew+"\""<<"-IPAddress"<<"\""+newIPAddress+"\""<<"-PrefixLength"<<newSubnet;
IPProcess.start("powershell",commands);
IPProcess.waitForFinished();
QString pStdout = IPProcess.readAllStandardOutput();
QString pStderr = IPProcess.readAllStandardError();
if (pStderr.size() > 1){ //Error
emit status("Fehler bei Adapter "+interfaceNew+".\ Folgender Fehler ist aufgetreten: \n"+pStderr+"\n");
return 0;
} else {
emit status("Adapter "+interfaceNew+" wurde mit IP konfiguriert.\n");
return 1;
}
return 0;
} //end change IP
并且ping()函数包含:
int ipconfig::ping(){
dwRetVal = 0, dwError = 0, replySize = 0;
emit status("Ping wird initialisiert.\n");
hIcmpFile = IcmpCreateFile();
if(hIcmpFile==INVALID_HANDLE_VALUE){
emit status("Windows Handle konnte nicht erstellt werden.\n");
emit pingFinished();
return 0;
}
replySize = sizeof(ICMP_ECHO_REPLY)+sizeof(SendData)+8;
ReplyBuffer = (VOID*)malloc(replySize);
if(ReplyBuffer==NULL){
emit status ("Buffer Speicher konnte nicht belegt werden.\n");
emit pingFinished();
return 9;
}
QByteArray array = pingAddress.toLocal8Bit();
const char* buffer = array.data();
ulIPAddr = inet_addr(buffer);
qDebug()<<ulIPAddr;
dwRetVal = IcmpSendEcho2(hIcmpFile, NULL,NULL, NULL, ulIPAddr, SendData, sizeof(SendData),
NULL, ReplyBuffer, replySize, 1000);
emit status ("dwRetVal = "+QString::number(dwRetVal));
if (dwRetVal != 0){
PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
struct in_addr ReplyAddr;
ReplyAddr.S_un.S_addr = pEchoReply->Address;
emit status ("Sende Ping zu "+pingAddress);
if (dwRetVal>1){
qDebug()<<"Antwort: "<<dwRetVal;
} else {
qDebug()<<"Antwort: "<<dwRetVal;
}
QString str = inet_ntoa(ReplyAddr);
emit status ("Antwort von: " + str);
if (ReplyAddr.S_un.S_addr == ulIPAddr){
emit status("Ping erfolgreich.\n");
emit pingFinished();
return 1;
}
emit status ("Antwort von falscher Adresse.\n");
emit pingFinished();
return 404;
}
else {
qDebug()<<"ICMP fehlgeschlagen";
dwError=GetLastError();
settingsLogging(dwError);
switch (dwError) {
case IP_BUF_TOO_SMALL:
qDebug()<<"ReplyBuffer zu klein";
emit status ("Antwort Buffer zu klein.\n");
emit pingFinished();
return 8;
break;
case IP_REQ_TIMED_OUT:
qDebug()<<"Timeout";
emit status ("Timout bei Ping.\n");
emit pingFinished();
return 7;
break;
default:
emit status ("Unbekannter Fehler bei Ping: "+QString::number(dwError));
emit pingFinished();
return 10;
break;
}
emit status ("Extended Error.\n");
//emit IPSetStatus(false);
emit pingFinished();
return 100;
}
}