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 ? -
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.
-
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); }
-
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; } } }
-
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; } }
-
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
-
@aha_1980 said in String Split:
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 thefor
loop)). -
@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(); }