I need to get the product version and file version for a dll or exe 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, selecting "Properties", then looking at the "Details" tab. This is usually a four-part dotted version number x.x.x.x.
+4
A:
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:
- C++: Determining the version number of a DLL or Executable
- VB (probably version 6): How to Use Functions in VERSION.DLL
Hope these help!
Steve J
2009-06-02 17:11:45
In practice I've found that the build is the HIWORD, and the LOWORD is the number after the last dot, which is almost always 0. Otherwise, this is correct.
JSBangs
2009-06-04 20:35:51
I generally only worry about 3 numbers for my apps, so I just grab the value. Which ends up being like A.B.0.C But if you wanted all four, then you would need to HIWORD/LOWORD the other.
crashmstr
2009-06-04 20:40:31
+3
A:
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.
Dani van der Meer
2009-06-02 17:15:10
+6
A:
[Self-answering, since it took me a lot of Googling to get the right answer to this question, and right now the top Google hits are misleading.]
You get this information using the Version Information APIs (http://msdn.microsoft.com/en-us/library/ms646981.aspx). Here is a sample:
void PrintFileVersion( TCHAR *pszFilePath )
{
DWORD dwSize = 0;
BYTE *pbVersionInfo = NULL;
VS_FIXEDFILEINFO *pFileInfo = NULL;
UINT puLenFileInfo = 0;
// get the version info 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
);
}
JSBangs
2009-06-02 17:22:40
+1 since this code this code is better (new and delete), however what worked for me for the printf was this: `printf( "File Version: %d.%d.%d.%d\n", ` `HIWORD(pFileInfo->dwFileVersionMS ), ` `LOWORD(pFileInfo->dwFileVersionMS ), ` `HIWORD( pFileInfo->dwFileVersionLS ), ` `LOWORD( pFileInfo->dwFileVersionLS ) ); `
CharlesB
2010-03-25 18:42:19