DLL 版本信息
darra::ethercat::statics 命名空间提供 DLL 库的版本信息和元数据查询, 用于诊断、日志记录和兼容性检查。
通过 darra::ethercat::statics 静态函数访问。
版本信息属性
dll_version()
namespace darra::ethercat::statics {
std::optional<DllVersionInfo> dll_version(dll_t& dll);
}
获取完整的 DLL 版本信息结构。读取失败返回 std::nullopt。
返回值:
std::optional<DllVersionInfo>— 包含版本号、构建日期、校验和等完整信息
相关结构:
struct DllVersionInfo {
uint16_t major; // 主版本号
uint16_t minor; // 次版本号
uint16_t patch; // 修订号
uint16_t build; // 构建号
std::string build_date; // 构建日期 (ISO 8601)
};
示例:
auto info = darra::ethercat::statics::dll_version(dll);
if (info) {
printf("DLL 版本: %u.%u.%u.%u\n",
info->major, info->minor, info->patch, info->build);
printf("构建日期: %s\n", info->build_date.c_str());
}
version_number()
namespace darra::ethercat::statics {
std::optional<std::string> version_number(dll_t& dll);
}
获取 DLL 版本号字符串, 格式为 X.X.X.X。
返回值:
std::optional<std::string>— 版本号 (例如:1.0.0.0)
示例:
auto version = darra::ethercat::statics::version_number(dll);
if (version) {
printf("当前 DLL 版本: %s\n", version->c_str());
if (version->rfind("1.0", 0) == 0) {
printf("使用 v1.0 系列\n");
}
}
build_date()
namespace darra::ethercat::statics {
std::optional<std::string> build_date(dll_t& dll);
}
获取 DLL 编译日期。
返回值:
std::optional<std::string>— 构建日期 (ISO 8601 格式)
示例:
auto bd = darra::ethercat::statics::build_date(dll);
if (bd) {
printf("DLL 构建于: %s\n", bd->c_str());
}
checksum()
namespace darra::ethercat::statics {
std::optional<std::string> checksum(dll_t& dll);
}
获取 DLL 的唯一识别码。
返回值:
std::optional<std::string>— 识别码 (5 个字符)
示例:
auto cs = darra::ethercat::statics::checksum(dll);
if (cs) {
printf("DLL 校验和: %s\n", cs->c_str());
}
完整示例
应用程序日志
在日志中记录 DLL 版本信息:
#include "ethercat.hpp"
using namespace darra::ethercat;
int main() {
dll_t dll;
auto info = statics::dll_version(dll);
printf("=== Darra EtherCAT Master ===\n");
if (info) {
printf("DLL 版本: %u.%u.%u.%u\n",
info->major, info->minor, info->patch, info->build);
printf("构建日期: %s\n", info->build_date.c_str());
}
auto cs = statics::checksum(dll);
if (cs) printf("校验和: %s\n", cs->c_str());
printf("============================\n");
return 0;
}
版本兼容性检查
确保应用程序与 DLL 版本兼容:
bool check_minimum_version(dll_t& dll, int min_major, int min_minor) {
auto info = darra::ethercat::statics::dll_version(dll);
if (!info) {
printf("错误: 无法获取 DLL 版本\n");
return false;
}
if (info->major < min_major ||
(info->major == min_major && info->minor < min_minor)) {
printf("错误: DLL 版本过低 (%u.%u.%u.%u), 需要 v%d.%d+\n",
info->major, info->minor, info->patch, info->build,
min_major, min_minor);
return false;
}
printf("DLL 版本兼容: %u.%u.%u.%u\n",
info->major, info->minor, info->patch, info->build);
return true;
}
诊断信息收集
生成诊断报告:
std::string generate_report(dll_t& dll) {
std::ostringstream oss;
oss << "=== 系统诊断报告 ===\n\n";
oss << "DLL 信息:\n";
auto info = darra::ethercat::statics::dll_version(dll);
if (info) {
oss << " 版本: " << info->major << "." << info->minor
<< "." << info->patch << "." << info->build << "\n";
oss << " 构建: " << info->build_date << "\n";
}
auto cs = darra::ethercat::statics::checksum(dll);
if (cs) oss << " 校验和: " << *cs << "\n";
return oss.str();
}
唯一标识符生成
为日志文件或会话生成唯一标识符:
std::string create_session_id(dll_t& dll) {
auto cs = darra::ethercat::statics::checksum(dll);
auto t = std::time(nullptr);
char buf[32];
std::strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", std::localtime(&t));
return std::string("SESSION_") + (cs ? *cs : "UNKNOWN") + "_" + buf;
}
注意事项
版本管理
在生产环境中, 建议记录 DLL 版本信息到应用程序日志, 便于问题追溯和版本管理。
校验和用途
checksum() 返回 DLL 版本唯一标识码, 主要用于快速区分不同构建版本, 不应用于安全验证。