45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 电脑教程 > 阅读资讯:如何获取程序本身的版本号?

如何获取程序本身的版本号?

2016-08-26 04:18:01 来源:www.45fan.com 【

如何获取程序本身的版本号?

Hello,guys!

During your programing , you could meet how to get your application's version sometimes,now I will tell you how to get the application's versionfrom the VS_VERSION_INFO resource.

First ,you should make a project named GetVersion from MFC Appwizard(exe) depends on dialog!

Next, we create a button control named IDC_BTN_GETVERSION, it uses to get the application function.In this button, we add the function named GetApplicationVersion(), it uses to get the application's version!

Before Add the function , we should add a library named Version.lib, code just like:

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

Now , we Add the function named GetApplicationVersion(), and no return value,just void prefix.

This function's code ,as follows :

TCHAR szFullPath[MAX_PATH];
DWORD dwVerInfoSize = 0;
DWORD dwVerHnd;
VS_FIXEDFILEINFO * pFileInfo;

GetModuleFileName(NULL, szFullPath, sizeof(szFullPath));
dwVerInfoSize = GetFileVersionInfoSize(szFullPath, &dwVerHnd);
if (dwVerInfoSize)
{
// If we were able to get the information, process it:
HANDLE hMem;
LPVOID lpvMem;
unsigned int uInfoSize= 0;

hMem = GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
lpvMem = GlobalLock(hMem);
GetFileVersionInfo(szFullPath, dwVerHnd, dwVerInfoSize, lpvMem);

::VerQueryValue(lpvMem, (LPTSTR)_T("//"), (void**)&pFileInfo, &uInfoSize);

WORD m_nProdVersion[4];

// Product version from the FILEVERSION of the version info resource
m_nProdVersion[0] = HIWORD(pFileInfo->dwProductVersionMS);
m_nProdVersion[1] = LOWORD(pFileInfo->dwProductVersionMS);
m_nProdVersion[2] = HIWORD(pFileInfo->dwProductVersionLS);
m_nProdVersion[3] = LOWORD(pFileInfo->dwProductVersionLS);

CString strVersion ;
strVersion.Format(_T("The file's version : %d.%d.%d.%d"),m_nProdVersion[0],
m_nProdVersion[1],m_nProdVersion[2],m_nProdVersion[3]);

GlobalUnlock(hMem);
GlobalFree(hMem);

AfxMessageBox(strVersion);

}

It's very easy,do not you think ?

compile:

VC6.0 + VSP6 + WINXP!


本文地址:http://www.45fan.com/dnjc/67664.html
Tags: 获取 程序 本身
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部