功能代码:
1 /*========================================================================
2 功能:获取文件版本信息.
3 说明:要使用此函数必需在程序中加入
4 #pragma comment(lib, "Version.lib ")
5 ----------------------------------------------------------------------------
6 参数:lpszFileName=程序文件名,如果为空则是获取当前运行程序的版本信息
7 Reference From : http://topic.csdn.net/t/20050204/10/3774845.html
8 ==========================================================================*/
9 CString GetProductVersion(LPCTSTR lpszFileName)
10 {
11 CString strVersion;
12 TCHAR szSrcfn[MAX_PATH];
13 if(lpszFileName == NULL)
14 {
15 if (!::GetModuleFileName(NULL, szSrcfn, sizeof(szSrcfn)))
16 {
17 printf("Function <GetModuleFileName> unsuccessful!, ErrorCode:%d\n", ::GetLastError());
18 return "Error";
19 }
20 }
21 else
22 {
23 lstrcpy(szSrcfn, lpszFileName);
24 }
25
26 if (!CFileFind().FindFile(lpszFileName))
27 {
28 printf("%s is not Exist!\n", lpszFileName);
29 return "Error";
30 }
31
32 BOOL bRet;
33 DWORD dwVerHnd = 0;
34 DWORD dwVerInfoSize = ::GetFileVersionInfoSize(szSrcfn, &dwVerHnd);
35 if(!dwVerInfoSize)
36 {
37 printf("Function <GetFileVersionInfo> unsuccessful!, ErrorCode:%d\n", ::GetLastError());
38 return "Error";
39 }
40
41 HANDLE hMem;
42 LPVOID lpvMem;
43 unsigned int uInfoSize = 0;
44 VS_FIXEDFILEINFO * pFileInfo;
45
46 hMem = ::GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
47 lpvMem = ::GlobalLock(hMem);
48 bRet = ::GetFileVersionInfo(szSrcfn, dwVerHnd, dwVerInfoSize, lpvMem);
49 if (!bRet)
50 {
51 printf("Function <GetFileVersionInfo> unsuccessful!, ErrorCode:%d\n", ::GetLastError());
52 return "Error";
53 }
54
55 bRet = ::VerQueryValue(lpvMem, (LPTSTR)_T( "\\"), (LPVOID*)&pFileInfo, &uInfoSize);
56 if (!bRet)
57 {
58 printf("Function <VerQueryValue> unsuccessful!\n");
59 return "Error";
60 }
61
62 WORD nVer[4];
63 nVer[0] = HIWORD(pFileInfo-> dwProductVersionMS);
64 nVer[1] = LOWORD(pFileInfo-> dwProductVersionMS);
65 nVer[2] = HIWORD(pFileInfo-> dwProductVersionLS);
66 nVer[3] = LOWORD(pFileInfo-> dwProductVersionLS);
67 strVersion.Format(_T( "%d.%d.%d.%d "), nVer[0], nVer[1], nVer[2], nVer[3]);
68
69 ::GlobalUnlock(hMem);
70 ::GlobalFree(hMem);
71
72 return strVersion;
73 }
调用代码:
1 LPCTSTR sFilePath = argv[1];
2 CString sFileVersion = GetProductVersion(sFilePath);
3 if (sFileVersion.CompareNoCase("Error") == 0)
4 {
5 printf("Get File Version unsuccessful!\n");
6 }
7 else
8 {
9 printf("Version : %s\n", sFileVersion);
10 printf("Get File Version successful!\n");
11 }
posted on 2012-10-25 13:43
王海光 阅读(1201)
评论(0) 编辑 收藏 引用 所属分类:
MFC