[SOLVED] Windows: netsh and QProcess
-
wrote on 8 Sept 2014, 07:10 last edited by
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
-
wrote on 8 Sept 2014, 09:53 last edited by
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?
-
wrote on 8 Sept 2014, 18:08 last edited by
netsh is returning 0 because it changed the IP successfully.
The conflict notification occurs later.
-
wrote on 9 Sept 2014, 05:46 last edited by
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 ?
-
wrote on 9 Sept 2014, 08:10 last edited by
Yes that is coming from the OS
-
wrote on 9 Sept 2014, 16:18 last edited by
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.
-
wrote on 9 Sept 2014, 17:49 last edited by
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.
-
wrote on 11 Sept 2014, 16:49 last edited by
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, .
-
wrote on 12 Sept 2014, 05:57 last edited by
@ 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. -
wrote on 12 Sept 2014, 08:09 last edited by
Thanks from every one who help to solve it :)
1/13