在Windows Mobile和Wince(Windows Embedded CE)下开发的产品,有时候需要显示当前产品的版本信息。一般来说,版本信息是保存在资源文件里面的,例如下图:
为了保持一致,所有版本信息应该都从资源文件读取,不应该另外硬编码(Hard code)。
下面讲述读取资源文件版本信息的方法:
1.在资源文件中新建一个版本信息项
2.根据需求修改版本信息
3.增加取版本信息函数
要加入version。lib的链接库
CString GetProductVersion() { int nMaxPathName = 4096; // Max length of file name/path char *pBuffer; UINT nItemLength; void* pData, *lpBuffer; CString sVersion; DWORD dwInfoSize, dwHandle; VS_FIXEDFILEINFO *pFileInfo; // Get the file path and name pBuffer = new char[nMaxPathName]; GetModuleFileName(NULL, pBuffer, nMaxPathName-1); // Get File Version Info size dwInfoSize = GetFileVersionInfoSize(pBuffer, &dwHandle); if(dwInfoSize > 0) { pData = new char[dwInfoSize]; if(GetFileVersionInfo(pBuffer, dwHandle, dwInfoSize, pData)) if(VerQueryValue(pData, "\", &lpBuffer, &nItemLength)) { pFileInfo = (VS_FIXEDFILEINFO*)lpBuffer; sVersion.Format("%d.%d.%d.%d", pFileInfo->dwProductVersionMS >> 16, pFileInfo->dwProductVersionMS & 0xFFFF, pFileInfo->dwProductVersionLS >> 16, pFileInfo->dwProductVersionLS & 0xFFFF); // Calculate the product version as a number, you can delete the next statement if you don't need it. DWORD dwProductVersion = (pFileInfo->dwProductVersionMS >> 16) * 1000 + (pFileInfo->dwProductVersionMS & 0xFFFF) * 100 + (pFileInfo->dwProductVersionLS >> 16) * 10 + (pFileInfo->dwProductVersionLS & 0xFFFF) * 1; } // Delete the data buffer delete [] pData; } // Get rid of the allocated string buffer delete [] pBuffer; return sVersion; }
4.用法
/* 用法 */ CString sProductVersion = GetProductVersion();
完成了,效果如下图: