MFC中獲取程式自身的版本資訊

yousss發表於2018-08-29

收集整理自CSDN論壇。

首先、在Stdafx.h中引入連結庫:

#pragma comment(lib,"Version.lib")

在對應的處理方法中如下:

char FileVerInfo[4096];

DWORD dwVerLen = 0;
CString strPath;//路徑
GetModuleFileName(NULL,strPath.GetBufferSetLength(MAX_PATH + 1), MAX_PATH);
strPath.ReleaseBuffer();
int n_Len = GetFileVersionInfoSize(strPath, &dwVerLen);
if (0 != n_Len)
{
BOOL m_bFile = GetFileVersionInfo(strPath,NULL,n_Len,FileVerInfo);

UINT cbTranslate;
char SubBlock[4096];
UINT dwBytes;

struct LANGANDCODEPAGE {
WORD wLanguage;
WORD wCodePage;
} *lpTranslate;


struct version {
char * strProductName;
char * strProductVersion;
char * strLegalCopyright;
char * strCompanyName;
char * strSpecialBuild;
} ver;

VerQueryValue(FileVerInfo, 
TEXT("\\VarFileInfo\\Translation"),
(LPVOID*)&lpTranslate,
&cbTranslate);

for( UINT i=0; i < (cbTranslate/sizeof(struct LANGANDCODEPAGE)); i++ )
{
wsprintf( SubBlock, 
TEXT("\\StringFileInfo\\%04x%04x\\ProductName"),
lpTranslate[i].wLanguage,
lpTranslate[i].wCodePage);
VerQueryValue(FileVerInfo, 
SubBlock, 
(void **)&ver.strProductName, 
&dwBytes); 
wsprintf( SubBlock, 
TEXT("\\StringFileInfo\\%04x%04x\\ProductVersion"),
lpTranslate[i].wLanguage,
lpTranslate[i].wCodePage);


// Retrieve file description for language and code page "i". 
VerQueryValue(FileVerInfo, 
SubBlock, 
(void **)&ver.strProductVersion, 
&dwBytes); 
wsprintf( SubBlock, 
TEXT("\\StringFileInfo\\%04x%04x\\LegalCopyright"),
lpTranslate[i].wLanguage,
lpTranslate[i].wCodePage);


// Retrieve file description for language and code page "i". 
VerQueryValue(FileVerInfo, 
SubBlock, 
(void **)&ver.strLegalCopyright, 
&dwBytes); 
wsprintf( SubBlock, 
TEXT("\\StringFileInfo\\%04x%04x\\CompanyName"),
lpTranslate[i].wLanguage,
lpTranslate[i].wCodePage);

// Retrieve file description for language and code page "i". 
VerQueryValue(FileVerInfo, 
SubBlock, 
(void **)&ver.strCompanyName, 
&dwBytes); 
wsprintf( SubBlock, 
TEXT("\\StringFileInfo\\%04x%04x\\SpecialBuild"),
lpTranslate[i].wLanguage,
lpTranslate[i].wCodePage);

// Retrieve file description for language and code page "i". 
VerQueryValue(FileVerInfo, 
SubBlock, 
(void **)&ver.strSpecialBuild, 
&dwBytes); 

((CStatic *)GetDlgItem(IDC_STATIC_ABOUT_COPYRIGHT))->SetWindowText(ver.strLegalCopyright);
CString strTemp;
CString strVersion;
strTemp = ver.strProductName;
strVersion += strTemp;
strTemp = ver.strProductVersion;
strVersion += " " + strTemp + "版本。";
((CStatic *)GetDlgItem(IDC_STATIC_ABOUT_VERSION))->SetWindowText(strVersion);
break;
}
}

相關文章