C++引导程序检测.netFramework版本

  1. 新建一个C++ win32控制台应用程序

接下来是简单代码:

//引用C++常用类库
#include "stdafx.h"
#include "stdio.h"
#include "windows.h"
#include "tchar.h"
#include "strsafe.h"
#include "stdafx.h"

//函数原型声明:
bool isExistFramework();

//取得注册表的值
bool RegistryGetValue(HKEY, const TCHAR*, const TCHAR*, DWORD, LPBYTE, DWORD);
const TCHAR *tFramework45RegKey = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v4.5");
const TCHAR *tFramework45RegVal = _T("Install");

int _tmain(int argc, _TCHAR* argv[])
{    
    if(isExistFramework()){
        //检测.net framework4.5版本通过打开微信
        ShellExecuteA(NULL, "open", "WeChat.exe", NULL, NULL, SW_SHOW);
    }
     else
    {
        MessageBox(NULL,TEXT("检测到系统未安装.net framework 4.5,请安装后再运行该程序。"),TEXT("温馨提示"),MB_OK);
    }
}


bool isExistFramework(){
    
    bool bRetValue = false;
    DWORD dwRegValue=0;

    // 检查安装的注册表值是否存在,等于1?
    if (RegistryGetValue(HKEY_LOCAL_MACHINE, tFramework45RegKey, tFramework45RegVal, NULL, (LPBYTE)&dwRegValue, sizeof(DWORD)))
    {
        //回传过来的注册表的值 == 1,说明已经安装了.netFramework
        if (1 == dwRegValue)
            bRetValue = true;
    }

    // 补充核查,检查版本列出的版本号在注册表中,是否已有预发布版的 .NET Framework 3.5 InstallSuccess值。
    return (bRetValue);
     /*&& CheckNetfxBuildNumber(g_szNetfx35RegKeyName, g_szNetfxStandardVersionRegValueName, g_iNetfx35VersionMajor, g_iNetfx35VersionMinor, g_iNetfx35VersionBuild, g_iNetfx35VersionRevision)*/
}





/******************************************************************
Function Name: RegistryGetValue
Description:    注册表中Key是否存在,存在则取得它的值

Inputs:        
HKEY hk – 注册表的主目录
TCHAR *pszKey – .netFramework的详细路径,子路径KEY
TCHAR *pszValue – 注册表子路径KEY对应的值
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 successful, false otherwise
******************************************************************/
bool RegistryGetValue(HKEY hk, const TCHAR * pszKey, const TCHAR * pszValue, DWORD dwType, LPBYTE data, DWORD dwSize)
{
    HKEY hkOpened;

    // 试着打开KEY,将结果放到hkOpened里面。
    if (RegOpenKeyEx(hk, pszKey, 0, KEY_READ, &hkOpened) != ERROR_SUCCESS)
    {
        return false;
    }

    // 从打开的hkOpened注册表key,检索值,存放到dwSize(引用参数,会回传到主方法)里面。
    if (RegQueryValueEx(hkOpened, pszValue, 0, &dwType, (LPBYTE)data, &dwSize) != ERROR_SUCCESS)
    {
        //关闭打开的注册表Key
        RegCloseKey(hkOpened);
        return false;
    }

    // Clean up
    RegCloseKey(hkOpened);

    return true;
}
View Code

猜你喜欢

转载自www.cnblogs.com/gongjin/p/10264000.html