Using QT5.7.0, after executing a Powershell command New-NetIPAddress, waitForFinished () and then using icmpSendEcho2 returning 0, I get a return value of 0 when calling GetLastError 0, what can cause this behaviour?
I am using a 64bit Windows 10 OS where I execute the .exe and the address i am trying to ping is reachable. And when i change the IP address of an interface I get at "random" the mentioned error and from time to time I get an answer from the next router - destination unreachable or ping successful when i hit the right interface.
From the documentation MSDN IcmpSendEcho2 i read that after returning 0, I should check GetLastError. And from the documentation Error Codes 0 - 499 a return value of 0 means no Error.
In a few threads there was mentioned that another windows process can execute in the meantime. Could this be the case that the GetLastError returns 0?
This Problem occurs when i change the IP and ping afterwards. When the IP is already set, I ping without changing the IP Address and there the behaviour is like i think it should be.
The code executes in this order, simplified:
if (ipIsSet()==false){ //check if the IP address is already configured
changeIP(interface,IPAddress,maskPrefix);
ping();
} else {
ping();
}
where the changeIP Function contains the following:
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
and the ping() function contains this:
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;
}
}