MFC获取文件版本信息

.h头文件

CString GetProductVersion(LPCTSTR   lpszFileName = NULL)

cpp文件头部加入version.lib

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

功能代码


/*=================================================================
功能:获取文件版本信息.  
说明:要使用此函数必需在程序中加入  

  ----------------------------------------------------------------------------  
参数:lpszFileName=程序文件名,如果为空则是获取当前运行程序的版本信息
Reference From : http://topic.csdn.net/t/20050204/10/3774845.html 
==========================================================================*/  
CString CGetAppVersionApp::GetProductVersion(LPCTSTR   lpszFileName)
{
 
	CString strVersion;
	TCHAR   szSrcfn[MAX_PATH];
	if (lpszFileName == NULL)
	{
		if (!::GetModuleFileName(NULL, szSrcfn, sizeof(szSrcfn)))
		{
			printf("Function <GetModuleFileName> unsuccessful!, ErrorCode:%d\n", ::GetLastError());
			return _T("Error");
		}
	}
	else
	{
		lstrcpy(szSrcfn, lpszFileName);
	}
 
	if (!CFileFind().FindFile(lpszFileName))
	{
		printf("%s is not Exist!\n", lpszFileName);
		return _T("Error");
	}
 
	BOOL bRet;
	DWORD   dwVerHnd = 0;
	DWORD   dwVerInfoSize = ::GetFileVersionInfoSize(szSrcfn, &dwVerHnd);
	if (!dwVerInfoSize)
	{
		printf("Function <GetFileVersionInfo> unsuccessful!, ErrorCode:%d\n", ::GetLastError());
		return _T("Error");
	}
 
	HANDLE     hMem;
	LPVOID     lpvMem;
	unsigned int uInfoSize = 0;
	VS_FIXEDFILEINFO * pFileInfo;
 
	hMem = ::GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
	lpvMem = ::GlobalLock(hMem);
	bRet = ::GetFileVersionInfo(szSrcfn, dwVerHnd, dwVerInfoSize, lpvMem);
	if (!bRet)
	{
		printf("Function <GetFileVersionInfo> unsuccessful!, ErrorCode:%d\n", ::GetLastError());
		return _T("Error");
	}
 
	bRet = ::VerQueryValue(lpvMem, (LPTSTR)_T("\\"), (LPVOID*)&pFileInfo, &uInfoSize);
	if (!bRet)
	{
		printf("Function <VerQueryValue> unsuccessful!\n");
		return _T("Error");
	}
 
	WORD nVer[4];
	nVer[0] = HIWORD(pFileInfo->dwProductVersionMS);
	nVer[1] = LOWORD(pFileInfo->dwProductVersionMS);
	nVer[2] = HIWORD(pFileInfo->dwProductVersionLS);
	nVer[3] = LOWORD(pFileInfo->dwProductVersionLS);
	strVersion.Format(_T("%d.%d.%d.%d "), nVer[0], nVer[1], nVer[2], nVer[3]);
 
	::GlobalUnlock(hMem);
	::GlobalFree(hMem);
 
	return strVersion;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值