[SOLVED] Windows: netsh and QProcess
-
In my application I need to be able to change the IP address, for which I am using QProcess. The application is running with Admin privileges and I am using the following code
@
QProcess process;
QString cmd;
cmd = QString("netsh interface ip set address name="%1" static %2 %3 %4 1").arg(m_adapter).arg(m_ip).arg(m_netmask).arg(m_gateway);process.start(cmd);
process.waitForFinished();
@
As such this is working fine and I am able to change the IP address.
The problem is if I have an IP conflict. Currently I just get a pop-up window telling me about the IP conflict, but I don't know how to get any return code/message from the netsh command. I can have the situation where the IP is changed from a remote PC in which case the pop-up window is of no use.
Can anyone tell me if it is possible to get some status output from the netsh command such that I can tell if a problem occured? Or is there another option to the QProcess which is better? -
Hi,
You can use e.g. QProcess::exitCode() which will give you the exit status of netsh
Hope it helps
-
Thanks a lot for the reply, SGaist.
I have tried to use exitCode() as shown below, but this just gives me zero regardless if I have an IP conflict or not.@
QProcess process;
QString cmd;
cmd = QString("netsh interface ip set address name="%1" static %2 %3 %4 1").arg(m_adapter).arg(m_ip).arg(m_netmask).arg(m_gateway);process.start(cmd);
process.waitForFinished();
qDebug() << process.exitCode();
@I have tried to run the netsh command manually in a command prompt. And it does not give any output either if I trigger an IP conflict (other than the pop-up window). Any ideas how to extract this information from netsh?
-
netsh is returning 0 because it changed the IP successfully.
The conflict notification occurs later.
-
Ahh.. Good to know :) Do you know if it is possible to get the notification of the conflict other than a pop-up window?
-
Isn't that popup coming from your OS ?
-
Yes that is coming from the OS
-
I don't know if it's possible.
The popup is displayed when a network packet arrives at the NIC. This can happen immediately or take minutes, depending on network traffic.
-
Hi, helthans
i think that below code can be useful for you... change for yourself and try it:
@
QProcess pingProcess;
QString exec = "ping";
QStringList params;
params << "-c" << "1" << IP;
pingProcess.start(exec,params,QIODevice::ReadOnly);
pingProcess.waitForFinished(-1);
QString p_stdout = pingProcess.readAllStandardOutput();
QString p_stderr = pingProcess.readAllStandardError();
@ -
@ a.jafarabadi QProcess is not the problem here, the impact of the call to netsh is completely outside of QProcess.
-
The code from a.jafarabadi can be usefull: Test the IP (with ping) before invoke the netsh. Maybe you need to decode the results.
If your code will run on Windows only, you can call IcmpSendEcho:http://msdn.microsoft.com/en-us/library/windows/desktop/aa366050(v=vs.85).aspx instead invoke PING.EXE, .
-
@ a.jafarabadi and TiRoy: Yes I agree, testing the IP before netsh is the way to go.
Thanks a lot for the solutions suggested. For the network issue my code is split up in a Windows and Linux part (no way around this :)). For Windows I will look into lcmpSendEcho, but for Linux I need to use the ping command - I was hoping I could use another metric, instead of having to do some pattern matching of the output of the ping command, but I guess there is no way around this either. -
Thanks from every one who help to solve it :)