VC MFC程序,在About对话框中获取并显示程序的版本号

 

=================================================
本文为HeYuanHui原作

转载必须确保本文完整并完整保留原作者信息及本文原始链接!

NN:      khler
E-mail: khler@163.com
QQ:     23381103
MSN:   pragmac@hotmail.com
=================================================


    用VC++写的MFC程序,不管是exe的或者dll,都有个'VERSION'资源,在里面可以指定程序的版本号,这样在程序文件上右键点击,查看属性,就可以看到内嵌的版本信息了。同样,所有程序都愿意在'About'对话框中显示程序的当前版本,但是这里如何显示,跟资源里的'VERSION'信息还是有好几毛钱的关系呢 :)

    如果我们从'VERSION'资源里获取版本信息并在about对话框中显示,那么我们每次发布的时候只要修改'VERSION'里的信息就行了,经过一定处理,就可以在about对话框中显示了。

    其实MSDN中也有说明,但是说实话,要想从'VERSION'中读取信息,还真不是件简单的事情,下面一一实现,代码大部分来自MSDN。

    结果如下图所示:

显示1.1.6 build 718

 

    代码:

 

void  CAboutDlg::OnShowWindow(BOOL bShow, UINT nStatus)
{
 CDialog::OnShowWindow(bShow, nStatus);

 
//  TODO: 在此处添加消息处理程序代码


 CString ver 
=  GetAppVersion(L " TowerWatch.exe " );
 
if (ver.IsEmpty())  return ;

 
int  pos  =  ver.ReverseFind( ' . ' );
 CString mainVer 
=  ver.Left(pos);
 CString build 
=  ver.Right(ver.GetLength()  -  pos  - 1 );
 GetDlgItem(IDC_STATIC_VER)
-> SetWindowText(mainVer);
 GetDlgItem(IDC_STATIC_BUILD)
-> SetWindowText(build);

}

void  ErrorExit(LPTSTR lpszFunction) 

 
//  Retrieve the system error message for the last-error code

 LPVOID lpMsgBuf;
 LPVOID lpDisplayBuf;
 DWORD dw 
=  GetLastError(); 

 FormatMessage(
  FORMAT_MESSAGE_ALLOCATE_BUFFER 
|  
  FORMAT_MESSAGE_FROM_SYSTEM 
|
  FORMAT_MESSAGE_IGNORE_INSERTS,
  NULL,
  dw,
  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  (LPTSTR) 
& lpMsgBuf,
  
0 , NULL );

 
//  Display the error message and exit the process

 lpDisplayBuf 
=  (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
  (lstrlen((LPCTSTR)lpMsgBuf)
+ lstrlen((LPCTSTR)lpszFunction) + 40 ) * sizeof (TCHAR)); 
 StringCchPrintf((LPTSTR)lpDisplayBuf, 
  LocalSize(lpDisplayBuf),
  TEXT(
" %s failed with error %d: %s " ), 
  lpszFunction, dw, lpMsgBuf); 
 MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT(
" Error " ), MB_OK); 

 LocalFree(lpMsgBuf);
 LocalFree(lpDisplayBuf);
}

 

CString CAboutDlg::GetAppVersion(WCHAR
*  AppName) 
{
 CString   AppVersion; 

 DWORD   RessourceVersionInfoSize; 
 DWORD   JustAJunkVariabel; 
 WCHAR
*    VersionInfoPtr; 
 
struct    LANGANDCODEPAGE
 { 
  WORD   wLanguage; 
  WORD   wCodePage; 
 }   
* TranslationPtr; 
 WCHAR
*      InformationPtr; 
 UINT      VersionInfoSize; 
 WCHAR     VersionValue[
255 ]; 

 RessourceVersionInfoSize
= GetFileVersionInfoSize(AppName, & JustAJunkVariabel); 
 
if ( 0 != RessourceVersionInfoSize) 
 {
  VersionInfoPtr 
=   new  WCHAR[RessourceVersionInfoSize];
  
if ( ! GetFileVersionInfo(AppName, 0 ,RessourceVersionInfoSize,VersionInfoPtr)) 
  {
    ErrorExit((LPTSTR)L
" GetFileVersionInfo " );
   delete[]   VersionInfoPtr; 
   
return  NULL; 
  } 

  
if ( ! VerQueryValue( VersionInfoPtr, L " VarFileInfo\\Translation " , (LPVOID * ) & TranslationPtr,  & VersionInfoSize)) 
  { 
   ErrorExit((LPTSTR)L
" VerQueryValue " );
   delete[]   VersionInfoPtr; 
   
return  NULL; 
  } 

  
//  retrieve product version
  wsprintf(VersionValue, L " \\StringFileInfo\\%04x%04x\\ProductVersion " , TranslationPtr[ 0 ].wLanguage, TranslationPtr[ 0 ].wCodePage); 

  
if ( ! VerQueryValue( VersionInfoPtr, VersionValue, (LPVOID * ) & InformationPtr,  & VersionInfoSize)) 
  { 
   ErrorExit((LPTSTR)L
" VerQueryValue " );
   delete[]   VersionInfoPtr; 
   
return  NULL; 
  } 
  
if (wcslen(InformationPtr) >   0 )    // Not   Null 
  { 
   AppVersion
= CString(InformationPtr); 
  } 

  delete[]   VersionInfoPtr; 
 } 
 
return    AppVersion; 

 

当然,你可以将分解Version和build部分代码也封装到GetAppVersion函数中。

 

 

转载于:https://www.cnblogs.com/khler/archive/2011/02/25/1964789.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值