Home  >  Article  >  Backend Development  >  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 ?

DDD
DDDOriginal
2024-11-01 08:23:02436browse

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 .

The above is the detailed content of How can I retrieve version information from DLLs and EXEs using Win32 API 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