1

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

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

没关系,我编译,我转到资源管理器-> 文件属性,我看到表单中的所有字段。

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

谢谢

4

2 回答 2

6

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;
}
于 2011-07-20T14:20:07.150 回答
2

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

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

于 2011-07-20T14:16:45.120 回答