在 Qt 中使用 winAPI 函数 (wininet)

Using winAPI functions in Qt (wininet)

提问人:Daniel Sanin 提问时间:1/31/2016 最后编辑:demonplusDaniel Sanin 更新时间:1/31/2016 访问量:549

问:

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

上传文件.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

上传文件.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 ++

Qt C++11 WinAPI 未解析外部

评论

1赞 Igor Tandetnik 1/31/2016
您需要与 链接。如何在Qt项目中实现这一点,我不知道。在某个地方必须有一个位置来指定其他外部库。Wininet.lib
0赞 IInspectable 1/31/2016
什么是未定义的引用/未解析的外部符号错误以及如何修复它?

答:

1赞 demonplus 1/31/2016 #1

链接器找不到的所有函数都与 wininet 相关。您需要将 wininet lib 添加到您的 .pro 文件中,如下所示:

win32 {

LIBS += -lwininet

}