Home  >  Article  >  Backend Development  >  How to Programmatically Get the Version of a DLL or EXE File in C/C ?

How to Programmatically Get the Version of a DLL or EXE File in C/C ?

Susan Sarandon
Susan SarandonOriginal
2024-10-30 19:57:02948browse

How to Programmatically Get the Version of a DLL or EXE File in C/C  ?

How to Get the Version of a DLL or EXE File Programmatically

Obtaining the product and file versions of a DLL or EXE file is a crucial aspect of software development. To accomplish this in C or C using Win32 native APIs, follow these steps:

  1. Utilize the GetFileVersionInfo API:

    This function provides access to version information associated with the specified file.

  2. Example Implementation:

    The following code demonstrates how to use the GetFileVersionInfo API to retrieve the version numbers:

    DWORD verHandle = 0;
    UINT size = 0;
    LPBYTE lpBuffer = NULL;
    DWORD verSize = GetFileVersionInfoSize(szVersionFile, &verHandle);
    
    if (verSize != NULL)
    {
        LPSTR verData = new char[verSize];
    
        if (GetFileVersionInfo(szVersionFile, verHandle, verSize, verData))
        {
            if (VerQueryValue(verData, "\", (VOID FAR* FAR*)&lpBuffer, &size))
            {
                if (size)
                {
                    VS_FIXEDFILEINFO *verInfo = (VS_FIXEDFILEINFO *)lpBuffer;
                    if (verInfo->dwSignature == 0xfeef04bd)
                    {
                        // DWORD is always 32 bits, so first two revision numbers
                        // come from dwFileVersionMS, last two come from dwFileVersionLS
                        TRACE("File Version: %d.%d.%d.%d\n",
                            (verInfo->dwFileVersionMS >> 16) & 0xffff,
                            (verInfo->dwFileVersionMS >> 0) & 0xffff,
                            (verInfo->dwFileVersionLS >> 16) & 0xffff,
                            (verInfo->dwFileVersionLS >> 0) & 0xffff
                        );
                    }
                }
            }
        }
        delete[] verData;
    }

By leveraging the GetFileVersionInfo API, you can effectively retrieve the version information of DLL or EXE files, enabling you to perform version checking and other related tasks.

The above is the detailed content of How to Programmatically Get the Version of a DLL or EXE File in C/C ?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn