从 vs 的 rc 文件中获取版本号

更新项目版本号时,需要与 rc 文件的 version 同步,比较方便的方法是直接从 rc 文件中获取版本号,并应用到程序中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 删除日志检查
bool GetVersion() {
  // get the filename of the executable containing the version resource
  wchar_t filename[MAX_PATH + 1];
  if (GetModuleFileName(nullptr, filename, MAX_PATH) == 0) {
    return false;
  }
 
  // allocate a block of memory for the version info
  DWORD dummy;
  DWORD size = GetFileVersionInfoSize(filename, &dummy);
  if (size == 0) {
    return false;
  }
 
  auto data = std::make_unique<BYTE[]>(size);
  // load the version info
  if (!GetFileVersionInfo(filename, 0, size, &data[0])) {
    return false;
  }
 
  uint len = 0;
  VS_FIXEDFILEINFO* fixed_file_info = 0;
  if (!VerQueryValue(&data[0], TEXT("\\"),
                     reinterpret_cast<void**>(&fixed_file_info), &len)) {
    return false;
  }
 
  // version 为版本号
  // 需要窄字节的,可以另外转
  std::wstring version;
  base::SStringPrintf(&version, L"%u.%u.%u",
                      HIWORD(fixed_file_info->dwProductVersionMS),
                      LOWORD(fixed_file_info->dwProductVersionMS),
                      HIWORD(fixed_file_info->dwProductVersionLS));
 
  return true;
}

  

posted @   strive-sun  阅读(604)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示