Home >Backend Development >C++ >How to Programmatically Get Version Information for DLLs and EXEs?

How to Programmatically Get Version Information for DLLs and EXEs?

Susan Sarandon
Susan SarandonOriginal
2024-10-29 14:17:02473browse

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:

<code class="python">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;
}</code>

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.

The above is the detailed content of How to Programmatically Get Version Information for DLLs and EXEs?. 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