"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 can I retrieve version information from DLLs and EXEs using Win32 API in C or C++?

How can I retrieve version information from DLLs and EXEs using Win32 API in C or C++?

Published on 2024-11-18
Browse:955

How can I retrieve version information from DLLs and EXEs using Win32 API in C or C  ?

Obtaining Version Information for DLLs and EXEs

Many applications require the ability to retrieve version information from files. This information is used for display purposes, such as showing the version number on the properties dialog box.

The Win32 API provides several functions that can be used to obtain version information. One common approach is to use the GetFileVersionInfo API.

Using GetFileVersionInfo

The GetFileVersionInfo function retrieves information about a file's version resources. The steps involved in using this function are:

  1. Call GetFileVersionInfo with the file path and a pointer to a DWORD variable that will receive the handle to the version information.
  2. Call GetFileVersionInfoSize with the file path and the handle from step 1 to determine the size of the version information.
  3. Allocate a buffer of the size retrieved in step 2.
  4. Call GetFileVersionInfo again with the file path, handle, size, and pointer to the buffer from step 3 to retrieve the version information.

Extracting Version Data

Once the version information is retrieved, you can use the VerQueryValue function to extract specific information. The following sample code demonstrates how to extract the product version and file version numbers:

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)
            {
                TRACE("File Version: %d.%d.%d.%d\n",
                (verInfo->dwFileVersionMS >> 16) & 0xffff,
                (verInfo->dwFileVersionMS >> 0) & 0xffff,
                (verInfo->dwFileVersionLS >> 16) & 0xffff,
                (verInfo->dwFileVersionLS >> 0) & 0xffff
                );
            }
        }
    }
}

By following these steps, you can programmatically obtain the product version and file version numbers for DLLs or EXE files using Win32 native APIs in C or C .

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