C++ How to Detect Windows 10

Detecting Windows 10 version

The most straight-forward way to retrieve the true OS version is to call RtlGetVersion. It is what GetVersionEx and VerifyVersionInfo call, but doesn't employ the compatibility shims.

You can either use the DDK (by #including <ntddk.h> and linking against NtosKrnl.lib from kernel mode, or ntdll.lib from user mode), or use runtime dynamic linking as in the following snippet:

typedef LONG NTSTATUS, *PNTSTATUS;
#define STATUS_SUCCESS (0x00000000)

typedef NTSTATUS (WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);

RTL_OSVERSIONINFOW GetRealOSVersion() {
HMODULE hMod = ::GetModuleHandleW(L"ntdll.dll");
if (hMod) {
RtlGetVersionPtr fxPtr = (RtlGetVersionPtr)::GetProcAddress(hMod, "RtlGetVersion");
if (fxPtr != nullptr) {
RTL_OSVERSIONINFOW rovi = { 0 };
rovi.dwOSVersionInfoSize = sizeof(rovi);
if ( STATUS_SUCCESS == fxPtr(&rovi) ) {
return rovi;
}
}
}
RTL_OSVERSIONINFOW rovi = { 0 };
return rovi;
}

In case you need additional information you can pass an RTL_OSVERSIONINFOEXW structure in place of the RTL_OSVERSIONINFOW structure (properly assigning the dwOSVersionInfoSize member).

This returns the expected result on Windows 10, even when there is no manifest attached.


As an aside, it is commonly accepted as a better solution to provide different implementations based on available features rather than OS versions.

Is there an *official* way to detect Windows 11?

The question thread "Windows 11 build ver is still 10.0.22000.194" on Microsoft Q&A has received responses from two Microsoft employees. In the accepted answer, one says:

At least for now, OS build number 22000 is the standard that distinguishes Windows 10 from Windows 11.

And, in a comment below the answer, another adds:

This is correct. Anything above 10.0.22000.0 is Win 11. Anything below is Win 10.

This suggests that, at the time of that writing, a week after the public release of Windows 11, Microsoft had not yet published an official recommendation for software developers. That may obviously change at some point, but until then the 22000 build number seems to be the criterion one should rely on to draw the line between Windows 10 and 11.

How can I detect if my app is running on Windows 10

If you look at registry you will found environment name:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName

For example my product name is Windows 10 Home:

Registry

With this code you get if it Windows 10:

 static bool IsWindows10()
{
var reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");

string productName = (string)reg.GetValue("ProductName");

return productName.StartsWith("Windows 10");
}

Note: Add using Microsoft.Win32; to your usings.

Detect Windows or Linux in C, C++

It's generally done like this (more or less):

#ifdef _WIN32
#include <windows.h>
#include <stdio.h>
#include <tchar.h>

#define DIV 1048576
#define WIDTH 7
#endif

#ifdef linux
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#endif


int main(int argc, char *argv[])
{
#ifdef _WIN32
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);

_tprintf (TEXT("There is %*ld %% of memory in use.\n"),
WIDTH, statex.dwMemoryLoad);
#endif

#ifdef linux
char cmd[30];
int flag = 0;
FILE *fp;
char line[130];
int TotalMem, TotalFree, TotalUsed;

flag=0;
memcpy (cmd,"\0",30);
sprintf(cmd,"free -t -m|grep Total");
fp = popen(cmd, "r");
while ( fgets( line, sizeof line, fp))
{
flag++;
sscanf(line,"%*s %d %d %d",&TotalMem, &TotalUsed, &TotalFree);
}
pclose(fp);

if(flag)
printf("TotalMem:%d -- TotalUsed:%d -- TotalFree:%d\n",TotalMem,TotalUsed,TotalFree);
else
printf("not found\n");
#endif

return 0;
}

This way, only code for linux will be compiled while on a linux platform, and only windows code will be compiled on a windows platform.

Detecting Windows 10 Edition using Qt5

If you looking for Windows 10 detection in general on Qt 5 (not the edition, but the simple fact you're running on Win 8.1 or Win 10), refer to my post here:
C++ How to detect Windows 10

How to detect Windows 10 light/dark mode in Win32 application?

Well, it looks like this option is not exposed to regular Win32 applications directly, however it can be set / retrieved through the AppsUseLightTheme key at the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize registry path.



Related Topics



Leave a reply



Submit