在 Qt (wininet) 中使用 winAPI 函数

Using winAPI functions in Qt (wininet)

我正在尝试制作一些 ftp 样式的上传功能,因为我正在使用一些 windows 库,但我在编译时遇到了一些麻烦。

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);

}

当我尝试编译它时,我遇到了一些未解决的外部符号错误

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)

我需要在配置文件中添加一些指令才能工作吗? 还是我还缺少其他东西?

注意:我正在为 msvc2015 使用 qt 5.6 c++

链接器找不到的所有函数都与wininet有关。您需要像这样将 wininet lib 添加到您的 .pro 文件中:

win32 {

LIBS += -lwininet

}