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



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



  • 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


  • Moderators

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.