File version

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul

    #1

    File version

    Hi,

    Does anyone know how to check a version of a file in C++? Any WIN32 API
    that does this?

    Please advice, thanks!
    -P
  • Jochen Kalmbach [MVP]

    #2
    Re: File version

    Hi Paul!
    [color=blue]
    > Does anyone know how to check a version of a file in C++? Any WIN32 API
    > that does this?[/color]

    Here is susch a function:

    <code>
    // Returns
    // - a negative value if an error occured (more information can be
    retrived by calling "GetLastErr or")
    // - "0" if the version is the same
    // - "1" if the file-version is lower than the provided version
    // - "2" if the file-version is higher than the provided version
    int CompareFileVers ion(LPCTSTR szFileName, WORD v1, WORD v2, WORD v3,
    WORD v4)
    {
    LPVOID vData = NULL;
    VS_FIXEDFILEINF O *fInfo = NULL;
    DWORD dwHandle;
    ULONGLONG ullVersion;
    ullVersion = v4;
    ullVersion |= (((ULONGLONG) v3) << 16);
    ullVersion |= (((ULONGLONG) v2) << 32);
    ullVersion |= (((ULONGLONG) v1) << 48);

    DWORD dwSize = GetFileVersionI nfoSize(szFileN ame, &dwHandle);
    if (dwSize <= 0)
    return -1;

    // try to allocate memory
    ULONGLONG fileVersion = 0;
    vData = malloc(dwSize);
    if (vData == NULL)
    {
    SetLastError(ER ROR_OUTOFMEMORY );
    return -1;
    }

    // try to the the version-data
    if (GetFileVersion Info(szFileName , dwHandle, dwSize, vData) == 0)
    {
    DWORD gle = GetLastError();
    free(vData);
    SetLastError(gl e);
    return -1;
    }

    // try to query the root-version-info
    UINT len;
    if (VerQueryValue( vData, _T("\\"), (LPVOID*) &fInfo, &len) == 0)
    {
    DWORD gle = GetLastError();
    free(vData);
    SetLastError(gl e);
    return -1;
    }

    // build the file-version
    fileVersion = ((ULONGLONG)fIn fo->dwFileVersionL S) +
    ((ULONGLONG)fIn fo->dwFileVersionM S << 32);

    free(vData);

    // compare if the installed version is newer than this hotfix
    if (fileVersion > ullVersion)
    {
    // The file is newer than the provided version
    return 2;
    }
    else if (fileVersion < ullVersion)
    {
    // The file is older than the provided version
    return 1;
    }
    return 0;
    }


    int _tmain()
    {
    CompareFileVers ion(_T("c:\\DOT NETFX_v11.EXE") , 1, 1, 4322, 572);
    }
    </code>


    --
    Greetings
    Jochen

    My blog about Win32 and .NET

    Comment

    Working...