// 必须的头文件; #include // 获取注册表中的.net framwork版本信息; #define MAX_VALUE_NAME 16383 #define TOTALBYTES 8192 #define BYTEINCREMENT 4096 void GetVersionFromRegistry() { DWORD dwRet = 0; HKEY hKeyResult; HKEY hSubKeyResult; TCHAR szKeyValue[MAX_PATH] = {0}; TCHAR szKeyName[MAX_PATH] = _T("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\"); if ( ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKeyName, 0, KEY_READ, &hKeyResult)) { // 枚举所有子键; DWORD dwSubKeyCount = 0; // number of subkeys; DWORD dwSubKeyMaxSize = 0; // longset subkey size; DWORD dwValueCount = 0; // number of values; DWORD dwValueMaxSize = 0; // longset value name; DWORD dwValueDataMaxSize = 0; // longset value data; DWORD dwClassMaxSize = 0; DWORD dwClassNameLen = MAX_PATH; TCHAR szClassName[MAX_PATH] = _T(""); DWORD dwSubKeyNameLen = 0; TCHAR szSubKeyName[MAX_VALUE_NAME] = _T(""); DWORD dwValueNameLen = 0; TCHAR szValueName[MAX_VALUE_NAME] = _T(""); DWORD dwSecurityDescriptor = 0; if ( ERROR_SUCCESS == RegQueryInfoKey( hKeyResult, // Key句柄; szClassName, // Class Name buffer; &dwSubKeyNameLen, // size of class string; NULL, // reserved; &dwSubKeyCount, // number of subkeys; &dwSubKeyMaxSize, // longset subkey size; &dwClassMaxSize, // longset class string; &dwValueCount, // number of values for this key; &dwValueMaxSize, // longset value name; &dwValueDataMaxSize, // longset value data; &dwSecurityDescriptor, // security descriptor; NULL // last write name; ) ) { // 遍历所有子键; for ( int i = 0; i < dwSubKeyCount; i++ ) { dwSubKeyNameLen = MAX_VALUE_NAME; if ( ERROR_SUCCESS == RegEnumKeyEx(hKeyResult, i, szSubKeyName, &dwSubKeyNameLen,NULL, NULL, NULL, NULL) ) { if ( _tcsstr(szSubKeyName, _T("v")) ) { // 查询版本号; if ( _tcsicmp(szSubKeyName, _T("v4")) == 0 ) { // 查询Full键Release子键; _stprintf(szKeyName, _T("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\%s\\Full\\"), szSubKeyName); if ( ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKeyName, 0, KEY_READ, &hSubKeyResult) ) { DWORD cbData = 0; DWORD dwBufferSize = TOTALBYTES; cbData = dwBufferSize; PPERF_DATA_BLOCK PerfData = (PPERF_DATA_BLOCK) malloc( dwBufferSize ); dwRet = RegQueryValueEx(hSubKeyResult, _T("Version"), NULL, NULL, (LPBYTE)PerfData, &cbData); while ( dwRet == ERROR_MORE_DATA ) { dwBufferSize += BYTEINCREMENT; cbData = dwBufferSize; PerfData = (PPERF_DATA_BLOCK) realloc( PerfData, dwBufferSize ); dwRet = RegQueryValueEx( hSubKeyResult, TEXT("Version"), NULL, NULL, (LPBYTE) PerfData, &cbData ); } // 版本号;4.0.30319 if ( dwRet == ERROR_SUCCESS ) { memcpy(szKeyValue, PerfData, cbData); if ( _tcscmp(szKeyValue,_T("4.0.30319")) >= 0 ) { OutputDebugString(_T("已安装.net framwork:")); OutputDebugString(szKeyValue); OutputDebugString(_T("\n")); } } if (PerfData) free(PerfData); // 关闭注册表操作句柄; RegCloseKey(hSubKeyResult); } } else {// 4.0版本之前,直接获取Version键值; _stprintf(szKeyName, _T("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\%s\\"), szSubKeyName); if ( ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKeyName, 0, KEY_READ, &hSubKeyResult) ) { DWORD cbData = 0; DWORD dwBufferSize = TOTALBYTES; cbData = dwBufferSize; PPERF_DATA_BLOCK PerfData = (PPERF_DATA_BLOCK) malloc( dwBufferSize ); dwRet = RegQueryValueEx(hSubKeyResult, _T("Version"), NULL, NULL, (LPBYTE)PerfData, &cbData); while ( dwRet == ERROR_MORE_DATA ) { dwBufferSize += BYTEINCREMENT; cbData = dwBufferSize; PerfData = (PPERF_DATA_BLOCK) realloc( PerfData, dwBufferSize ); dwRet = RegQueryValueEx( hSubKeyResult, TEXT("Version"), NULL, NULL, (LPBYTE) PerfData, &cbData ); } // 版本号; if ( dwRet == ERROR_SUCCESS ) { memcpy(szKeyValue, PerfData, cbData); OutputDebugString(_T("已安装.net framwork:")); OutputDebugString(szKeyValue); OutputDebugString(_T("\n")); } if (PerfData) free(PerfData); // 关闭注册表操作句柄; RegCloseKey(hSubKeyResult); } } } } } #ifdef _QUERY_VALUE_ // 不查询键值; for ( int i = 0; i < dwValueCount; i++ ) { dwValueNameLen = MAX_VALUE_NAME; szValueName[0] = '\0'; if ( ERROR_SUCCESS == RegEnumKeyEx(hKeyResult, i, szValueName, &dwValueNameLen, NULL, NULL, NULL, NULL) ) { OutputDebugString(szValueName); OutputDebugString(_T("\n")); } } #endif } // 关闭注册表操作句柄; RegCloseKey(hKeyResult); } }