1
0

GetWinVer.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // GetWinVer.cpp Version 1.1
  2. //
  3. // Copyright (C) 2001-2003 Hans Dietrich
  4. //
  5. // This software is released into the public domain.
  6. // You are free to use it in any way you like, except
  7. // that you may not sell this source code.
  8. //
  9. // This software is provided "as is" with no expressed
  10. // or implied warranty. I accept no liability for any
  11. // damage or loss of business that this software may cause.
  12. //
  13. ///////////////////////////////////////////////////////////////////////////////
  14. //#include "tchar.h"
  15. #include "GetWinVer.h"
  16. // from winbase.h
  17. #ifndef VER_PLATFORM_WIN32s
  18. #define VER_PLATFORM_WIN32s 0
  19. #endif
  20. #ifndef VER_PLATFORM_WIN32_WINDOWS
  21. #define VER_PLATFORM_WIN32_WINDOWS 1
  22. #endif
  23. #ifndef VER_PLATFORM_WIN32_NT
  24. #define VER_PLATFORM_WIN32_NT 2
  25. #endif
  26. #ifndef VER_PLATFORM_WIN32_CE
  27. #define VER_PLATFORM_WIN32_CE 3
  28. #endif
  29. /*
  30. This table has been assembled from Usenet postings, personal
  31. observations, and reading other people's code. Please feel
  32. free to add to it or correct it.
  33. dwPlatFormID dwMajorVersion dwMinorVersion dwBuildNumber
  34. 95 1 4 0 950
  35. 95 SP1 1 4 0 >950 && <=1080
  36. 95 OSR2 1 4 <10 >1080
  37. 98 1 4 10 1998
  38. 98 SP1 1 4 10 >1998 && <2183
  39. 98 SE 1 4 10 >=2183
  40. ME 1 4 90 3000
  41. NT 3.51 2 3 51
  42. NT 4 2 4 0 1381
  43. 2000 2 5 0 2195
  44. XP 2 5 1 2600
  45. 2003 Server 2 5 2 3790
  46. VISTA 2 6 0 6000
  47. 7 2 6 1 7600
  48. 8 2 6 2 9200
  49. 8.1 2 6 3 9600
  50. 10 2 10 0 10240
  51. 11 2 11 0 22000
  52. CE 3
  53. */
  54. ///////////////////////////////////////////////////////////////////////////////
  55. // GetWinVer
  56. BOOL GetWinVer(LPWSTR pszVersion, int *nVersion, LPWSTR pszMajorMinorBuild)
  57. {
  58. if (!pszVersion || !nVersion || !pszMajorMinorBuild)
  59. return FALSE;
  60. lstrcpy(pszVersion, WUNKNOWNSTR);
  61. *nVersion = WUNKNOWN;
  62. DWORD (WINAPI *RtlGetVersion)(LPOSVERSIONINFOEXW);
  63. OSVERSIONINFOEXW osinfo;
  64. *(FARPROC*)&RtlGetVersion = GetProcAddress(GetModuleHandleW(L"ntdll"), "RtlGetVersion");
  65. if (!RtlGetVersion) {
  66. return FALSE;
  67. }
  68. osinfo.dwOSVersionInfoSize = sizeof(osinfo);
  69. if (RtlGetVersion(&osinfo)) {
  70. return FALSE;
  71. }
  72. DWORD dwPlatformId = osinfo.dwPlatformId;
  73. DWORD dwMajorVersion = osinfo.dwMajorVersion;
  74. DWORD dwMinorVersion = osinfo.dwMinorVersion;
  75. DWORD dwBuildNumber = osinfo.dwBuildNumber & 0xFFFF; // Win 95 needs this
  76. wsprintfW(pszMajorMinorBuild, L"%u.%u.%u", dwMajorVersion, dwMinorVersion, dwBuildNumber);
  77. if ((dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) && (dwMajorVersion == 4))
  78. {
  79. if ((dwMinorVersion < 10) && (dwBuildNumber == 950))
  80. {
  81. lstrcpy(pszVersion, W95STR);
  82. *nVersion = W95;
  83. }
  84. else if ((dwMinorVersion < 10) &&
  85. ((dwBuildNumber > 950) && (dwBuildNumber <= 1080)))
  86. {
  87. lstrcpy(pszVersion, W95SP1STR);
  88. *nVersion = W95SP1;
  89. }
  90. else if ((dwMinorVersion < 10) && (dwBuildNumber > 1080))
  91. {
  92. lstrcpy(pszVersion, W95OSR2STR);
  93. *nVersion = W95OSR2;
  94. }
  95. else if ((dwMinorVersion == 10) && (dwBuildNumber == 1998))
  96. {
  97. lstrcpy(pszVersion, W98STR);
  98. *nVersion = W98;
  99. }
  100. else if ((dwMinorVersion == 10) &&
  101. ((dwBuildNumber > 1998) && (dwBuildNumber < 2183)))
  102. {
  103. lstrcpy(pszVersion, W98SP1STR);
  104. *nVersion = W98SP1;
  105. }
  106. else if ((dwMinorVersion == 10) && (dwBuildNumber >= 2183))
  107. {
  108. lstrcpy(pszVersion, W98SESTR);
  109. *nVersion = W98SE;
  110. }
  111. else if (dwMinorVersion == 90)
  112. {
  113. lstrcpy(pszVersion, WMESTR);
  114. *nVersion = WME;
  115. }
  116. }
  117. else if (dwPlatformId == VER_PLATFORM_WIN32_NT)
  118. {
  119. if ((dwMajorVersion == 3) && (dwMinorVersion == 51))
  120. {
  121. lstrcpy(pszVersion, WNT351STR);
  122. *nVersion = WNT351;
  123. }
  124. else if ((dwMajorVersion == 4) && (dwMinorVersion == 0))
  125. {
  126. lstrcpy(pszVersion, WNT4STR);
  127. *nVersion = WNT4;
  128. }
  129. else if ((dwMajorVersion == 5) && (dwMinorVersion == 0))
  130. {
  131. lstrcpy(pszVersion, W2KSTR);
  132. *nVersion = W2K;
  133. }
  134. else if ((dwMajorVersion == 5) && (dwMinorVersion == 1))
  135. {
  136. lstrcpy(pszVersion, WXPSTR);
  137. *nVersion = WXP;
  138. }
  139. else if ((dwMajorVersion == 5) && (dwMinorVersion == 2))
  140. {
  141. lstrcpy(pszVersion, W2003SERVERSTR);
  142. *nVersion = W2003SERVER;
  143. }
  144. else if ((dwMajorVersion == 6) && (dwMinorVersion == 0))
  145. {
  146. lstrcpy(pszVersion, WVSTR);
  147. *nVersion = WV;
  148. }
  149. else if ((dwMajorVersion == 6) && (dwMinorVersion == 1))
  150. {
  151. lstrcpy(pszVersion, W7STR);
  152. *nVersion = W7;
  153. }
  154. else if ((dwMajorVersion == 6) && (dwMinorVersion == 2))
  155. {
  156. lstrcpy(pszVersion, W8STR);
  157. *nVersion = W8;
  158. }
  159. else if ((dwMajorVersion == 6) && (dwMinorVersion == 3))
  160. {
  161. lstrcpy(pszVersion, W81STR);
  162. *nVersion = W81;
  163. }
  164. else if ((dwMajorVersion == 10) && (dwMinorVersion == 0) && (dwBuildNumber < 22000))
  165. {
  166. lstrcpy(pszVersion, W10STR);
  167. *nVersion = W10;
  168. }
  169. else if ((dwMajorVersion == 10) && (dwMinorVersion == 0) && (dwBuildNumber >= 22000))
  170. {
  171. lstrcpy(pszVersion, W11STR);
  172. *nVersion = W11;
  173. }
  174. }
  175. else if (dwPlatformId == VER_PLATFORM_WIN32_CE)
  176. {
  177. lstrcpy(pszVersion, WCESTR);
  178. *nVersion = WCE;
  179. }
  180. return TRUE;
  181. }