Get OS in C ++ win32 for all win versions?

How to get the OS version for all windows, at least the name for win95,98, me, xp, vista, 7?

Im using visual C ++ 2010, and I want to enable this feature in a clean win32 application.

+4
source share
4 answers

How about something like this:

 #include <windows.h> #include <string> #include <lm.h> #pragma comment(lib, "netapi32.lib") bool GetWinMajorMinorVersion(DWORD& major, DWORD& minor) { bool bRetCode = false; LPBYTE pinfoRawData = 0; if (NERR_Success == NetWkstaGetInfo(NULL, 100, &pinfoRawData)) { WKSTA_INFO_100* pworkstationInfo = (WKSTA_INFO_100*)pinfoRawData; major = pworkstationInfo->wki100_ver_major; minor = pworkstationInfo->wki100_ver_minor; ::NetApiBufferFree(pinfoRawData); bRetCode = true; } return bRetCode; } std::string GetWindowsVersionString() { std::string winver; OSVERSIONINFOEX osver; SYSTEM_INFO sysInfo; typedef void(__stdcall *GETSYSTEMINFO) (LPSYSTEM_INFO); __pragma(warning(push)) __pragma(warning(disable:4996)) memset(&osver, 0, sizeof(osver)); osver.dwOSVersionInfoSize = sizeof(osver); GetVersionEx((LPOSVERSIONINFO)&osver); __pragma(warning(pop)) DWORD major = 0; DWORD minor = 0; if (GetWinMajorMinorVersion(major, minor)) { osver.dwMajorVersion = major; osver.dwMinorVersion = minor; } else if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 2) { OSVERSIONINFOEXW osvi; ULONGLONG cm = 0; cm = VerSetConditionMask(cm, VER_MINORVERSION, VER_EQUAL); ZeroMemory(&osvi, sizeof(osvi)); osvi.dwOSVersionInfoSize = sizeof(osvi); osvi.dwMinorVersion = 3; if (VerifyVersionInfoW(&osvi, VER_MINORVERSION, cm)) { osver.dwMinorVersion = 3; } } GETSYSTEMINFO getSysInfo = (GETSYSTEMINFO)GetProcAddress(GetModuleHandle(L"kernel32.dll"), "GetNativeSystemInfo"); if (getSysInfo == NULL) getSysInfo = ::GetSystemInfo; getSysInfo(&sysInfo); if (osver.dwMajorVersion == 10 && osver.dwMinorVersion >= 0 && osver.wProductType != VER_NT_WORKSTATION) winver = "Windows 10 Server"; if (osver.dwMajorVersion == 10 && osver.dwMinorVersion >= 0 && osver.wProductType == VER_NT_WORKSTATION) winver = "Windows 10"; if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 3 && osver.wProductType != VER_NT_WORKSTATION) winver = "Windows Server 2012 R2"; if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 3 && osver.wProductType == VER_NT_WORKSTATION) winver = "Windows 8.1"; if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 2 && osver.wProductType != VER_NT_WORKSTATION) winver = "Windows Server 2012"; if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 2 && osver.wProductType == VER_NT_WORKSTATION) winver = "Windows 8"; if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 1 && osver.wProductType != VER_NT_WORKSTATION) winver = "Windows Server 2008 R2"; if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 1 && osver.wProductType == VER_NT_WORKSTATION) winver = "Windows 7"; if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 0 && osver.wProductType != VER_NT_WORKSTATION) winver = "Windows Server 2008"; if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 0 && osver.wProductType == VER_NT_WORKSTATION) winver = "Windows Vista"; if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 2 && osver.wProductType == VER_NT_WORKSTATION && sysInfo.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64) winver = "Windows XP x64"; if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 2) winver = "Windows Server 2003"; if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 1) winver = "Windows XP"; if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 0) winver = "Windows 2000"; if (osver.dwMajorVersion < 5) winver = "unknown"; if (osver.wServicePackMajor != 0) { std::string sp; char buf[128] = { 0 }; sp = " Service Pack "; sprintf_s(buf, sizeof(buf), "%hd", osver.wServicePackMajor); sp.append(buf); winver += sp; } return winver; } 
+4
source

Take a look at the MSDN article Getting a System Version

Only supported versions of Windows are mentioned in the article; see this article is the knowledge base article for numbers that you will see in the OSVERSIONINFO structure for Win 95, 98, etc.

+2
source

It all depends on why you need to know the OS version:

  • Use a specific function that may not be available in an older OS. In this case, I highly recommend checking to see if the API itself is accessible with LoadLibrary and GetProcAddress . Otherwise, I think you can dynamically import RtlGetVersion from ntdll.dll and use it, but again, there are too many ways to return inaccurate information (those that come to mind are compatibility mode and API trampolines, which can be installed by malware, AVP, and even the OS itself.)

  • For display only. (for example: in About for your application or for inclusion in the diagnostic event log, etc.). In this case, quickly and simply hack it as text from the registry:

enter image description here

The disadvantage of this approach is that names and numbers can be localized to the end-user system.

Use the following key available with Windows XP:

 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion 

and the following REG_SZ values ​​(or strings):

  • ProductName = for OS name
  • CurrentVersion = for OS version
  • BuildLab = for full build number
  • BuildLabEx = extended build number (available with Windows 7)
  • ReleaseId = Release Number (available with Windows 10)
0
source

Source: https://habr.com/ru/post/1310421/


All Articles