Get File Version in your code

本文介绍了一种使用C++实现的方法来获取Windows环境下程序文件的版本信息。通过调用GetFileVersionInfo等API函数,演示了如何从资源文件中读取FILEINFO值,并将其转换为易于理解的版本号字符串。
 
//////////////////////////////////////////////////////////////
//
//         File: version.cpp
//  Description: Sample code for getting version info
//      Created: 2008-1-4
//       Author: Ken Zhang
//       E-Mail: cpp.china@hotmail.com
//
//////////////////////////////////////////////////////////////

/*
    The following code shows how to get FILEINFO value from resource file.

    These WIN32 functions will be used:

        * GetFileVersionInfo
        * GetFileVersionInfoSize
        * VerQueryValue
        * GetModuleFileName
 */

#include <windows.h>
#include <tchar.h>
#include <string>
#include <iostream>

#pragma comment(lib, "version.lib")
using namespace std;

bool GetFileVersion(HMODULE hModule, WORD *pBuffer)
{
    TCHAR fname[MAX_PATH];
    VS_FIXEDFILEINFO *pVi;
    DWORD dwHandle;
    string str;

    if (::GetModuleFileName(hModule, fname, MAX_PATH))
    {
        int size = GetFileVersionInfoSize(fname, &dwHandle);

        if (size > 0) {
            BYTE *buffer = new BYTE[size];
            
            if (GetFileVersionInfo(fname, dwHandle, size, buffer)) {
                if (VerQueryValue(buffer, _T("//"), (LPVOID *)&pVi, (PUINT)&size)) {
                    pBuffer[0] = HIWORD(pVi->dwFileVersionMS);
                    pBuffer[1] = LOWORD(pVi->dwFileVersionMS);
                    pBuffer[2] = HIWORD(pVi->dwFileVersionLS);
                    pBuffer[3] = LOWORD(pVi->dwFileVersionLS);

                    delete buffer;
                    return true;
                }
            }

            delete buffer;
        }
    }

    return false;
}

string GetFileVersion(HMODULE hModule)
{
    string str;
    WORD buffer[4];

    if (GetFileVersion(hModule, buffer))
    {
        char str2[32];

        for (int i = 0; i < sizeof(buffer)/sizeof(WORD); i++)
        {
            itoa(buffer[i], str2, 10);
            str += str2;

            if (i != sizeof(buffer)/sizeof(WORD) - 1)
            {
                str += ".";
            }
        }
    }

    return str;
}

void main()
{
    cout << "Current version is: " << GetFileVersion(::GetModuleHandle(NULL)) << endl;
}
这些是来自 SonarQube 静态代码分析工具的警告和建议。我将逐个解释它们的含义: 1. "SCM provider autodetection failed. Please use "sonar.scm.provider" to define SCM of your project, or disable the SCM Sensor in the project settings." 这表示 SonarQube 无法自动检测到你的项目的源代码管理(SCM)提供者。你需要手动设置 "sonar.scm.provider" 参数来定义你的项目的 SCM,或在项目设置中禁用 SCM 传感器。 2. "Your code is analyzed as compatible with python 2 and 3 by default. This will prevent the detection of issues specific to python 2 or python 3. You can get a more precise analysis by setting a python version in your configuration via the parameter "sonar.python.version"." 这表示默认情况下,你的代码被分析为与 Python 2 和 3 兼容。这将阻止检测特定于 Python 2 或 Python 3 的问题。你可以通过在配置中设置 Python 版本参数 "sonar.python.version" 来获得更精确的分析。 3. "There are problems with file encoding in the source code. Please check the scanner logs for more details." 这表示源代码中存在文件编码问题。请检查扫描器日志以获取更多详细信息。 4. "24 unanalyzed C files were detected in this project during the last analysis. C cannot be analyzed with your current SonarQube edition. Please consider upgrading to Developer Edition to find Bugs, Code Smells, Vulnerabilities and Security Hotspots in this file." 这表示在最近的分析中检测到了 24 个未分析的 C 文件。你当前使用的 SonarQube 版本无法分析 C 语言。请考虑升级到 Developer Edition,以便在该文件中查找错误、代码异味、漏洞和安全热点。 希望这些解释对你有所帮助!如果你需要进一步的解释或有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值