VC MFC程序,在About对话框中获取并显示程序的版本号
2011-02-25 13:44
1451 查看
=================================================
本文为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函数中。
本文为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函数中。
相关文章推荐
- (他山之石)MFC学习之路(一)VC MFC程序,在About对话框中获取并显示程序的版本号
- vc 对话框程序里,如何在密码输入框显示为****,屏蔽掉密码
- VC/MFC 设置程序与文件关联与双击文件获取文件路径
- 获取VC/MFC程序的运行时间
- 给基于对话框的MFC程序添加状态栏并实时显示时间
- MFC对话框程序,去掉边框和标题栏;窗口全屏显示。
- 给基于对话框的MFC程序添加状态栏并实时显示时间
- VC++/MFC 小程序 文件/目录对话框 递归遍历文件/文件夹
- MFC对话框应用程序 如何在对话框窗口显示前获取窗口(控件)的屏幕坐标位置
- VC6 MFC 如何让用户选择颜色并显示在对话框内?
- VC设置对话框背景图片 MFC显示背景图片方法
- MFC基于对话框框架的简易飞鸽系统(二)----获取局域网内运行同样程序的主机名和IP
- VC扩展mfc dll中子对话框不能获取焦点问题
- VC++ MFC SDI/MDI Ribbon程序的停靠窗格被关闭后如何再次显示
- 孙鑫VC学习笔记:第十五讲 (四) 编写一个基于MFC对话框的聊天程序
- vs2008自动添加消息和MFC对话框程序不显示主画面
- VC/MFC 设置程序与文件关联与双击文件获取文件路径
- VC++MFC对话框应用程序实现程序最小化到系统托盘
- MSChart控件在对话框程序(vc2010 MFC)中的错误怎么解决?
- 关于“在MFC对话框程序中图片显示不出来”问题的解决方法