2017-01-23 111 views
0

我有問題,在下面的代碼中,「QSslCertificate :: fromPath」找不到我指定的文件,但是當我檢查它時使用下面的fileExists函數,它告訴我該文件畢竟存在。只有當我嘗試在與我的開發PC不同的PC上運行我的應用程序時,纔會出現此問題。我在Windows 10 64 Bit上開發,測試PC是Windows 7 64 Bit。我使用QT 5.4.0。我究竟做錯了什麼?Qt:QSslCertificate :: fromPath沒有找到我的文件,但QFileInfo發現它

void MyClass::init() 
{ 

    // ... some other init code 

    QLatin1String rootCApath = QLatin1String("./ssl/rootCA.crt"); 
    if (fileExists(rootCApath)) 
     log("File exists"); // This is just a log function for displaying messages in the GUI. 
    else 
     log("File doesn't exist"); 

    static QList<QSslCertificate> cert = QSslCertificate::fromPath(rootCApath); 
    if(cert.size()==1) 
    { 
     ssl_configuration.setCaCertificates(cert); 
     m_webSocket.setSslConfiguration(ssl_configuration); 
    } 
    else 
    { 
     QString s = "Server certificate not found. Size is " + QString::number(cert.size()); 
     log(s); 
    } 
} 

bool MyClass::fileExists(QString path) { 
    QFileInfo check_file(path); 
    // check if file exists and if yes: Is it really a file and no directory? 
    return check_file.exists() && check_file.isFile(); 
} 

編輯:當我閱讀證書到一個QByteArray中,並傳遞一個,那麼我的連接功能不Windows 7的PC機上做任何事情,而在我的Windows 10開發PC一切仍然正常工作。

此外,還有另一臺PC始終給我TLS握手錯誤,無論我做什麼。 3臺電腦,3個結果,令人沮喪。

回答

0

我發現我的情況的解決方案:我沒有在那裏添加OpenSSL庫LIBEAY32.DLL和ssleay32.dll到目錄我exe文件駐留在。

0

你的代碼看起來很好,在我的linux系統上工作沒有任何問題。您應該檢查相對路徑或簡單地嘗試通過QFileInfo設置證書:

QLatin1String rootCApath = QLatin1String("./ssl/rootCA.crt"); 
QFileInfo temp(rootCApath); 
QString tempssl; 
QDir dir ;//try to set dir also if this does not work dir("/home/foo/yourapppath/"); 

tempssl=dir.relativeFilePath("./ssl/rootCA.crt"); 
static QList<QSslCertificate> cert = QSslCertificate::fromPath(tempssl); 

if(cert.size()==1) 
{ 
    ssl_configuration.setCaCertificates(cert); 
    m_webSocket.setSslConfiguration(ssl_configuration); 
} 
else 
{ 
    QString s = "Server certificate not found. Size is " + QString::number(cert.size()); 
    log(s); 
} 
+0

不幸的是,這並沒有幫助,無論我是否將QDir dir設置爲QCoreApplication :: applicationDirPath()。 – Alex

相關問題