I need to get the product version and file version for a DLL or EXE file using Win32 native APIs in C or C++. I'm not looking for the Windows version, but the version numbers that you see by right-clicking on a DLL file, selecting "Properties", then looking at the "Details" tab. This is usually a four-part dotted version number xxxx
You would use the GetFileVersionInfo API.
See Using Version Information on the MSDN site.
Sample:
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)
{
// Doesn't matter if you are on 32 bit or 64 bit,
// 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;
}
All these solutions did not work properly (with my system). I found out that each of the four parts of the version number are saved as a 16-bit value.
The first two numbers are saved in the 32-bit DWORD dwFileVersionMS, and the second two in dwFileVersionLS. So I edited your code at the output section like this:
TRACE( "File Version: %d.%d.%d.%d\n",
( pFileInfo->dwFileVersionMS >> 16 ) & 0xffff,
( pFileInfo->dwFileVersionMS >> 0 ) & 0xffff,
( pFileInfo->dwFileVersionLS >> 16 ) & 0xffff,
( pFileInfo->dwFileVersionLS >> 0 ) & 0xffff
);
And it works perfectly. The output is formatted like on my system:
You get this information using the version information APIs . Here is a sample:
void PrintFileVersion( TCHAR *pszFilePath )
{
DWORD dwSize = 0;
BYTE *pbVersionInfo = NULL;
VS_FIXEDFILEINFO *pFileInfo = NULL;
UINT puLenFileInfo = 0;
// Get the version information for the file requested
dwSize = GetFileVersionInfoSize( pszFilePath, NULL );
if ( dwSize == 0 )
{
printf( "Error in GetFileVersionInfoSize: %d\n", GetLastError() );
return;
}
pbVersionInfo = new BYTE[ dwSize ];
if ( !GetFileVersionInfo( pszFilePath, 0, dwSize, pbVersionInfo ) )
{
printf( "Error in GetFileVersionInfo: %d\n", GetLastError() );
delete[] pbVersionInfo;
return;
}
if ( !VerQueryValue( pbVersionInfo, TEXT("\\"), (LPVOID*) &pFileInfo, &puLenFileInfo ) )
{
printf( "Error in VerQueryValue: %d\n", GetLastError() );
delete[] pbVersionInfo;
return;
}
// pFileInfo->dwFileVersionMS is usually zero. However, you should check
// this if your version numbers seem to be wrong
printf( "File Version: %d.%d.%d.%d\n",
( pFileInfo->dwFileVersionLS >> 24 ) & 0xff,
( pFileInfo->dwFileVersionLS >> 16 ) & 0xff,
( pFileInfo->dwFileVersionLS >> 8 ) & 0xff,
( pFileInfo->dwFileVersionLS >> 0 ) & 0xff
);
// pFileInfo->dwProductVersionMS is usually zero. However, you should check
// this if your version numbers seem to be wrong.
printf( "Product Version: %d.%d.%d.%d\n",
( pFileInfo->dwProductVersionLS >> 24 ) & 0xff,
( pFileInfo->dwProductVersionLS >> 16 ) & 0xff,
( pFileInfo->dwProductVersionLS >> 8 ) & 0xff,
( pFileInfo->dwProductVersionLS >> 0 ) & 0xff
);
}
Found these articles...sorry, but I don't have direct experience with how to do this using native APIs, so I deferred to an Internet search:
Hope these help!
This code shows the file version numbers correctly.
( pFileInfo->dwFileVersionMS >> 16 ) & 0xff,
( pFileInfo->dwFileVersionMS >> 0 ) & 0xff,
( pFileInfo->dwFileVersionLS >> 16 ) & 0xff,
( pFileInfo->dwFileVersionLS >> 0 ) & 0xff);
The easiest way is to use the GetFileVersionInfoEx or GetFileVersionInfo API functions.
You can also do it from within your application resources as explained here .
Since none of the answers mention it... I found out that you have to make different calculations depending on if you're running on 32- or 64-bit systems . That's why you find that certain answers in this question work for you, and others do not.
Here's a sample implementation I use:
if(IsWow64())
{
// 64 bit build
major = (verInfo->dwProductVersionMS >> 16) & 0xffff;
minor = (verInfo->dwProductVersionMS >> 0) & 0xffff;
revision = (verInfo->dwProductVersionLS >> 16) & 0xffff;
build = (verInfo->dwProductVersionLS >> 0) & 0xffff;
}
else
{
// 32 bit build
major = HIWORD(verInfo->dwProductVersionMS);
minor = LOWORD(verInfo->dwProductVersionMS);
revision = HIWORD(verInfo->dwProductVersionLS);
build = LOWORD(verInfo->dwProductVersionLS);
}
And the implementation of IsWow64 (not mine):
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
LPFN_ISWOW64PROCESS fnIsWow64Process;
BOOL IsWow64()
{
BOOL bIsWow64 = FALSE;
//IsWow64Process is not available on all supported versions of Windows.
//Use GetModuleHandle to get a handle to the DLL that contains the function
//and GetProcAddress to get a pointer to the function if available.
fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress(
GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
if(NULL != fnIsWow64Process)
{
if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64))
{
// Handle error...
}
}
return bIsWow64;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.