1
0

ieversion.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "main.h"
  2. #include "./ieversion.h"
  3. #include <shlwapi.h>
  4. #include <strsafe.h>
  5. HRESULT MSIE_GetVersionString(LPWSTR pszBuffer, INT cchBufferMax)
  6. {
  7. if (NULL == pszBuffer) return E_INVALIDARG;
  8. HKEY hKey = NULL;
  9. LONG result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Internet Explorer", 0,
  10. STANDARD_RIGHTS_READ | KEY_QUERY_VALUE, &hKey);
  11. if (ERROR_SUCCESS == result)
  12. {
  13. DWORD cbBuffer = sizeof(WCHAR) * cchBufferMax;
  14. result = RegQueryValueEx(hKey, L"svcVersion", NULL, NULL, (BYTE*)pszBuffer, &cbBuffer);
  15. if (ERROR_SUCCESS != result
  16. || L'\0' == *pszBuffer)
  17. {
  18. cbBuffer = sizeof(WCHAR) * cchBufferMax;
  19. result = RegQueryValueEx(hKey, L"Version", NULL, NULL, (BYTE*)pszBuffer, &cbBuffer);
  20. }
  21. RegCloseKey(hKey);
  22. }
  23. if (ERROR_SUCCESS != result)
  24. {
  25. *pszBuffer = L'\0';
  26. return HRESULT_FROM_WIN32(result);
  27. }
  28. return S_OK;
  29. }
  30. HRESULT MSIE_GetVersion(INT *majorOut, INT *minorOut, INT *buildOut, INT *subBuildOut)
  31. {
  32. INT szVersion[4] = { 0, 0, 0, 0};
  33. WCHAR szBuffer[64] = {0};
  34. HRESULT hr = MSIE_GetVersionString(szBuffer, ARRAYSIZE(szBuffer));
  35. if (SUCCEEDED(hr))
  36. {
  37. INT index = 0;
  38. LPWSTR block = szBuffer;
  39. LPWSTR cursor = block;
  40. for(;;)
  41. {
  42. if (L'\0' == *cursor)
  43. {
  44. if (block != cursor && FALSE != StrToIntEx(block,STIF_DEFAULT, &szVersion[index]))
  45. index++;
  46. break;
  47. }
  48. else if (L'.' == *cursor)
  49. {
  50. *cursor = L'\0';
  51. if (block != cursor && FALSE != StrToIntEx(block,STIF_DEFAULT, &szVersion[index]))
  52. {
  53. index++;
  54. if (index == ARRAYSIZE(szVersion))
  55. break; // too many numbers
  56. }
  57. cursor++;
  58. block = cursor;
  59. }
  60. cursor++;
  61. }
  62. if (index < ARRAYSIZE(szVersion))
  63. hr = E_FAIL;
  64. }
  65. if (NULL != majorOut) *majorOut = szVersion[0];
  66. if (NULL != minorOut) *minorOut = szVersion[1];
  67. if (NULL != buildOut) *buildOut = szVersion[2];
  68. if (NULL != subBuildOut) *subBuildOut = szVersion[3];
  69. return hr;
  70. }