C++ 检测.net框架

  1. #include "stdafx.h"
    #include <stdio.h>
    #include <tchar.h>
    #include <windows.h>
    
    // The computer on which this code is compiled may not have the most recent platform SDK
    // with these values defined. In this scenario, define the values.
    #ifndef SM_TABLETPC
        #define SM_TABLETPC                     86
    #endif
    
    #ifndef SM_MEDIACENTER
        #define SM_MEDIACENTER                  87
    #endif
    
    // The following constants represent registry key names and value names
    // to use for detection.
    const TCHAR *g_szNetfx10RegKeyName          = _T("Software\\Microsoft\\.NETFramework\\Policy\\v1.0");
    const TCHAR *g_szNetfx10RegKeyValue         = _T("3705");
    const TCHAR *g_szNetfx10SPxMSIRegKeyName    = _T("Software\\Microsoft\\Active Setup\\Installed Components\\{78705f0d-e8db-4b2d-8193-982bdda15ecd}");
    const TCHAR *g_szNetfx10SPxOCMRegKeyName    = _T("Software\\Microsoft\\Active Setup\\Installed Components\\{FDC11A6F-17D1-48f9-9EA3-9051954BAA24}");
    const TCHAR *g_szNetfx10SPxRegValueName     = _T("Version");
    const TCHAR *g_szNetfx11RegKeyName          = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v1.1.4322");
    const TCHAR *g_szNetfx20RegKeyName          = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v2.0.50727");
    const TCHAR *g_szNetfx11PlusRegValueName    = _T("Install");
    const TCHAR *g_szNetfx11PlusSPxRegValueName = _T("SP");
    
    // The following items are function prototypes.
    int  GetNetfx10SPLevel();
    int  GetNetfx11SPLevel();
    int  GetNetfx20SPLevel();
    bool IsCurrentOSTabletMedCenter();
    bool IsNetfx10Installed();
    bool IsNetfx11Installed();
    bool IsNetfx20Installed();
    bool RegistryGetValue(HKEY, const TCHAR*, const TCHAR*, DWORD, LPBYTE, DWORD);
    
    /******************************************************************
    Function Name:  IsNetfx10Installed
    Description:    Uses the detection method recommended at
                    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetdep/html/dotnetfxref.asp
                    to determine whether the .NET Framework 1.0 is
                    installed on the computer.
    Inputs:         NONE
    Results:        true if the .NET Framework 1.0 is installed
                    false otherwise
    ******************************************************************/
    bool IsNetfx10Installed()
    {
        TCHAR szRegValue[MAX_PATH];
        return (RegistryGetValue(HKEY_LOCAL_MACHINE, g_szNetfx10RegKeyName, g_szNetfx10RegKeyValue, NULL, (LPBYTE)szRegValue, MAX_PATH));
    }
    
    
    /******************************************************************
    Function Name:  IsNetfx11Installed
    Description:    Uses the detection method recommended at
                    http://msdn.microsoft.com/en-us/library/ms994402.aspx
                    to determine whether the .NET Framework 1.1 is
                    installed on the computer.
    Inputs:         NONE
    Results:        true if the .NET Framework 1.1 is installed
                    false otherwise
    ******************************************************************/
    bool IsNetfx11Installed()
    {
        bool bRetValue = false;
        DWORD dwRegValue=0;
    
        if (RegistryGetValue(HKEY_LOCAL_MACHINE, g_szNetfx11RegKeyName, g_szNetfx11PlusRegValueName, NULL, (LPBYTE)&dwRegValue, sizeof(DWORD)))
        {
            if (1 == dwRegValue)
                bRetValue = true;
        }
    
        return bRetValue;
    }
    
    
    /******************************************************************
    Function Name:  IsNetfx20Installed
    Description:    Uses the detection method recommended at
                    http://msdn.microsoft.com/en-us/library/ms994402.aspx
                    to determine whether the .NET Framework 2.0 is
                    installed on the computer.
    Inputs:         NONE
    Results:        true if the .NET Framework 2.0 is installed
                    false otherwise
    ******************************************************************/
    bool IsNetfx20Installed()
    {
        bool bRetValue = false;
        DWORD dwRegValue=0;
    
        if (RegistryGetValue(HKEY_LOCAL_MACHINE, g_szNetfx20RegKeyName, g_szNetfx11PlusRegValueName, NULL, (LPBYTE)&dwRegValue, sizeof(DWORD)))
        {
            if (dwRegValue == 1)
                bRetValue = true;
        }
    
        return bRetValue;
    }
    
    
    /******************************************************************
    Function Name:  GetNetfx10SPLevel
    Description:    Uses the detection method recommended at
                    http://blogs.msdn.com/astebner/archive/2004/09/14/229802.aspx
                    to determine what service pack for the
                    .NET Framework 1.0 is installed on the computer.
    Inputs:         NONE
    Results:        integer representing the service pack level for the .NET Framework 1.0
    ******************************************************************/
    int GetNetfx10SPLevel()
    {
        TCHAR szRegValue[MAX_PATH];
        TCHAR *pszSPLevel = NULL;
        int iRetValue = -1;
        bool bRegistryRetVal = false;
    
        // Determine the registry key to use to look up the service pack level
        // because it the registry key is operating system-dependent.
        if (IsCurrentOSTabletMedCenter())
            bRegistryRetVal = RegistryGetValue(HKEY_LOCAL_MACHINE, g_szNetfx10SPxOCMRegKeyName, g_szNetfx10SPxRegValueName, NULL, (LPBYTE)szRegValue, MAX_PATH);
        else
            bRegistryRetVal = RegistryGetValue(HKEY_LOCAL_MACHINE, g_szNetfx10SPxMSIRegKeyName, g_szNetfx10SPxRegValueName, NULL, (LPBYTE)szRegValue, MAX_PATH);
    
        if (bRegistryRetVal)
        {
            // This registry value should be in the following format:
            // #,#,#####,# 
            // Note: The last # is the service pack level.
            // Try to parse off the last # here.
            pszSPLevel = _tcsrchr(szRegValue, _T(','));
            if (NULL != pszSPLevel)
            {
                // Increment the pointer to skip the comma.
                pszSPLevel++;
    
                // Convert the remaining value to an integer.
                iRetValue = _tstoi(pszSPLevel);
            }
        }
    
        return iRetValue;
    }
    
    
    /******************************************************************
    Function Name:  GetNetfx11SPLevel
    Description:    Uses the detection method recommended at
                    http://blogs.msdn.com/astebner/archive/2004/09/14/229574.aspx
                    to determine what service pack for the 
                    .NET Framework 1.1 is installed on the computer.
    Inputs:         NONE
    Results:        integer representing service pack level for the .NET Framework 1.1
    ******************************************************************/
    int GetNetfx11SPLevel()
    {
        DWORD dwRegValue=0;
    
        if (RegistryGetValue(HKEY_LOCAL_MACHINE, g_szNetfx11RegKeyName, g_szNetfx11PlusSPxRegValueName, NULL, (LPBYTE)&dwRegValue, sizeof(DWORD)))
        {
            return (int)dwRegValue;
        }
    
        // We only are here if the .NET Framework 1.1 is not
        // installed or if some kind of error occurred when the code retrieved
        // the data from the registry.
        return -1;
    }
    
    
    /******************************************************************
    Function Name:  GetNetfx20SPLevel
    Description:    Uses the detection method recommended at
                    http://blogs.msdn.com/astebner/archive/2004/09/14/229574.aspx
                    to determine what service pack for the 
                    .NET Framework 2.0 is installed on the computer.
    Inputs:         NONE
    Results:        integer representing the service pack level for the .NET Framework 2.0
    ******************************************************************/
    int GetNetfx20SPLevel()
    {
        DWORD dwRegValue=0;
    
        if (RegistryGetValue(HKEY_LOCAL_MACHINE, g_szNetfx20RegKeyName, g_szNetfx11PlusSPxRegValueName, NULL, (LPBYTE)&dwRegValue, sizeof(DWORD)))
        {
            return (int)dwRegValue;
        }
    
        // We are only here if the .NET Framework 2.0 is not
        // installed or if some kind of error occurred when the code retrieved
        // the data from the registry
        return -1;
    }
    
    
    bool IsCurrentOSTabletMedCenter()
    {
    
        // Use the GetSystemMetrics function to detect if we are on a Tablet PC or a Microsoft Windows Media Center operating system.
        return ( (GetSystemMetrics(SM_TABLETPC) != 0) || (GetSystemMetrics(SM_MEDIACENTER) != 0) );
    }
    
    
    /******************************************************************
    Function Name:  RegistryGetValue
    Description:    Obtain the value of a registry key.
    Inputs:         HKEY hk - The hk of the key to retrieve
                    TCHAR *pszKey - Name of the key to retrieve
                    TCHAR *pszValue - The value that will be retrieved
                    DWORD dwType - The type of the value that will be retrieved
                    LPBYTE data - A buffer to save the retrieved data
                    DWORD dwSize - The size of the data retrieved
    Results:        true if it is successful, false otherwise
    ******************************************************************/
    bool RegistryGetValue(HKEY hk, const TCHAR * pszKey, const TCHAR * pszValue, DWORD dwType, LPBYTE data, DWORD dwSize)
    {
        HKEY hkOpened;
    
        // Try to open the key.
        if (RegOpenKeyEx(hk, pszKey, 0, KEY_READ, &hkOpened) != ERROR_SUCCESS)
        {
            return false;
        }
    
        // If the key was opened, try to retrieve the value.
        if (RegQueryValueEx(hkOpened, pszValue, 0, &dwType, (LPBYTE)data, &dwSize) != ERROR_SUCCESS)
        {
            RegCloseKey(hkOpened);
            return false;
        }
    
        // Clean up.
        RegCloseKey(hkOpened);
    
        return true;
    }
    
    int APIENTRY _tWinMain(HINSTANCE hInstance,
                           HINSTANCE hPrevInstance,
                           LPTSTR    lpCmdLine,
                           int       nCmdShow)
    {
        int iNetfx10SPLevel = -1;
        int iNetfx11SPLevel = -1;
        int iNetfx20SPLevel = -1;
        TCHAR szMessage[MAX_PATH];
    
        // Determine whether the .NET Framework
        // 1.0, 1.1, or 2.0 is installed.
        bool bNetfx10Installed = IsNetfx10Installed();
        bool bNetfx11Installed = IsNetfx11Installed();
        bool bNetfx20Installed = IsNetfx20Installed();
    
        // If the .NET Framework 1.0 is installed, obtain the
        // service pack level.
        if (bNetfx10Installed)
        {
            iNetfx10SPLevel = GetNetfx10SPLevel();
    
            if (iNetfx10SPLevel > 0)
                _stprintf(szMessage, _T("The .NET Framework 1.0 Service Pack %i is installed."), iNetfx10SPLevel);
            else
                _stprintf(szMessage, _T("The .NET Framework 1.0 is installed without service packs."));
    
            MessageBox(NULL, szMessage, _T("The .NET Framework 1.0"), MB_OK | MB_ICONINFORMATION);
        }
        else
        {
            MessageBox(NULL, _T("The .NET Framework 1.0 is not installed."), _T(".NET Framework 1.0"), MB_OK | MB_ICONINFORMATION);
        }
    
        // If the .NET Framework 1.1 is installed, obtain the
        // service pack level.
        if (bNetfx11Installed)
        {
            iNetfx11SPLevel = GetNetfx11SPLevel();
    
            if (iNetfx11SPLevel > 0)
                _stprintf(szMessage, _T("The .NET Framework 1.1 Service Pack %i is installed."), iNetfx11SPLevel);
            else
                _stprintf(szMessage, _T("The .NET Framework 1.1 is installed without service packs."));
    
            MessageBox(NULL, szMessage, _T("The .NET Framework 1.1"), MB_OK | MB_ICONINFORMATION);
        }
        else
        {
            MessageBox(NULL, _T("The .NET Framework 1.1 is not installed."), _T("The .NET Framework 1.1"), MB_OK | MB_ICONINFORMATION);
        }
    
        // If the .NET Framework 2.0 is installed, obtain the
        // service pack level.
        if (bNetfx20Installed)
        {
            iNetfx20SPLevel = GetNetfx20SPLevel();
    
            if (iNetfx20SPLevel > 0)
                _stprintf(szMessage, _T("The .NET Framework 2.0 Service Pack %i is installed."), iNetfx11SPLevel);
            else
                _stprintf(szMessage, _T("The .NET Framework 2.0 is installed without service packs."));
    
            MessageBox(NULL, szMessage, _T("The .NET Framework 2.0"), MB_OK | MB_ICONINFORMATION);
        }
        else
        {
            MessageBox(NULL, _T("The .NET Framework 2.0 is not installed."), _T("The .NET Framework 2.0"), MB_OK | MB_ICONINFORMATION);
        }
    
        return 0;
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值