获取.net版本.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // 必须的头文件;
  2. #include <Windows.h>
  3. // 获取注册表中的.net framwork版本信息;
  4. #define MAX_VALUE_NAME 16383
  5. #define TOTALBYTES 8192
  6. #define BYTEINCREMENT 4096
  7. void GetVersionFromRegistry()
  8. {
  9. DWORD dwRet = 0;
  10. HKEY hKeyResult;
  11. HKEY hSubKeyResult;
  12. TCHAR szKeyValue[MAX_PATH] = {0};
  13. TCHAR szKeyName[MAX_PATH] = _T("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\");
  14. if ( ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKeyName, 0, KEY_READ, &hKeyResult))
  15. {
  16. // 枚举所有子键;
  17. DWORD dwSubKeyCount = 0; // number of subkeys;
  18. DWORD dwSubKeyMaxSize = 0; // longset subkey size;
  19. DWORD dwValueCount = 0; // number of values;
  20. DWORD dwValueMaxSize = 0; // longset value name;
  21. DWORD dwValueDataMaxSize = 0; // longset value data;
  22. DWORD dwClassMaxSize = 0;
  23. DWORD dwClassNameLen = MAX_PATH;
  24. TCHAR szClassName[MAX_PATH] = _T("");
  25. DWORD dwSubKeyNameLen = 0;
  26. TCHAR szSubKeyName[MAX_VALUE_NAME] = _T("");
  27. DWORD dwValueNameLen = 0;
  28. TCHAR szValueName[MAX_VALUE_NAME] = _T("");
  29. DWORD dwSecurityDescriptor = 0;
  30. if ( ERROR_SUCCESS == RegQueryInfoKey(
  31. hKeyResult, // Key句柄;
  32. szClassName, // Class Name buffer;
  33. &dwSubKeyNameLen, // size of class string;
  34. NULL, // reserved;
  35. &dwSubKeyCount, // number of subkeys;
  36. &dwSubKeyMaxSize, // longset subkey size;
  37. &dwClassMaxSize, // longset class string;
  38. &dwValueCount, // number of values for this key;
  39. &dwValueMaxSize, // longset value name;
  40. &dwValueDataMaxSize, // longset value data;
  41. &dwSecurityDescriptor, // security descriptor;
  42. NULL // last write name;
  43. ) )
  44. {
  45. // 遍历所有子键;
  46. for ( int i = 0; i < dwSubKeyCount; i++ )
  47. {
  48. dwSubKeyNameLen = MAX_VALUE_NAME;
  49. if ( ERROR_SUCCESS == RegEnumKeyEx(hKeyResult, i, szSubKeyName, &dwSubKeyNameLen,NULL, NULL, NULL, NULL) )
  50. {
  51. if ( _tcsstr(szSubKeyName, _T("v")) )
  52. {
  53. // 查询版本号;
  54. if ( _tcsicmp(szSubKeyName, _T("v4")) == 0 )
  55. {
  56. // 查询Full键Release子键;
  57. _stprintf(szKeyName, _T("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\%s\\Full\\"), szSubKeyName);
  58. if ( ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKeyName, 0, KEY_READ, &hSubKeyResult) )
  59. {
  60. DWORD cbData = 0;
  61. DWORD dwBufferSize = TOTALBYTES;
  62. cbData = dwBufferSize;
  63. PPERF_DATA_BLOCK PerfData = (PPERF_DATA_BLOCK) malloc( dwBufferSize );
  64. dwRet = RegQueryValueEx(hSubKeyResult, _T("Version"), NULL, NULL, (LPBYTE)PerfData, &cbData);
  65. while ( dwRet == ERROR_MORE_DATA )
  66. {
  67. dwBufferSize += BYTEINCREMENT;
  68. cbData = dwBufferSize;
  69. PerfData = (PPERF_DATA_BLOCK) realloc( PerfData, dwBufferSize );
  70. dwRet = RegQueryValueEx( hSubKeyResult,
  71. TEXT("Version"),
  72. NULL,
  73. NULL,
  74. (LPBYTE) PerfData,
  75. &cbData );
  76. }
  77. // 版本号;4.0.30319
  78. if ( dwRet == ERROR_SUCCESS )
  79. {
  80. memcpy(szKeyValue, PerfData, cbData);
  81. if ( _tcscmp(szKeyValue,_T("4.0.30319")) >= 0 )
  82. {
  83. OutputDebugString(_T("已安装.net framwork:"));
  84. OutputDebugString(szKeyValue);
  85. OutputDebugString(_T("\n"));
  86. }
  87. }
  88. if (PerfData)
  89. free(PerfData);
  90. // 关闭注册表操作句柄;
  91. RegCloseKey(hSubKeyResult);
  92. }
  93. }
  94. else
  95. {// 4.0版本之前,直接获取Version键值;
  96. _stprintf(szKeyName, _T("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\%s\\"), szSubKeyName);
  97. if ( ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKeyName, 0, KEY_READ, &hSubKeyResult) )
  98. {
  99. DWORD cbData = 0;
  100. DWORD dwBufferSize = TOTALBYTES;
  101. cbData = dwBufferSize;
  102. PPERF_DATA_BLOCK PerfData = (PPERF_DATA_BLOCK) malloc( dwBufferSize );
  103. dwRet = RegQueryValueEx(hSubKeyResult, _T("Version"), NULL, NULL, (LPBYTE)PerfData, &cbData);
  104. while ( dwRet == ERROR_MORE_DATA )
  105. {
  106. dwBufferSize += BYTEINCREMENT;
  107. cbData = dwBufferSize;
  108. PerfData = (PPERF_DATA_BLOCK) realloc( PerfData, dwBufferSize );
  109. dwRet = RegQueryValueEx( hSubKeyResult,
  110. TEXT("Version"),
  111. NULL,
  112. NULL,
  113. (LPBYTE) PerfData,
  114. &cbData );
  115. }
  116. // 版本号;
  117. if ( dwRet == ERROR_SUCCESS )
  118. {
  119. memcpy(szKeyValue, PerfData, cbData);
  120. OutputDebugString(_T("已安装.net framwork:"));
  121. OutputDebugString(szKeyValue);
  122. OutputDebugString(_T("\n"));
  123. }
  124. if (PerfData)
  125. free(PerfData);
  126. // 关闭注册表操作句柄;
  127. RegCloseKey(hSubKeyResult);
  128. }
  129. }
  130. }
  131. }
  132. }
  133. #ifdef _QUERY_VALUE_ // 不查询键值;
  134. for ( int i = 0; i < dwValueCount; i++ )
  135. {
  136. dwValueNameLen = MAX_VALUE_NAME;
  137. szValueName[0] = '\0';
  138. if ( ERROR_SUCCESS == RegEnumKeyEx(hKeyResult, i, szValueName, &dwValueNameLen, NULL, NULL, NULL, NULL) )
  139. {
  140. OutputDebugString(szValueName);
  141. OutputDebugString(_T("\n"));
  142. }
  143. }
  144. #endif
  145. }
  146. // 关闭注册表操作句柄;
  147. RegCloseKey(hKeyResult);
  148. }
  149. }