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. QProcess start netsh command with quotes and spaces in parameter argument question.
Forum Updated to NodeBB v4.3 + New Features

QProcess start netsh command with quotes and spaces in parameter argument question.

Scheduled Pinned Locked Moved General and Desktop
windowsqprocessnetsh
3 Posts 2 Posters 2.6k Views 2 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.
  • T Offline
    T Offline
    teungri
    wrote on last edited by teungri
    #1

    Hello,
    I want to set my network interface adapter on windows to static/dhcp with the netsh command via QProcess.
    The command should be:
    netsh interface ipv4 set address "USB-Ethernet Adapter" dhcp
    This command is working when I execute it from the windows command line. However I want to execute this
    command in a Qt application via QProcess.

    I read already various threads about this issue but I never reached a successful result. see
    http://doc.qt.io/qt-5/qprocess.html#start-3
    From this documentation I should use """"USB-Ethernet Adapter"""",
    triple " to expand to ", the 4th one because a space in the parameter.
    Here is my different code I use to set dhcp:

        //-----------------------------------------------------------------------------------------------------------
        void setDhcp()
        {
            QStringList args;
            QProcess proc;
            args.clear();
            args << "interface" << "ipv4" << "set" << "address";
             //    args << "\"" + nwAdapter + "\"";
            args << "\"\"\"\"" + nwAdapter + "\"\"\"\"";
            args << "dhcp";
    
         //    proc.start( "netsh", args );
        //    proc.waitForFinished();
    
        int exitCode = 0;
        //    exitCode = QProcess::execute("netsh", args );
    
        QString cmd = "netsh " + args.join(" ");
        qCDebug(DBG) << "cmd=" << cmd << "exitCode=" << exitCode;
        //    exitCode = QProcess::execute( cmd );
        proc.start( cmd );
        proc.waitForFinished();
    }
    //-----------------------------------------------------------------------------------------------------------
    

    The result of printing cmd is:

    cmd= "netsh interface ipv4 set address """"USB-Ethernet Adapter"""" dhcp" exitCode= 0
    

    Is there anybody who has a clue to this problem?

    currently i stick the the "system(cmd)';" solution which is working, but than annoying
    windows popups occur.

    Thanks in advance for your info.
    Kind regards,

    1 Reply Last reply
    0
    • T Offline
      T Offline
      teungri
      wrote on last edited by teungri
      #2

      Hello,
      Since no one is replying, there is possibly no qt solution. FYI, I solved my problem with old school
      windows api, see code below (the netshName contains spaces):

      cmd = QString("netsh interface ipv4 set address name=\"%1\" static").  arg(this->networkAdapter()->netshName());
      cmd += " 169.254.1.1 255.255.255.0 169.254.1.1 1";
      ExecuteNetsh( cmd.toStdString().c_str() );
      

      static void ExecuteNetsh(const char* strCmd )
      {
          char strPath[1024];
          STARTUPINFOA startinfo;
          PROCESS_INFORMATION process;
          DWORD dwRes;
      
          //fill start-info
          GetStartupInfoA(&startinfo);
          startinfo.cb = sizeof (startinfo);
          startinfo.dwFlags = STARTF_USESHOWWINDOW;
          startinfo.wShowWindow = SW_HIDE;
      
      	//get netsh-path
      	GetSystemDirectoryA(strPath, 1024);
      	strcat( strPath, "\\netsh.exe" );
      
          //start netsh
          if (!CreateProcessA(strPath, (LPSTR)strCmd, NULL, NULL,
                              FALSE, 0, NULL, NULL, &startinfo, &process))
          {
              qCDebug(DBG) << QString("Error while trying to execute %1 %2").arg(strPath).arg(strCmd);
              return;
          }
      
      	//wait until finished
      	WaitForSingleObject(process.hProcess, INFINITE);
      
      	//check exit-code
      	GetExitCodeProcess(process.hProcess, &dwRes);
      	if(dwRes != 0) qCCritical(DBG) << QString( "GetExitCodeProcess error %1\n\tcmd=%2").
                  arg( GetLastError() ).
                  arg( strCmd );
      
      	//close handles
      	CloseHandle(process.hThread);
      	CloseHandle(process.hProcess);
      }
      

      Kind regards,
      Teun Grinwis

      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by Chris Kawa
        #3

        Here's an example that will print info on all available interfaces:

        QString command("netsh interface ipv4 show addresses \"%1\"");
        
        auto infs = QNetworkInterface::allInterfaces();
        for(auto inf : infs)
        {
            QProcess proc;
            proc.start(command.arg(inf.humanReadableName()));
            proc.waitForFinished();
            qDebug() << proc.readAll();
        }
        

        You can adjust it to fit your needs.

        1 Reply Last reply
        0

        • Login

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