PDA

View Full Version : Can't convert QString to LPCWSTR



xMarioZ
22nd March 2016, 20:08
Hello guys, i have this code:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "urlmon.h"
#include "string"
#include "iostream"
#include "cstring"

//...A lot of s*it!

void MainWindow::on_pushButton_clicked()
{
QString url;
QString path;

path = ui->lineEdit_2->text();
url= ui->lineEdit->text();
ui->label_2->setText(url+"//"+path);
URLDownloadToFile(NULL,&url,&path,NULL);
}

//other sh*t

I've got an error in the line

URLDownloadToFile(NULL,&url,&path,NULL);


...\mainwindow.cpp:27: error: cannot convert 'QString*' to 'LPCWSTR {aka const wchar_t*}' for argument '2' to 'HRESULT URLDownloadToFileW(LPUNKNOWN, LPCWSTR, LPCWSTR, DWORD, LPBINDSTATUSCALLBACK)'
URLDownloadToFile(NULL,&url,&path,NULL);

d_stranz
24th March 2016, 16:03
That's because a pointer to a QString ("&url") is not a wchar_t pointer. Use one of these as the argument:



url.toStdWString().c_str()

// or

url.utf16()


You will probably run into trouble later in your coding life if you continue to write code that #includes system files with the "" convention instead of the <> convention:



// Should be
#include <cstring>
// and not
#include "cstring"


This is because the "" convention tells the compiler to look first in the current directory (and in directories you explicitly tell the compiler to look in) for the include file, then look in system directories. The <> convention tells the compiler to look only in system directories. If one of those other directories accidentally contains a file with the same name as the system file, then you'll likely end up with all sorts of unexplained compilation errors that will take a while to figure out. If you mean to include a system file, then help the compiler out by telling it that.