0

If you type ver in cmd you get something like:

Microsoft Windows [Version 10.0.17192.162]

Is there anyway I can access this information to use in my C program? I need to find the version of Windows a person is running. I've checked out SYSTEM_INFO:

typedef struct _SYSTEM_INFO {
  union {
    DWORD  dwOemId;
    struct {
      WORD wProcessorArchitecture;
      WORD wReserved;
    };
  };
  DWORD     dwPageSize;
  LPVOID    lpMinimumApplicationAddress;
  LPVOID    lpMaximumApplicationAddress;
  DWORD_PTR dwActiveProcessorMask;
  DWORD     dwNumberOfProcessors;
  DWORD     dwProcessorType;
  DWORD     dwAllocationGranularity;
  WORD      wProcessorLevel;
  WORD      wProcessorRevision;
} SYSTEM_INFO;

and OSVERSIONINFO

typedef struct _OSVERSIONINFOA {
  DWORD dwOSVersionInfoSize;
  DWORD dwMajorVersion;
  DWORD dwMinorVersion;
  DWORD dwBuildNumber;
  DWORD dwPlatformId;
  CHAR  szCSDVersion[128];
} OSVERSIONINFOA, *POSVERSIONINFOA, *LPOSVERSIONINFOA;

but neither contains the full version info.

Also, for retrieving the name of the OS is there any other way apart from doing #ifdef __WIN32 checks?

alk
  • 69,737
  • 10
  • 105
  • 255
Courtney White
  • 612
  • 8
  • 22
  • 1
    look for https://stackoverflow.com/questions/39778525/how-to-get-windows-version-as-in-windows-10-version-1607 for example. – RbMm Aug 12 '18 at 10:52
  • Possibly an [XY Problem](http://xyproblem.info). What do you plan to do with that information? – IInspectable Aug 12 '18 at 11:21

2 Answers2

0

For <= Windows 8.1 use might like to use the API functions WinVersion or WinVersionEx

For newer windows version use the Version Helper functions.

alk
  • 69,737
  • 10
  • 105
  • 255
0

if want get the same result as cmd can run next code:

ULONG GetVersionRevision(PULONG Ubr)
{
    HKEY hKey;
    ULONG dwError = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_READ|KEY_WOW64_64KEY, &hKey);
    if (dwError == NOERROR)
    {
        ULONG Type;
        ULONG cb = sizeof(ULONG);
        dwError = RegQueryValueExW(hKey, L"UBR", 0, &Type, (PBYTE)Ubr, &cb);

        if (dwError == NOERROR)
        {
            if (Type != REG_DWORD || cb != sizeof(ULONG))
            {
                dwError = ERROR_GEN_FAILURE;
            }
        }
        RegCloseKey(hKey);
    }

    return dwError;
}

    ULONG M, m, b, Ubr;
    RtlGetNtVersionNumbers(&M, &m, &b);

    if (GetVersionRevision(&Ubr) == NOERROR)
    {
        DbgPrint("[Version %u.%u.%u.%u]\n", M, m, b & 0xffff, Ubr);
    }
    else
    {
        DbgPrint("[Version %u.%u.%u]\n", M, m, b & 0xffff);
    }
RbMm
  • 31,280
  • 3
  • 35
  • 56