首页 > 代码库 > C++判断Office版本——转载

C++判断Office版本——转载

自:http://blog.csdn.net/lpc_china/article/details/18359145

 

 

主要原理:查询windows注册表microsoft office软件项的值来判断版本。

 

主要源码:

头文件:

 1 #pragma once 2 #include <Windows.h> 3 #include <tchar.h> 4  5 class CJudgeOfficeVersion  6 { 7 public: 8     CJudgeOfficeVersion(); 9     ~CJudgeOfficeVersion();10 11 public:12     BOOL JudgeVersion(OUT LPTSTR _lpVersion, IN DWORD _dwVersionBufferLen);13 };

源码:

  1 #include "JudgeOfficeVersion.h"  2 #include <strsafe.h>  3   4 /*  5  * 函数名称:CJudgeOfficeVersion  6  * 函数功能:构造函数  7  * 函数参数:无  8  * 函数返回:无  9  * 函数备注:无 10  * 编 写 人:刘鹏春 11  */ 12 CJudgeOfficeVersion::CJudgeOfficeVersion() 13 { 14  15 } 16  17 /* 18  * 函数名称:~CJudgeOfficeVersion 19  * 函数功能:析构函数 20  * 函数参数:无 21  * 函数返回:无 22  * 函数备注:无 23  * 编 写 人:刘鹏春 24  */ 25 CJudgeOfficeVersion::~CJudgeOfficeVersion() 26 { 27  28 } 29  30 /* 31  * 函数名称:JudgeVersion 32  * 函数功能:判断版本 33  * 函数参数:1字符指针;2指针长度; 34  * 函数返回:判断状态 35  * 函数备注:通过ProgID查找CLSID查询服务器中记录的Office版本信息 36  *           该代码源自:http://support.microsoft.com/kb/247985/zh-cn 37  * 编 写 人:刘鹏春 38  */ 39 BOOL CJudgeOfficeVersion::JudgeVersion(OUT LPTSTR _lpVersion, IN DWORD _dwVersionBufferLen) 40 { 41     HKEY hKey; 42     HKEY hSubKey; 43     LONG lResult = 0L; 44  45     TCHAR szValueName[128] = {_T("CurVer")}; 46     TCHAR szKey[128] = {_T("Excel.Application")}; 47  48     lResult = RegOpenKeyEx( 49         HKEY_CLASSES_ROOT,  50         szKey,  51         0,  52         KEY_ALL_ACCESS,  53         &hKey 54         ); 55     if (ERROR_SUCCESS != lResult) { 56         MessageBox(NULL, _T("Could not get CLSID from ProgID, Make sure ProgID is correct."), _T("提示"), MB_OK); 57         return FALSE; 58     } 59  60     lResult = RegOpenKeyEx( 61         hKey,  62         szValueName,  63         0,  64         KEY_ALL_ACCESS,  65         &hSubKey 66         ); 67     if (ERROR_SUCCESS != lResult) { 68         MessageBox(NULL, _T("Excel is registered, but no local server can be found!"), _T("提示"), MB_OK); 69         return FALSE; 70     } 71  72     lResult = RegQueryValueEx(hSubKey, NULL, NULL, NULL, (LPBYTE)_lpVersion, &_dwVersionBufferLen); 73  74     RegCloseKey(hSubKey); 75     RegCloseKey(hKey); 76  77     if (ERROR_SUCCESS != lResult) { 78         return FALSE; 79     } 80  81     PTCHAR pszVersionNumber = _tcsrchr(_lpVersion, _T(.)); 82     PTCHAR pVersion = (pszVersionNumber + 1); 83     INT nVersion = _ttoi(pVersion); 84  85     ZeroMemory(_lpVersion, _dwVersionBufferLen); 86     switch (nVersion)  87     { 88     case 0: 89     case 1: 90     case 2: 91     case 3: 92     case 5: 93         StringCchCopy(_lpVersion, _dwVersionBufferLen, _T("Office 95以前版本")); 94         break; 95     case 6: 96         StringCchCopy(_lpVersion, _dwVersionBufferLen, _T("Office 95")); 97         break; 98     case 8: 99         StringCchCopy(_lpVersion, _dwVersionBufferLen, _T("Office 97"));100         break;101     case 9:102         StringCchCopy(_lpVersion, _dwVersionBufferLen, _T("Office 2000"));103         break;104     case 10:105         StringCchCopy(_lpVersion, _dwVersionBufferLen, _T("Office XP"));106         break;107     case 11:108         StringCchCopy(_lpVersion, _dwVersionBufferLen, _T("Office 2003"));109         break;110     case 12:111         StringCchCopy(_lpVersion, _dwVersionBufferLen, _T("Office 2007"));112         break;113     case 13:114     case 14:115     case 15:116         StringCchCopy(_lpVersion, _dwVersionBufferLen, _T("Office 2010"));117         break;118     default:119         StringCchCopy(_lpVersion, _dwVersionBufferLen, _T("Version 2010以后版本"));120     }121 122     return TRUE;123 }

 

*注:此方法还是比较简答而且容易是实现的。

 

C++判断Office版本——转载