Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Windows: netsh and QProcess
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Windows: netsh and QProcess

Scheduled Pinned Locked Moved General and Desktop
13 Posts 4 Posters 5.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    helthans
    wrote on 8 Sept 2014, 07:10 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 8 Sept 2014, 07:32 last edited by
      #2

      Hi,

      You can use e.g. QProcess::exitCode() which will give you the exit status of netsh

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • H Offline
        H Offline
        helthans
        wrote on 8 Sept 2014, 09:53 last edited by
        #3

        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?

        1 Reply Last reply
        0
        • T Offline
          T Offline
          TioRoy
          wrote on 8 Sept 2014, 18:08 last edited by
          #4

          netsh is returning 0 because it changed the IP successfully.

          The conflict notification occurs later.

          1 Reply Last reply
          0
          • H Offline
            H Offline
            helthans
            wrote on 9 Sept 2014, 05:46 last edited by
            #5

            Ahh.. Good to know :) Do you know if it is possible to get the notification of the conflict other than a pop-up window?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 9 Sept 2014, 07:57 last edited by
              #6

              Isn't that popup coming from your OS ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • H Offline
                H Offline
                helthans
                wrote on 9 Sept 2014, 08:10 last edited by
                #7

                Yes that is coming from the OS

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  TioRoy
                  wrote on 9 Sept 2014, 16:18 last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jafarabadi.qt
                    wrote on 9 Sept 2014, 17:49 last edited by
                    #9

                    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();
                    @

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 9 Sept 2014, 20:00 last edited by
                      #10

                      @ a.jafarabadi QProcess is not the problem here, the impact of the call to netsh is completely outside of QProcess.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • T Offline
                        T Offline
                        TioRoy
                        wrote on 11 Sept 2014, 16:49 last edited by
                        #11

                        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, .

                        1 Reply Last reply
                        0
                        • H Offline
                          H Offline
                          helthans
                          wrote on 12 Sept 2014, 05:57 last edited by
                          #12

                          @ 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.

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            jafarabadi.qt
                            wrote on 12 Sept 2014, 08:09 last edited by
                            #13

                            Thanks from every one who help to solve it :)

                            1 Reply Last reply
                            0

                            1/13

                            8 Sept 2014, 07:10

                            • Login

                            • Login or register to search.
                            1 out of 13
                            • First post
                              1/13
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved