Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Windows OS version programmatically

I am trying to fetch Windows version with C# on my Windows 10 machine.

I always get those values (with C#\C++):

Major: 6

Minor: 2

Which is Windows 8 OS, accordingly to MSDN

C# code:

var major = OperatingSystem.Version.Major
var minor  = OperatingSystem.Version.Minor

C++ code

void print_os_info()
{
    //http://stackoverflow.com/questions/1963992/check-windows-version
    OSVERSIONINFOW info;
    ZeroMemory(&info, sizeof(OSVERSIONINFOW));
    info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);

    LPOSVERSIONINFOW lp_info = &info;
    GetVersionEx(lp_info);

    printf("Windows version: %u.%u\n", info.dwMajorVersion, info.dwMinorVersion);
}

Windows 10 suppose to be with those:

Major: 10

Minor: 0*

  • (When I am taking a dump file from running process I can see that the OS version of that file is set to 10.0)

built by: 10.0.10586.0 (th2_release.151029-1700)

What am I missing here?

like image 303
Pavel Durov Avatar asked Jun 08 '16 10:06

Pavel Durov


People also ask

How do I find my os version using CMD?

Winver is a command that displays the version of Windows that is running, the build number and what service packs are installed: Click Start – RUN , type “winver” and press enter.

How do I get the os version of Windows using PowerShell?

To find out the Windows version, you can utilize the following PowerShell “Get-ComputerInfo” command, “Get-ItemProperty” command, “systeminfo” command, “System. Environment” command, and “Get-CimInstance” command. All of these commands can display the Windows version along with other related information.

How do I get Windows version from WMIC?

You can use wmic os get commands on a Microsoft Windows system to view information related to the operating system via a command-line interface (CLI). E.g., to determine the version of the operating system you can issue the command Windows Management Instrumentation Command-line (WMIC) command wmic os get version .


1 Answers

As the accepted answer is only for C#, here is a solution for C++.

It uses the RtlGetVersion in the ntdll.dll that uses the same structure as GetVersionEx (name is different, but the elements are the same) and gives you the correct version. As this function is normally used for driver development, the function is declared in the DDK and not in the SDK. So I used a dynamic solution to call the function. Please be aware that the ntdll.dll is loaded and released in every call. So if you need the function more often, keep the library loaded.

The structure pOSversion is pointing to must be initialized like for GetVersionEx.

BOOL GetTrueWindowsVersion(OSVERSIONINFOEX* pOSversion)
{
   // Function pointer to driver function
   NTSTATUS (WINAPI *pRtlGetVersion)(
      PRTL_OSVERSIONINFOW lpVersionInformation) = NULL;

   // load the System-DLL
   HINSTANCE hNTdllDll = LoadLibrary("ntdll.dll");

   // successfully loaded?
   if (hNTdllDll != NULL)
   {
      // get the function pointer to RtlGetVersion
      pRtlGetVersion = (NTSTATUS (WINAPI *)(PRTL_OSVERSIONINFOW))
            GetProcAddress (hNTdllDll, "RtlGetVersion");

      // if successfull then read the function
      if (pRtlGetVersion != NULL)
         pRtlGetVersion((PRTL_OSVERSIONINFOW)pOSversion);

      // free the library
      FreeLibrary(hNTdllDll);
   } // if (hNTdllDll != NULL)

   // if function failed, use fallback to old version
   if (pRtlGetVersion == NULL)
      GetVersionEx((OSVERSIONINFO*)pOSversion);

   // always true ...
   return (TRUE);
} // GetTrueWindowsVersion
like image 188
Peter Thaus Avatar answered Oct 12 '22 23:10

Peter Thaus