Search code examples
c++versioninfo

c++ get version from .rc into code


Possible Duplicate:
How do I read from a version resource in Visual C++

in my c++ project i've added a .rc file where I can store the file version, the executable description, copyright, etc..

and is ok, I compile, i go to the explorer -> file property and I see all the fields in the form.

My question is: if I need to read from the project the own file version ( to show into a form for example), how can I do this?

thanks


Solution

  • Windows provides a set of API calls for retrieving the version information from executable files. The following snippet of code should help you get started.

    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;
    }