Qt如何让应用程序获取自身的md5校验和

Qt how to get application to get md5 checksum of itself

本文关键字:md5 校验和 获取 应用程序 Qt      更新时间:2023-10-16

正在开发Qt应用程序。我正在尝试让 exe 文件在运行时返回自身的 md5 校验和。我该怎么做?

我试过这个:

QFile theFile("file.exe");
QByteArray thisFile;
if (theFile.open(QIODevice::ReadOnly))
{
    thisFile = theFile.readAll();
}
else
{
    qDebug() << "Can't open";
}
qDebug() << QString("%1").arg(thisFile.length());
fileMd5 = QString(QCryptographicHash::hash((thisFile), QCryptographicHash::Md5).toHex().toUpper());
qDebug() << fileMd5;

但是,这不会返回正确的值。

更新:

我让它与其他文件一起工作。问题似乎是我无法在 exe 运行时读取它。

最终更新:

这是解决方案:

QFile theFile(QCoreApplication::applicationFilePath());
QByteArray thisFile;
if (theFile.open(QIODevice::ReadOnly))
{
    thisFile = theFile.readAll();
}
else
{
    qDebug() << "Can't open file.";
}
QString fileMd5 = QString(QCryptographicHash::hash((thisFile), QCryptographicHash::Md5).toHex());
qDebug() << fileMd5;

你忘了在theFile上打电话给open

if (!theFile.open(QIODevice::ReadOnly))
    // Handle error here

此外,您应该使用 QCoreApplication::applicationFilePath() 来获取可执行文件的路径。

您必须创建一个独立的应用程序(我们称之为myApp),它检查MD5sum并将其与您的PHP脚本进行比较,并在需要时要求更新或直接加载应用程序。

像这样:myApp =>需要更新?(更新) : ( TheRealApp

好的,看起来它只是没有找到该文件。我尝试了绝对路径而不是相对路径,它奏效了。我必须弄清楚出了什么问题,但它看起来可以在运行时读取自己。