Home  >  Article  >  Backend Development  >  How to Retrieve DLL or EXE File Versions Programmatically in C or C ?

How to Retrieve DLL or EXE File Versions Programmatically in C or C ?

Barbara Streisand
Barbara StreisandOriginal
2024-10-30 01:25:28314browse

How to Retrieve DLL or EXE File Versions Programmatically in C or C  ?

How to Programmatically Retrieve DLL or EXE File Versions in C or C

Often, when troubleshooting software issues or verifying file integrity, it becomes necessary to obtain the version information for a given DLL or EXE file. By utilizing native Win32 APIs, C and C developers can effectively retrieve these version numbers, which are typically presented as a four-part dotted notation (e.g., x.x.x.x) under the "Details" tab in the file's Properties window.

To accomplish this task, one must employ the GetFileVersionInfo API. As documented on MSDN, this API provides access to version information associated with the specified file. By using this API, developers can obtain the product version and file version of DLLs and EXEs.

The following code sample demonstrates how to retrieve the version information using GetFileVersionInfo and VerQueryValue:

<code class="c">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, regardless of system architecture
                    // Get the version numbers from dwFileVersionMS and 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;
}</code>

By utilizing the aforementioned APIs and code sample, programmers can effortlessly retrieve the version information of any DLL or EXE file. This capability proves invaluable when debugging or conducting detailed file inspections.

The above is the detailed content of How to Retrieve DLL or EXE File Versions Programmatically in C or 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