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 getRegistryValue(HKEY, const TCHAR*, const TCHAR*, DWORD, LPBYTE, DWORD);
//注册表子目录
const TCHAR *tFramework45RegSubPath = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v3.5");
//子目录下真正的Key【Install】
const TCHAR *tFramework45RegKey = _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;

    //dwRegValue = 注册表key对应的返回值
    if (getRegistryValue(HKEY_LOCAL_MACHINE, tFramework45RegSubPath, tFramework45RegKey, 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)*/
}





/******************************************************************
注册表中子目录是否存在,存在则取得它的值

HKEY LhPath – 注册表的主路径【HKEY_LOCAL_MACHINE】
TCHAR *LcSubPath – 详细路径
TCHAR *LcKey – 详细路径下的Key【Install】
DWORD dwType – The type of the value that will be retrieved
LPBYTE LbValue – 返回值:【Key对应的Value】
DWORD dwSize – The size of the LbValue retrieved
Results:        true if successful, false otherwise
******************************************************************/
bool getRegistryValue(HKEY LhPath, const TCHAR * LcSubPath, const TCHAR * LcKey, DWORD dwType, LPBYTE LbValue, DWORD dwSize)
{
    HKEY hkOpened;

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

    // 从打开的hkOpened键值队中,检索LcKey对应的值,存放到dwSize(引用参数,会回传到主方法)
    if (RegQueryValueEx(hkOpened, LcKey, 0, &dwType, (LPBYTE)LbValue, &dwSize) != ERROR_SUCCESS)
    {
        //关闭打开的注册表Key
        RegCloseKey(hkOpened);
        return false;
    }

    // Clean up
    RegCloseKey(hkOpened);

    return true;
}

 

posted @ 2019-01-13 21:04  剑诀浮云  阅读(1140)  评论(0编辑  收藏  举报