DeviceProduct.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "DeviceProduct.h"
  2. DeviceProduct::DeviceProduct()
  3. {
  4. }
  5. DeviceProduct::DeviceProduct(wstring ProductName)
  6. {
  7. m_ProductName = ProductName;
  8. FindNETFramework();
  9. FindVsCPulsPulsRedistributable();
  10. OpenLocalMachineRootKey(RegeditCore::smPath_ProductsInfo.c_str());
  11. }
  12. DeviceProduct::~DeviceProduct()
  13. {
  14. }
  15. wstring DeviceProduct::GetVersion()
  16. {
  17. return m_Version;
  18. }
  19. wstring DeviceProduct::GetInstallLocation()
  20. {
  21. return m_InstallLocation;
  22. }
  23. wstring DeviceProduct::GetNETVersion()
  24. {
  25. return m_NETVersion;
  26. }
  27. wstring DeviceProduct::GetNETTargetVersion()
  28. {
  29. return m_NETTargetVersion;
  30. }
  31. wstring DeviceProduct::GetVsCPulsPulsVersion() {
  32. return m_VsCPulsPulsVersion;
  33. }
  34. wstring DeviceProduct::GetVsCPulsPulsInstalled() {
  35. return m_VsCPulsPulsInstalled;
  36. }
  37. bool DeviceProduct::OpenLocalMachineRootKey(LPCTSTR productsInfoPath)
  38. {
  39. long lRet;
  40. HKEY hKey;
  41. LPCTSTR lpSubKey;
  42. lpSubKey = productsInfoPath;
  43. lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpSubKey, 0, KEY_READ | KEY_WOW64_64KEY, &hKey);
  44. bool state = false;
  45. if (lRet == ERROR_SUCCESS)
  46. {
  47. state = SearchAllProducts(hKey, lpSubKey);
  48. }
  49. return state;
  50. }
  51. bool DeviceProduct::SearchAllProducts(HKEY hKey, LPCTSTR productsInfoPath)
  52. {
  53. bool state = false;
  54. DWORD index = 0;
  55. TCHAR szKeyName[255] = { 0 };
  56. DWORD dwKeyLen = 255;
  57. wstring strBuffer;
  58. while (ERROR_NO_MORE_ITEMS != RegEnumKeyEx(hKey, index, szKeyName, &dwKeyLen, 0, NULL, NULL, NULL))
  59. {
  60. index++;
  61. wstring temp(szKeyName);
  62. strBuffer = temp;
  63. if (!strBuffer.empty())
  64. {
  65. wstring productsInfo(productsInfoPath);
  66. strBuffer = productsInfo + L"\\" + strBuffer;
  67. state = OpenLocalMachineSubKey(strBuffer);
  68. if (state)
  69. break;
  70. //continue;
  71. }
  72. dwKeyLen = 255;
  73. memset(szKeyName, 0, 255);
  74. }
  75. dwKeyLen = 255;
  76. memset(szKeyName, 0, 255);
  77. return state;
  78. }
  79. bool DeviceProduct::OpenLocalMachineSubKey(std::wstring AppKey)
  80. {
  81. bool state = false;
  82. long lRet;
  83. HKEY hKey;
  84. LPCTSTR lpSubKey;
  85. //AppKey = RegeditCore::smPath_ProductsInfo + L"\\" + AppKey;
  86. lpSubKey = AppKey.c_str();
  87. lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpSubKey, 0, KEY_READ | KEY_WOW64_64KEY, &hKey);
  88. if (lRet == ERROR_SUCCESS)
  89. {
  90. state = FindProductInfo(hKey, lpSubKey);
  91. }
  92. else
  93. {
  94. m_Version = L"";
  95. }
  96. return state;
  97. }
  98. bool DeviceProduct::FindProductInfo(HKEY hKey, LPCTSTR lpSubKey)
  99. {
  100. bool state = false;
  101. DWORD dwType = REG_BINARY | REG_DWORD | REG_EXPAND_SZ | REG_MULTI_SZ | REG_NONE | REG_SZ | REG_DWORD_LITTLE_ENDIAN | REG_DWORD_BIG_ENDIAN | REG_LINK | REG_RESOURCE_LIST | REG_FULL_RESOURCE_DESCRIPTOR | REG_RESOURCE_REQUIREMENTS_LIST | REG_QWORD_LITTLE_ENDIAN;
  102. HKEY hkRKey;
  103. DWORD index = 0;
  104. TCHAR szKeyName[255] = { 0 };
  105. DWORD dwKeyLen = 255;
  106. wstring strBuffer;
  107. wstring strMidReg;
  108. TCHAR szBufferAppInfo[255] = { 0 };
  109. DWORD dwNameLen = 255;
  110. //遍历已安装程序
  111. while (ERROR_NO_MORE_ITEMS != RegEnumKeyEx(hKey, index, szKeyName, &dwKeyLen, 0, NULL, NULL, NULL))
  112. {
  113. //获取已安装程序的节点
  114. index++;
  115. wstring nodeTemp(szKeyName);
  116. strBuffer = nodeTemp;
  117. if (!strBuffer.empty())
  118. {
  119. wstring nodePathTemp(L"\\" + strBuffer);
  120. strMidReg = lpSubKey + nodePathTemp;
  121. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, strMidReg.c_str(), 0, KEY_ALL_ACCESS | KEY_WOW64_64KEY, &hkRKey) == ERROR_SUCCESS)
  122. {
  123. RegQueryValueEx(hkRKey, L"DisplayName", 0, &dwType, (LPBYTE)szBufferAppInfo, &dwNameLen);
  124. wstring displayNameTemp(szBufferAppInfo);
  125. dwNameLen = 255;
  126. memset(szBufferAppInfo, 0, 255);
  127. if (displayNameTemp == m_ProductName)
  128. {
  129. RegQueryValueEx(hkRKey, L"DisplayVersion", 0, &dwType, (LPBYTE)szBufferAppInfo, &dwNameLen);
  130. wstring displayVersionTemp(szBufferAppInfo);
  131. m_Version = displayVersionTemp;
  132. dwNameLen = 255;
  133. memset(szBufferAppInfo, 0, 255);
  134. RegQueryValueEx(hkRKey, L"InstallLocation", 0, &dwType, (LPBYTE)szBufferAppInfo, &dwNameLen);
  135. wstring installLocationTemp(szBufferAppInfo);
  136. m_InstallLocation = installLocationTemp;
  137. dwNameLen = 255;
  138. memset(szBufferAppInfo, 0, 255);
  139. return state;
  140. }
  141. dwNameLen = 255;
  142. memset(szBufferAppInfo, 0, 255);
  143. }
  144. dwKeyLen = 255;
  145. memset(szKeyName, 0, 255);
  146. }
  147. }
  148. return state;
  149. }
  150. bool DeviceProduct::FindNETFramework()
  151. {
  152. HKEY hkRKey;
  153. TCHAR szBufferAppInfo[255] = { 0 };
  154. DWORD dwNameLen = 255;
  155. DWORD dwType = REG_BINARY | REG_DWORD | REG_EXPAND_SZ | REG_MULTI_SZ | REG_NONE | REG_SZ | REG_DWORD_LITTLE_ENDIAN | REG_DWORD_BIG_ENDIAN | REG_LINK | REG_RESOURCE_LIST | REG_FULL_RESOURCE_DESCRIPTOR | REG_RESOURCE_REQUIREMENTS_LIST | REG_QWORD_LITTLE_ENDIAN;
  156. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, RegeditCore::smPath_NETFrameworkInfo.c_str(), 0, KEY_READ | KEY_WOW64_64KEY, &hkRKey) == ERROR_SUCCESS)
  157. {
  158. RegQueryValueEx(hkRKey, L"TargetVersion", 0, &dwType, (LPBYTE)szBufferAppInfo, &dwNameLen);
  159. wstring displayVersionTemp(szBufferAppInfo);
  160. m_NETTargetVersion = displayVersionTemp;
  161. dwNameLen = 255;
  162. memset(szBufferAppInfo, 0, 255);
  163. RegQueryValueEx(hkRKey, L"Version", 0, &dwType, (LPBYTE)szBufferAppInfo, &dwNameLen);
  164. wstring installLocationTemp(szBufferAppInfo);
  165. m_NETVersion = installLocationTemp;
  166. dwNameLen = 255;
  167. memset(szBufferAppInfo, 0, 255);
  168. return true;
  169. }
  170. return false;
  171. }
  172. bool DeviceProduct::FindVsCPulsPulsRedistributable() {
  173. HKEY hkRKey;
  174. TCHAR szBufferAppInfo[255] = { 0 };
  175. DWORD dwNameLen = 255;
  176. DWORD dwType = REG_BINARY | REG_DWORD | REG_EXPAND_SZ | REG_MULTI_SZ | REG_NONE | REG_SZ | REG_DWORD_LITTLE_ENDIAN | REG_DWORD_BIG_ENDIAN | REG_LINK | REG_RESOURCE_LIST | REG_FULL_RESOURCE_DESCRIPTOR | REG_RESOURCE_REQUIREMENTS_LIST | REG_QWORD_LITTLE_ENDIAN;
  177. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, RegeditCore::smPath_VsCPulsPulsInfoX64.c_str(), 0, KEY_READ | KEY_WOW64_64KEY, &hkRKey) == ERROR_SUCCESS)
  178. {
  179. RegQueryValueEx(hkRKey, L"Installed", 0, &dwType, (LPBYTE)szBufferAppInfo, &dwNameLen);
  180. wstring displayVersionTemp(szBufferAppInfo);
  181. m_VsCPulsPulsInstalled = displayVersionTemp;
  182. dwNameLen = 255;
  183. memset(szBufferAppInfo, 0, 255);
  184. RegQueryValueEx(hkRKey, L"Version", 0, &dwType, (LPBYTE)szBufferAppInfo, &dwNameLen);
  185. wstring installLocationTemp(szBufferAppInfo);
  186. m_VsCPulsPulsVersion = installLocationTemp;
  187. dwNameLen = 255;
  188. memset(szBufferAppInfo, 0, 255);
  189. return true;
  190. }
  191. return false;
  192. }