- 新建一個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; }