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

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

// 删除日志检查
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 @ 2022-07-07 11:16  strive-sun  阅读(482)  评论(0编辑  收藏  举报