How to Get the Version of a DLL or EXE File Programmatically
Obtaining the product and file versions of a DLL or EXE file is a crucial aspect of software development. To accomplish this in C or C using Win32 native APIs, follow these steps:
Utilize the GetFileVersionInfo API:
This function provides access to version information associated with the specified file.
Example Implementation:
The following code demonstrates how to use the GetFileVersionInfo API to retrieve the version numbers:
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, so first two revision numbers // come from dwFileVersionMS, last two come from 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; }
By leveraging the GetFileVersionInfo API, you can effectively retrieve the version information of DLL or EXE files, enabling you to perform version checking and other related tasks.
The above is the detailed content of How to Programmatically Get the Version of a DLL or EXE File in C/C ?. For more information, please follow other related articles on the PHP Chinese website!