Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

String Split



  • Hello,

    I with this code can download dll from url and can save to system32.

    HRESULT hr;
          LPCTSTR Url = _T("http://sitename.com/dlls/dllname.dll"), File = _T("C:\\Users\\user\\Desktop\\testfile\\dllname.dll");
          hr = URLDownloadToFile (0, Url, File, 0, 0);
          switch (hr)
              {
                  case S_OK:
                      qDebug() << "Successful download\n";
                      break;
                  case E_OUTOFMEMORY:
                      qDebug() << "Out of memory error\n";
                      break;
                  case INET_E_DOWNLOAD_FAILURE:
                      qDebug() << "Cannot access server data\n";
                      break;
                  default:
                      qDebug() << "Unknown error\n";
                      break;
              }
    

    But I have php file and I getting text of this file and strings like this.

    dllname1.dll,dllname2dll,

    First I need to split this string with " , ". After that I need to get name of dll's and download all dll to system32 with while.
    How can I do this ?


  • Lifetime Qt Champion

    Hi,

    For splitting: QString::split. As for moving your .dll there, you would have first to gain admin rights. In any case, it's always a bad idea to put application related .dlls in system32.



  • Somebody can help me please I dont know how can I code this split and loop :(


  • Lifetime Qt Champion

    Hi @jondoe

    QString files = "test1.dll,test2.dll,test3.dll";
    const QStringList list = files.split(',');
    for (const QString &name : list {
      qDebug() << name;
    }
    


  • @aha_1980

    How can I use this with download function ?



  • @jondoe said in String Split:

    @aha_1980

    How can I use this with download function ?

    for (const QString &name : list {
      downloadFile(name);
    }
    

  • Lifetime Qt Champion

    @jondoe

    Try this:

    QString dlls = "test1.dll,test2.dll,test3.dll";
    const QStringList list = dlls.split(',');
    
    for (const QString &name : list) {
      const QString url = QString("http://sitename.com/dlls/") + name;
      const QString file = QString("C:\\Users\\user\\Desktop\\testfile\\") + name;
      qDebug() << "Downloading" << url << "to" << file << "...";
      HRESULT hr = URLDownloadToFileW(0, (LPCWSTR)url.utf16(), (LPCWSTR)file.utf16(), 0, 0);
    }
    
    


  • @Gojir4

    I tried like this but I got error.

    QString getdlls = "http://sitename/dlls.php";
      QByteArray dllssonuc = QResolveUrl(getdlls);
    
      //qDebug() << "testedek" << dllssonuc;
    
      QString files = dllssonuc;
      const QStringList list = files.split(',');
      for (const QString &name : list) {
        //qDebug() << "deneyelim" << name;
    
          if (name != "")
          {
        HRESULT hr;
            LPCTSTR Url = _T("http://sitename.com/dlls/" + name), File = _T("C:\\Users\\user\\Desktop\\testklasor\\" + name);
            hr = URLDownloadToFile (0, Url, File, 0, 0);
            switch (hr)
                {
                    case S_OK:
                        qDebug() << "Successful download\n";
                        break;
                    case E_OUTOFMEMORY:
                        qDebug() << "Out of memory error\n";
                        break;
                    case INET_E_DOWNLOAD_FAILURE:
                        qDebug() << "Cannot access server data\n";
                        break;
                    default:
                        qDebug() << "Unknown error\n";
                        break;
                }
            }
    
    
      }
    


  • @aha_1980

    Error like this -> invalid conversion from 'const wchar_t*' to 'char' [-fpermissive] LPCTSTR Url



  • Hi,

    you need to do conversion for that. please refer this code if that help.

    Required Libs

    #include <Windows.h>
    #include <urlmon.h>
    #include <Wininet.h>
    #pragma comment(lib, "urlmon.lib")
    #pragma comment(lib,"wininet.lib")
    

    To Query DLLs

        QString data = "test1.dll,test2.dll,test3.dll";
        QStringList list = data.split(",");
        for (const QString &name : list ) {
            if (name != "") downloadFile(name);
        }
    

    To Download DLL

    void MainWindow::downloadFile(QString name)
    {
        HRESULT hr;
        LPCTSTR Url = (LPTSTR)QString("http://sitename.com/dlls/" + name).utf16(); // Convert QString to LPCTSTR 
        LPCTSTR File = (LPTSTR)QString("C:\\Users\\user\\Desktop\\testklasor\\" + name).utf16();
        hr = URLDownloadToFile (0, Url, File, 0, 0);
        switch (hr)
        {
        case S_OK:
            qDebug() << "Successful download\n";
            break;
        case E_OUTOFMEMORY:
            qDebug() << "Out of memory error\n";
            break;
        case INET_E_DOWNLOAD_FAILURE:
            qDebug() << "Cannot access server data\n";
            break;
        default:
            qDebug() << "Unknown error\n" << hr;
            break;
        }
    }
    


  • @TobbY

    Thank you for answer but debug like this.

    Unknown error
    -2146697211
    Unknown error
    -2146697211



  • @TobbY said in String Split:

    URLDownloadToFile

    Hi,
    Have you tried it with correct url and dlls ?



  • @TobbY yes but still same error.



  • Hi,
    Please check error code. INET_E_RESOURCE_NOT_FOUND (0x800C0005L or -2146697211)

    https://msdn.microsoft.com/en-us/library/bb268233(v=vs.85).aspx


  • Lifetime Qt Champion

    @jondoe if you don't copy my code 1:1, you'll have to fix yourself.



  • @aha_1980 said in String Split:

    @jondoe

    Try this:

    QString dlls = "test1.dll,test2.dll,test3.dll";
    const QStringList list = dlls.split(',');
    
    for (const QString &name : list {
      const QString url = QString("http://sitename.com/dlls/") + name;
      const QString file = QString("C:\\Users\\user\\Desktop\\testfile\\") + name;
      qDebug() << "Downloading" << url << "to" << file << "...";
      HRESULT hr = URLDownloadToFileW(0, (LPCWSTR)url.utf16(), (LPCWSTR)file.utf16(), 0, 0);
    }
    
    

    Hi! You forgot closing ) in the for loop)).


  • Lifetime Qt Champion

    @Cobra91151 said in String Split:

    Hi! You forgot closing ) in the for loop)).

    Psst. Don't tell anyone. It's part of the homework ;)



  • If you are using Qt I don't see why you have to complicate your life using LPCTSTR and stuff like that.

    const QString dlls = "test1.dll,test2.dll,test3.dll";
    const QStringList list = dlls.split(',');
    QEventLoop waitLoop;
    int dllsToDownLoad =  list.size();
    auto manager = new QNetworkAccessManager;
    for (const QString &name : list){
        auto destinationFile = new QFile("C:/Users/user/Desktop/testfile/" + name);
        if (!destinationFile.open(QFile::WriteOnly)){
            --dllsToDownLoad ;
            continue;
        }
        QNetworkReply* reply = manager->get(QNetworkRequest(QUrl::fromUserInput("http://sitename.com/dlls/" + name)));
        QObject::connect(reply, &QNetworkReply::readyRead(), [destinationFile, reply]() -> void {
            destinationFile->write(reply->readAll());
        });
        QObject::connect(reply, &QNetworkReply::finished(), [&dllsToDownLoad, reply, &waitLoop]() -> void {
            reply->deleteLater();
            destinationFile->deleteLater();
            if (--dllsToDownLoad == 0){
                manager->deleteLater();
                waitLoop.quit();
            }
        });
    }
    waitLoop.exec();
    }
    

Log in to reply