Search code examples
qtc++11winapiunresolved-external

Using winAPI functions in Qt (wininet)


I'm trying to make some ftp style upload function for that I'm using some windows libraries but i'm getting some troubles at compiling time.

uploadfile.h

#ifndef UPLOADFILE_H
#define UPLOADFILE_H

#include <QDialog>

#include <Windows.h>
#include <WinInet.h>
#include <Winineti.h>
#include <string>
#include <stdexcept>

using namespace std;

namespace Ui {
class UploadFile;
}

class UploadFile : public QDialog
{
    Q_OBJECT

public:
    explicit UploadFile(QWidget *parent = 0);
    ~UploadFile();

    void setConnection(string host, int port, string user, string password);
    void uploadFile(string filename, string newFileNane);
    bool getStatErr();

private:
    Ui::UploadFile *ui;
    HINTERNET hInternet;
    HINTERNET hFtp;
    int value;
};

#endif // UPLOADFILE_H

uploadfile.cpp

#include "uploadfile.h"
#include "ui_uploadfile.h"

UploadFile::UploadFile(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::UploadFile)
{
    ui->setupUi(this);
}

UploadFile::~UploadFile()
{
    delete ui;
}

void UploadFile::setConnection(string host, int port, string user, string password)
{
    LPCWSTR Host=(LPCWSTR)host.c_str();
    LPCWSTR User = (LPCWSTR)user.c_str();
    LPCWSTR Password = (LPCWSTR)password.c_str();

    hInternet = InternetOpenW(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
    hFtp = InternetConnectW(hInternet, Host, port, User, Password, INTERNET_SERVICE_FTP, 0, 0);

}

void UploadFile::uploadFile(string filename, string newFileNane)
{
    LPCWSTR FileName = (LPCWSTR)filename.c_str();
    LPCWSTR NewFileName = (LPCWSTR)newFileNane.c_str();
    FtpPutFileW(hFtp, FileName, NewFileName, FTP_TRANSFER_TYPE_BINARY, 0);
    if (FtpPutFileW(hFtp, FileName, NewFileName, FTP_TRANSFER_TYPE_BINARY, 0)) value = 1;
    else value = 0;
}

bool UploadFile::getStatErr()
{
    if (value==1){
        return true;
    }
    else
    {
        return false;
    }
    InternetCloseHandle(hFtp);
    InternetCloseHandle(hInternet);

}

when i try to compile it i get some unresolved external symbol errors

uploadfile.obj:-1: error: LNK2019: unresolved external symbol __imp__InternetOpenW@20 referenced in function "public: void __thiscall UploadFile::setConnection(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setConnection@UploadFile@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H00@Z)
uploadfile.obj:-1: error: LNK2019: unresolved external symbol __imp__InternetConnectW@32 referenced in function "public: void __thiscall UploadFile::setConnection(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?setConnection@UploadFile@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H00@Z)
uploadfile.obj:-1: error: LNK2019: unresolved external symbol __imp__FtpPutFileW@20 referenced in function "public: void __thiscall UploadFile::uploadFile(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?uploadFile@UploadFile@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z)

Did i need to add some directives to the configuration file in order to work? or there's something else am i missing?

Note: i'm using qt 5.6 c++ for msvc2015


Solution

  • All the functions linker can't find are related to wininet. You need to add wininet lib to your .pro file like that:

    win32 {
    
    LIBS += -lwininet
    
    }