"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Programmatically Get Version Information for DLLs and EXEs?

How to Programmatically Get Version Information for DLLs and EXEs?

Published on 2024-11-10
Browse:978

How to Programmatically Get Version Information for DLLs and EXEs?

How to Programmatically Obtain Version Information for DLLs and EXEs

In the realm of software development, it often becomes necessary to retrieve the version information associated with dynamic link libraries (DLLs) and executable programs (EXEs). By utilizing Win32 native APIs accessible through C or C , developers can seamlessly acquire the product and file versions of these executable entities.

The GetFileVersionInfo API: Unlocking Version Information

The key to unlocking version information lies in the GetFileVersionInfo API. This powerful function populates a buffer with data pertaining to the specified DLL or EXE file. Let us delve into the intricacies of its usage:

  • Determine the File Size: GetFileVersionInfoSize guides the optimization of memory allocation by providing the requisite buffer size.
  • Retrieving Version Data: GetFileVersionInfo grants access to the version information securely stored within the buffer.
  • Querying the Buffer: VerQueryValue extracts a subset of the version data, matching the user's predefined criteria.
  • Interpreting Version Numbers: By understanding the underlying structure of the version information, developers can accurately parse the four-part dotted version number (x.x.x.x) from the dwFileVersionMS and dwFileVersionLS values.

Exemplifying Version Retrieval in Practice

The following code sample illustrates the practical application of the GetFileVersionInfo API:

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) {
                    // Parse the file version into a four-part dotted string
                    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;
}

In conclusion, the GetFileVersionInfo API provides a comprehensive solution for programmatically obtaining version information from DLLs and EXEs. By tapping into this functionality, developers can automate version retrieval tasks, ensuring accuracy and efficiency in their software development endeavors.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3