如何在运行时检索存储在Windows exe/dll中的版本信息?使用资源文件手动设置此信息.
以下是使用标准Windows API函数的C++方法:
try { TCHAR szFileName[ MAX_PATH ]; if( !::GetModuleFileName( 0, szFileName, MAX_PATH ) ) throw __LINE__; DWORD nParam; DWORD nVersionSize = ::GetFileVersionInfoSize( szFileName, &nParam ); if( !nVersionSize ) throw __LINE__; HANDLE hMem = ::GetProcessHeap(); if( !hMem ) throw __LINE__; LPVOID lpVersionData = ::HeapAlloc( hMem, 0, nVersionSize ); if( !lpVersionData ) throw __LINE__; if( !::GetFileVersionInfo( szFileName, 0, nVersionSize, lpVersionData ) ) throw __LINE__; LPVOID pVersionInfo; UINT nSize; if( !::VerQueryValue( lpVersionData, _T("\\"), &pVersionInfo, &nSize ) ) throw __LINE__; VS_FIXEDFILEINFO *pVSInfo = (VS_FIXEDFILEINFO *)pVersionInfo; CString strVersion; strVersion.Format( _T(" version %i.%i.%i.%i"), pVSInfo->dwProductVersionMS >> 16, pVSInfo->dwProductVersionMS & 0xFFFF, pVSInfo->dwProductVersionLS >> 16, pVSInfo->dwProductVersionLS & 0xFFFF ); GetDlgItem( IDC_ABOUT_VERSION )->SetWindowText( strAppName + strVersion ); if( !HeapFree( hMem, 0, lpVersionData ) ) throw __LINE__; } catch( int err ) { ASSERT( !err ); // always break on debug builds to inspect error codes and such DWORD dwErr = ::GetLastError(); // handle memory cleanup... }
请注意,catch部分纯粹是教育性的 - 在实际情况下,您可以在内存分配后正确清理并实际使用错误代码!