簡體   English   中英

在Qt中使用winAPI函數(wininet)

[英]Using winAPI functions in Qt (wininet)

我正在嘗試使用一些Windows庫來實現一些ftp樣式上傳功能,但是在編譯時遇到了一些麻煩。

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)

我是否需要在配置文件中添加一些指令才能工作? 還是我想念其他東西?

注意:我正在將qt 5.6 c ++用於msvc2015

鏈接器找不到的所有功能都與wininet相關。 您需要像這樣將wininet lib添加到您的.pro文件中:

win32 {

LIBS += -lwininet

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM