正在开发 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()
获取可执行文件的路径。
关于c++ - Qt如何让应用程序获取自身的md5校验和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19406634/