Qt如何让应用程序获取自身的md5校验和
Qt how to get application to get md5 checksum of itself
正在开发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
)
好的,看起来它只是没有找到该文件。我尝试了绝对路径而不是相对路径,它奏效了。我必须弄清楚出了什么问题,但它看起来可以在运行时读取自己。
相关文章:
- 如何用尽可能少的数据将数据缓冲区计算为零校验和值
- 计算校验和的逐位运算符
- 如何从存储在 QByteArray 中的十六进制值计算校验和
- 如何在 c++ 中计算对象的哈希/校验和/指纹?
- C++文件传输的校验和算法
- 如何从 IP 校验和字段中计算十六进制值
- 计算句子的校验和(XOR)(由不同变量/类型的组合形成)
- 目录校验和
- 编译后如何计算某些二进制功能(或基本块)的校验和
- PMS5003带有ESP8266-许多校验和错误
- 未正确的校验和释放对象 - 打印时错误
- 将C++校验和函数转换为 Java
- 确定是否使用boost.asio计算UDP校验和
- 校验和CRC 16从C 到Java
- 不同的CRC32校验和同一文件
- Qt,tcp/ip通信校验和
- 正在计算ICMP数据包校验和
- 错误的校验和错误遍布程序
- Qt如何让应用程序获取自身的md5校验和
- 如何计算大文件的 MD5 和 SHA 校验和