如何以编程方式获得 DLL 或 EXE 文件的版本?

我需要得到一个 DLL 或 EXE 文件的产品版本和文件版本使用 C 或 C + + 中的 Win32原生 API。我的 没有寻找 Windows 版本,但版本号码,你看到的右键点击一个 DLL 文件,选择“属性”,然后看看“详细信息”选项卡。这通常是由四部分组成的虚线版本号 x.x.x.x。

97992 次浏览

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!

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;
}

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.

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
);
}

This code shows the file version numbers correctly.

( pFileInfo->dwFileVersionMS >> 16 ) & 0xff,
( pFileInfo->dwFileVersionMS >> 0 ) & 0xff,
( pFileInfo->dwFileVersionLS >>  16 ) & 0xff,
( pFileInfo->dwFileVersionLS >>  0 ) & 0xff);

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:

major.minor.build.revision