c++将.rc中的版本转换为代码

c++ get version from .rc into code

本文关键字:转换 代码 版本 rc c++      更新时间:2023-10-16

可能重复:
如何在Visual C++中读取版本资源

在我的c++项目中,我添加了一个.rc文件,在那里我可以存储文件版本、可执行文件描述、版权等。

好的,我编译了,我转到explorer->文件属性,我看到了表单中的所有字段。

我的问题是:如果我需要从项目中读取自己的文件版本(例如显示为表单(,我该如何做到这一点?

感谢

Windows提供了一组API调用,用于从可执行文件中检索版本信息。下面的代码片段应该可以帮助您入门。

bool GetVersionInfo(
    LPCTSTR filename,
    int &major,
    int &minor,
    int &build,
    int &revision)
{
    DWORD   verBufferSize;
    char    verBuffer[2048];
    //  Get the size of the version info block in the file
    verBufferSize = GetFileVersionInfoSize(filename, NULL);
    if(verBufferSize > 0 && verBufferSize <= sizeof(verBuffer))
    {
        //  get the version block from the file
        if(TRUE == GetFileVersionInfo(filename, NULL, verBufferSize, verBuffer))
        {
            UINT length;
            VS_FIXEDFILEINFO *verInfo = NULL;
            //  Query the version information for neutral language
            if(TRUE == VerQueryValue(
                verBuffer,
                _T("\"),
                reinterpret_cast<LPVOID*>(&verInfo),
                &length))
            {
                //  Pull the version values.
                major = HIWORD(verInfo->dwProductVersionMS);
                minor = LOWORD(verInfo->dwProductVersionMS);
                build = HIWORD(verInfo->dwProductVersionLS);
                revision = LOWORD(verInfo->dwProductVersionLS);
                return true;
            }
        }
    }
    return false;
}

在可执行文件上使用这些函数:

http://msdn.microsoft.com/en-us/library/ms646981%28v=VS.85%29.aspx