C++判断是否安装.Net Framework

来源:互联网 发布:网络语很迷是什么意思 编辑:程序博客网 时间:2024/05/29 19:57

通过C++判断本机器是否安装.Net Framework进行下步处理,

相关代码如下:

 

  1. #include "stdio.h"
  2. #include "windows.h"
  3. #include "tchar.h"
  4. #include "strsafe.h"
  5. #include "stdafx.h"
  6. // 为避免机器编译时候出现:SDK中某些值没有被定义的情况,先定义他们。
  7. #ifndef SM_TABLETPC
  8.     #define SM_TABLETPC     86
  9. #endif
  10. #ifndef SM_MEDIACENTER
  11.     #define SM_MEDIACENTER  87
  12. #endif
  13. // 用于检测注册表项的名称和值名称的常量
  14. const TCHAR *g_szNetfx10RegKeyName = _T("Software//Microsoft//.NETFramework//Policy//v1.0");
  15. const TCHAR *g_szNetfx10RegKeyValue = _T("3705");
  16. const TCHAR *g_szNetfx10SPxMSIRegKeyName = _T("Software//Microsoft//Active Setup//Installed Components//{78705f0d-e8db-4b2d-8193-982bdda15ecd}");
  17. const TCHAR *g_szNetfx10SPxOCMRegKeyName = _T("Software//Microsoft//Active Setup//Installed Components//{FDC11A6F-17D1-48f9-9EA3-9051954BAA24}");
  18. const TCHAR *g_szNetfx11RegKeyName = _T("Software//Microsoft//NET Framework Setup//NDP//v1.1.4322");
  19. const TCHAR *g_szNetfx20RegKeyName = _T("Software//Microsoft//NET Framework Setup//NDP//v2.0.50727");
  20. const TCHAR *g_szNetfx30RegKeyName = _T("Software//Microsoft//NET Framework Setup//NDP//v3.0//Setup");
  21. const TCHAR *g_szNetfx30SpRegKeyName = _T("Software//Microsoft//NET Framework Setup//NDP//v3.0");
  22. const TCHAR *g_szNetfx30RegValueName = _T("InstallSuccess");
  23. const TCHAR *g_szNetfx35RegKeyName = _T("Software//Microsoft//NET Framework Setup//NDP//v3.5");
  24. const TCHAR *g_szNetfxStandardRegValueName = _T("Install");
  25. const TCHAR *g_szNetfxStandardSPxRegValueName = _T("SP");
  26. const TCHAR *g_szNetfxStandardVersionRegValueName = _T("Version");
  27. //  .NET Framework 3.0最终版本的版本信息
  28. const int g_iNetfx30VersionMajor = 3;
  29. const int g_iNetfx30VersionMinor = 0;
  30. const int g_iNetfx30VersionBuild = 4506;
  31. const int g_iNetfx30VersionRevision = 26;
  32. // .NET Framework 3.5最终版本的版本信息
  33. const int g_iNetfx35VersionMajor = 3;
  34. const int g_iNetfx35VersionMinor = 5;
  35. const int g_iNetfx35VersionBuild = 21022;
  36. const int g_iNetfx35VersionRevision = 8;
  37. // 函数原型声明
  38. bool CheckNetfxBuildNumber(const TCHAR*, const TCHAR*, const intconst intconst intconst int);
  39. int GetNetfx10SPLevel();
  40. int GetNetfxSPLevel(const TCHAR*, const TCHAR*);
  41. bool IsCurrentOSTabletMedCenter();
  42. bool IsNetfx10Installed();
  43. bool IsNetfx11Installed();
  44. bool IsNetfx20Installed();
  45. bool IsNetfx30Installed();
  46. bool IsNetfx35Installed();
  47. bool RegistryGetValue(HKEYconst TCHAR*, const TCHAR*, DWORDLPBYTEDWORD);
  48. /******************************************************************
  49. Function Name:  判断.NET Framework 1.0是否安装
  50. Description:    Uses the detection method recommended at
  51.                 http://msdn.microsoft.com/library/ms994349.aspx
  52.                 to determine whether the .NET Framework 1.0 is
  53.                 installed on the machine
  54. Inputs:         NONE
  55. Results:        .NET Framework 1.0已安装返回TRUE否则返回FALSE
  56. ******************************************************************/
  57. bool IsNetfx10Installed()
  58. {
  59.     TCHAR szRegValue[MAX_PATH];
  60.     return (RegistryGetValue(HKEY_LOCAL_MACHINE, g_szNetfx10RegKeyName, g_szNetfx10RegKeyValue, NULL, (LPBYTE)szRegValue, MAX_PATH));
  61. }
  62. /******************************************************************
  63. Function Name:  判断.NET Framework 1.1是否安装
  64. Description:    Uses the detection method recommended at
  65.                 http://msdn.microsoft.com/library/ms994339.aspx
  66.                 to determine whether the .NET Framework 1.1 is
  67.                 installed on the machine
  68. Inputs:         NONE
  69. Results:        .NET Framework 1.1已安装返回TRUE否则返回FALSE
  70. ******************************************************************/
  71. bool IsNetfx11Installed()
  72. {
  73.     bool bRetValue = false;
  74.     DWORD dwRegValue=0;
  75.     if (RegistryGetValue(HKEY_LOCAL_MACHINE, g_szNetfx11RegKeyName, g_szNetfxStandardRegValueName, NULL, (LPBYTE)&dwRegValue, sizeof(DWORD)))
  76.     {
  77.         if (1 == dwRegValue)
  78.             bRetValue = true;
  79.     }
  80.     return bRetValue;
  81. }
  82. /******************************************************************
  83. Function Name:  判断.NET Framework 2.0是否安装
  84. Description:    Uses the detection method recommended at
  85.                 http://msdn.microsoft.com/library/aa480243.aspx
  86.                 to determine whether the .NET Framework 2.0 is
  87.                 installed on the machine
  88. Inputs:         NONE
  89. Results:        .NET Framework 2.0已安装返回TRUE否则返回FALSE
  90. ******************************************************************/
  91. bool IsNetfx20Installed()
  92. {
  93.     bool bRetValue = false;
  94.     DWORD dwRegValue=0;
  95.     if (RegistryGetValue(HKEY_LOCAL_MACHINE, g_szNetfx20RegKeyName, g_szNetfxStandardRegValueName, NULL, (LPBYTE)&dwRegValue, sizeof(DWORD)))
  96.     {
  97.         if (1 == dwRegValue)
  98.             bRetValue = true;
  99.     }
  100.     return bRetValue;
  101. }
  102. /******************************************************************
  103. Function Name:  判断.NET Framework 3.0是否安装
  104. Description:    Uses the detection method recommended at
  105.                 http://msdn.microsoft.com/library/aa964979.aspx
  106.                 to determine whether the .NET Framework 3.0 is
  107.                 installed on the machine
  108. Inputs:         NONE
  109. Results:        .NET Framework 3.0已安装返回TRUE否则返回FALSE
  110. ******************************************************************/
  111. bool IsNetfx30Installed()
  112. {
  113.     bool bRetValue = false;
  114.     DWORD dwRegValue=0;
  115.     // 检查InstallSuccess注册表值存在,等于1
  116.     if (RegistryGetValue(HKEY_LOCAL_MACHINE, g_szNetfx30RegKeyName, g_szNetfx30RegValueName, NULL, (LPBYTE)&dwRegValue, sizeof(DWORD)))
  117.     {
  118.         if (1 == dwRegValue)
  119.             bRetValue = true;
  120.     }
  121.     //补充核查,检查版本列出的版本号在注册表中,是否已有预发布版的 .NET Framework 3.0 InstallSuccess值。
  122.     return (bRetValue && CheckNetfxBuildNumber(g_szNetfx30RegKeyName, g_szNetfxStandardVersionRegValueName, g_iNetfx30VersionMajor, g_iNetfx30VersionMinor, g_iNetfx30VersionBuild, g_iNetfx30VersionRevision));
  123. }
  124. /******************************************************************
  125. Function Name:  判断.NET Framework 3.5是否安装
  126. Description:    Uses the detection method recommended at
  127.                 http://msdn.microsoft.com/library/cc160716.aspx
  128.                 to determine whether the .NET Framework 3.5 is
  129.                 installed on the machine
  130. Inputs:         NONE
  131. Results:        .NET Framework 3.5已安装返回TRUE否则返回FALSE
  132. ******************************************************************/
  133. bool IsNetfx35Installed()
  134. {
  135.     bool bRetValue = false;
  136.     DWORD dwRegValue=0;
  137.     // 检查安装的注册表值存在,等于1
  138.     if (RegistryGetValue(HKEY_LOCAL_MACHINE, g_szNetfx35RegKeyName, g_szNetfxStandardRegValueName, NULL, (LPBYTE)&dwRegValue, sizeof(DWORD)))
  139.     {
  140.         if (1 == dwRegValue)
  141.             bRetValue = true;
  142.     }
  143.     // 补充核查,检查版本列出的版本号在注册表中,是否已有预发布版的 .NET Framework 3.5 InstallSuccess值。
  144.     return (bRetValue && CheckNetfxBuildNumber(g_szNetfx35RegKeyName, g_szNetfxStandardVersionRegValueName, g_iNetfx35VersionMajor, g_iNetfx35VersionMinor, g_iNetfx35VersionBuild, g_iNetfx35VersionRevision));
  145. }
  146. /******************************************************************
  147. Function Name:  获取.NET Framework 1.0 SP 的版本
  148. Description:    Uses the detection method recommended at
  149.                 http://blogs.msdn.com/astebner/archive/2004/09/14/229802.aspx
  150.                 to determine what service pack for the 
  151.                 .NET Framework 1.0 is installed on the machine
  152. Inputs:         NONE
  153. Results:        integer representing SP level for .NET Framework 1.0
  154. ******************************************************************/
  155. int GetNetfx10SPLevel()
  156. {
  157.     TCHAR szRegValue[MAX_PATH];
  158.     TCHAR *pszSPLevel = NULL;
  159.     int iRetValue = -1;
  160.     bool bRegistryRetVal = false;
  161.     //需要检测操作系统上注册表项SP的版本
  162.     if (IsCurrentOSTabletMedCenter())
  163.         bRegistryRetVal = RegistryGetValue(HKEY_LOCAL_MACHINE, g_szNetfx10SPxOCMRegKeyName, g_szNetfxStandardVersionRegValueName, NULL, (LPBYTE)szRegValue, MAX_PATH);
  164.     else
  165.         bRegistryRetVal = RegistryGetValue(HKEY_LOCAL_MACHINE, g_szNetfx10SPxMSIRegKeyName, g_szNetfxStandardVersionRegValueName, NULL, (LPBYTE)szRegValue, MAX_PATH);
  166.     if (bRegistryRetVal)
  167.     {
  168.         // 格式化SP版本号: #,#,#####,# 
  169.         pszSPLevel = _tcsrchr(szRegValue, _T(','));
  170.         if (NULL != pszSPLevel)
  171.         {
  172.             // 增量指针跳过逗号
  173.             pszSPLevel++;
  174.             // 转换值为整数
  175.             iRetValue = _tstoi(pszSPLevel);
  176.         }
  177.     }
  178.     return iRetValue;
  179. }
  180. /******************************************************************
  181. Function Name:  获取.NET Framework SP 的版本
  182. Description:    确定哪些已安装Service Pack的版本。 NET框架使用基于注册表检测方法的记载。 NET Framework的部署指南。
  183. Inputs:         pszNetfxRegKeyName - registry key name to use for detection
  184.                 pszNetfxRegValueName - registry value to use for detection
  185. Results:        integer representing SP level for .NET Framework
  186. ******************************************************************/
  187. int GetNetfxSPLevel(const TCHAR *pszNetfxRegKeyName, const TCHAR *pszNetfxRegValueName)
  188. {
  189.     DWORD dwRegValue=0;
  190.     if (RegistryGetValue(HKEY_LOCAL_MACHINE, pszNetfxRegKeyName, pszNetfxRegValueName, NULL, (LPBYTE)&dwRegValue, sizeof(DWORD)))
  191.     {
  192.         return (int)dwRegValue;
  193.     }
  194.     // 从注册表检索 .NET框架未安装或有某种错误的数据
  195.     return -1;
  196. }
  197. /******************************************************************
  198. Function Name:  获取.NET Framework 编译版本
  199. Description:    从注册表检索 .NET Framework 的版本号,验证这不是一个预发布版本号
  200. Inputs:         NONE
  201. Results:        true if the build number in the registry is greater
  202.                 than or equal to the passed in version; false otherwise
  203. ******************************************************************/
  204. bool CheckNetfxBuildNumber(const TCHAR *pszNetfxRegKeyName, const TCHAR *pszNetfxRegKeyValue, const int iRequestedVersionMajor, const int iRequestedVersionMinor, const int iRequestedVersionBuild, const int iRequestedVersionRevision)
  205. {
  206.     TCHAR szRegValue[MAX_PATH];
  207.     TCHAR *pszToken = NULL;
  208.     TCHAR *pszNextToken = NULL;
  209.     int iVersionPartCounter = 0;
  210.     int iRegistryVersionMajor = 0;
  211.     int iRegistryVersionMinor = 0;
  212.     int iRegistryVersionBuild = 0;
  213.     int iRegistryVersionRevision = 0;
  214.     bool bRegistryRetVal = false;
  215.     // 尝试建立一些注册表值
  216.     bRegistryRetVal = RegistryGetValue(HKEY_LOCAL_MACHINE, pszNetfxRegKeyName, pszNetfxRegKeyValue, NULL, (LPBYTE)szRegValue, MAX_PATH);
  217.     if (bRegistryRetVal)
  218.     {
  219.         // 此注册表值应的格式#.#.#####.##.尝试解析4部分版本浏览
  220.         pszToken = _tcstok_s(szRegValue, _T("."), &pszNextToken);
  221.         while (NULL != pszToken)
  222.         {
  223.             iVersionPartCounter++;
  224.             switch (iVersionPartCounter)
  225.             {
  226.             case 1:
  227.                 // 转换主要版本为整数
  228.                 
  229.                 iRegistryVersionMajor = _tstoi(pszToken);
  230.                 break;
  231.             case 2:
  232.                 // 转换次要版本值为整数
  233.                 iRegistryVersionMinor = _tstoi(pszToken);
  234.                 break;
  235.             case 3:
  236.                 // 转换编译版本值为整数
  237.                 iRegistryVersionBuild = _tstoi(pszToken);
  238.                 break;
  239.             case 4:
  240.                 // 转换版本号值为整数
  241.                 iRegistryVersionRevision = _tstoi(pszToken);
  242.                 break;
  243.             default:
  244.                 break;
  245.             }
  246.             // 获取其它部分的版本号
  247.             pszToken = _tcstok_s(NULL, _T("."), &pszNextToken);
  248.         }
  249.     }
  250.     // Compare the version number retrieved from the registry with
  251.     // the version number of the final release of the .NET Framework 3.0
  252.     //从注册表中检索最后发布的 .NET Framework 3.0 的版本号码,比较版本号码
  253.     if (iRegistryVersionMajor > iRequestedVersionMajor)
  254.     {
  255.         return true;
  256.     }
  257.     else if (iRegistryVersionMajor == iRequestedVersionMajor)
  258.     {
  259.         if (iRegistryVersionMinor > iRequestedVersionMinor)
  260.         {
  261.             return true;
  262.         }
  263.         else if (iRegistryVersionMinor == iRequestedVersionMinor)
  264.         {
  265.             if (iRegistryVersionBuild > iRequestedVersionBuild)
  266.             {
  267.                 return true;
  268.             }
  269.             else if (iRegistryVersionBuild == iRequestedVersionBuild)
  270.             {
  271.                 if (iRegistryVersionRevision >= iRequestedVersionRevision)
  272.                 {
  273.                     return true;
  274.                 }
  275.             }
  276.         }
  277.     }
  278.     // If we get here, the version in the registry must be less than the
  279.     // version of the final release of the .NET Framework we are checking,
  280.     // so return false
  281.     return false;
  282. }
  283. bool IsCurrentOSTabletMedCenter()
  284. {
  285.     // Use GetSystemMetrics to detect if we are on a Tablet PC or Media Center OS  
  286.     return ( (GetSystemMetrics(SM_TABLETPC) != 0) || (GetSystemMetrics(SM_MEDIACENTER) != 0) );
  287. }
  288. /******************************************************************
  289. Function Name:  RegistryGetValue
  290. Description:    Get the value of a reg key
  291. Inputs:         HKEY hk - The hk of the key to retrieve
  292.                 TCHAR *pszKey - Name of the key to retrieve
  293.                 TCHAR *pszValue - The value that will be retrieved
  294.                 DWORD dwType - The type of the value that will be retrieved
  295.                 LPBYTE data - A buffer to save the retrieved data
  296.                 DWORD dwSize - The size of the data retrieved
  297. Results:        true if successful, false otherwise
  298. ******************************************************************/
  299. bool RegistryGetValue(HKEY hk, const TCHAR * pszKey, const TCHAR * pszValue, DWORD dwType, LPBYTE data, DWORD dwSize)
  300. {
  301.     HKEY hkOpened;
  302.     // Try to open the key
  303.     if (RegOpenKeyEx(hk, pszKey, 0, KEY_READ, &hkOpened) != ERROR_SUCCESS)
  304.     {
  305.         return false;
  306.     }
  307.     // If the key was opened, try to retrieve the value
  308.     if (RegQueryValueEx(hkOpened, pszValue, 0, &dwType, (LPBYTE)data, &dwSize) != ERROR_SUCCESS)
  309.     {
  310.         RegCloseKey(hkOpened);
  311.         return false;
  312.     }
  313.     
  314.     // Clean up
  315.     RegCloseKey(hkOpened);
  316.     return true;
  317. }
  318. int APIENTRY _tWinMain(HINSTANCE hInstance,
  319.                      HINSTANCE hPrevInstance,
  320.                      LPTSTR    lpCmdLine,
  321.                      int       nCmdShow)
  322. {
  323.     int iNetfx10SPLevel = -1;
  324.     int iNetfx11SPLevel = -1;
  325.     int iNetfx20SPLevel = -1;
  326.     int iNetfx30SPLevel = -1;
  327.     int iNetfx35SPLevel = -1;
  328.     TCHAR szMessage[MAX_PATH];
  329.     // Determine whether or not the .NET Framework
  330.     // 1.0, 1.1, 2.0 or 3.0 are installed
  331.     bool bNetfx10Installed = IsNetfx10Installed();
  332.     bool bNetfx11Installed = IsNetfx11Installed();
  333.     bool bNetfx20Installed = IsNetfx20Installed();
  334.     // The .NET Framework 3.0 is an add-in that installs
  335.     // on top of the .NET Framework 2.0.  For this version
  336.     // check, validate that both 2.0 and 3.0 are installed.
  337.     bool bNetfx30Installed = (IsNetfx20Installed() && IsNetfx30Installed());
  338.     // The .NET Framework 3.5 is an add-in that installs
  339.     // on top of the .NET Framework 2.0 and 3.0.  For this version
  340.     // check, validate that 2.0, 3.0 and 3.5 are installed.
  341.     bool bNetfx35Installed = (IsNetfx20Installed() && IsNetfx30Installed() && IsNetfx35Installed());
  342.     // If .NET Framework 1.0 is installed, get the
  343.     // service pack level
  344.     if (bNetfx10Installed)
  345.     {
  346.         iNetfx10SPLevel = GetNetfx10SPLevel();
  347.         if (iNetfx10SPLevel > 0)
  348.             _stprintf_s(szMessage, MAX_PATH, _T(".NET Framework 1.0 service pack %i is installed."), iNetfx10SPLevel);
  349.         else
  350.             _stprintf_s(szMessage, MAX_PATH, _T(".NET Framework 1.0 is installed with no service packs."));
  351.         MessageBox(NULL, szMessage, _T(".NET Framework 1.0"), MB_OK | MB_ICONINFORMATION);
  352.     }
  353.     else
  354.     {
  355.         MessageBox(NULL, _T(".NET Framework 1.0 is not installed."), _T(".NET Framework 1.0"), MB_OK | MB_ICONINFORMATION);
  356.     }
  357.     // If .NET Framework 1.1 is installed, get the
  358.     // service pack level
  359.     if (bNetfx11Installed)
  360.     {
  361.         iNetfx11SPLevel = GetNetfxSPLevel(g_szNetfx11RegKeyName, g_szNetfxStandardSPxRegValueName);
  362.         if (iNetfx11SPLevel > 0)
  363.             _stprintf_s(szMessage, MAX_PATH, _T(".NET Framework 1.1 service pack %i is installed."), iNetfx11SPLevel);
  364.         else
  365.             _stprintf_s(szMessage, MAX_PATH, _T(".NET Framework 1.1 is installed with no service packs."));
  366.         MessageBox(NULL, szMessage, _T(".NET Framework 1.1"), MB_OK | MB_ICONINFORMATION);
  367.     }
  368.     else
  369.     {
  370.         MessageBox(NULL, _T(".NET Framework 1.1 is not installed."), _T(".NET Framework 1.1"), MB_OK | MB_ICONINFORMATION);
  371.     }
  372.     // If .NET Framework 2.0 is installed, get the
  373.     // service pack level
  374.     if (bNetfx20Installed)
  375.     {
  376.         iNetfx20SPLevel = GetNetfxSPLevel(g_szNetfx20RegKeyName, g_szNetfxStandardSPxRegValueName);
  377.         if (iNetfx20SPLevel > 0)
  378.             _stprintf_s(szMessage, MAX_PATH, _T(".NET Framework 2.0 service pack %i is installed."), iNetfx20SPLevel);
  379.         else
  380.             _stprintf_s(szMessage, MAX_PATH, _T(".NET Framework 2.0 is installed with no service packs."));
  381.         MessageBox(NULL, szMessage, _T(".NET Framework 2.0"), MB_OK | MB_ICONINFORMATION);
  382.     }
  383.     else
  384.     {
  385.         MessageBox(NULL, _T(".NET Framework 2.0 is not installed."), _T(".NET Framework 2.0"), MB_OK | MB_ICONINFORMATION);
  386.     }
  387.     // If .NET Framework 3.0 is installed, get the
  388.     // service pack level
  389.     if (bNetfx30Installed)
  390.     {
  391.         iNetfx30SPLevel = GetNetfxSPLevel(g_szNetfx30SpRegKeyName, g_szNetfxStandardSPxRegValueName);
  392.         if (iNetfx30SPLevel > 0)
  393.             _stprintf_s(szMessage, MAX_PATH, _T(".NET Framework 3.0 service pack %i is installed."), iNetfx30SPLevel);
  394.         else
  395.             _stprintf_s(szMessage, MAX_PATH, _T(".NET Framework 3.0 is installed with no service packs."));
  396.         MessageBox(NULL, szMessage, _T(".NET Framework 3.0"), MB_OK | MB_ICONINFORMATION);
  397.     }
  398.     else
  399.     {
  400.         MessageBox(NULL, _T(".NET Framework 3.0 is not installed."), _T(".NET Framework 3.0"), MB_OK | MB_ICONINFORMATION);
  401.     }
  402.     // If .NET Framework 3.5 is installed, get the
  403.     // service pack level
  404.     if (bNetfx35Installed)
  405.     {
  406.         iNetfx35SPLevel = GetNetfxSPLevel(g_szNetfx35RegKeyName, g_szNetfxStandardSPxRegValueName);
  407.         if (iNetfx35SPLevel > 0)
  408.             _stprintf_s(szMessage, MAX_PATH, _T(".NET Framework 3.5 service pack %i is installed."), iNetfx35SPLevel);
  409.         else
  410.             _stprintf_s(szMessage, MAX_PATH, _T(".NET Framework 3.5 is installed with no service packs."));
  411.         MessageBox(NULL, szMessage, _T(".NET Framework 3.5"), MB_OK | MB_ICONINFORMATION);
  412.     }
  413.     else
  414.     {
  415.         MessageBox(NULL, _T(".NET Framework 3.5 is not installed."), _T(".NET Framework 3.5"), MB_OK | MB_ICONINFORMATION);
  416.     }
  417.     return 0;
  418. }
原创粉丝点击