1. 程式人生 > >C++載入程式檢測.netFramework版本

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