supercharge icon indicating copy to clipboard operation
supercharge copied to clipboard

I think this solves OS version detection

Open rbwdenny opened this issue 5 years ago • 3 comments
trafficstars

This return version as a double: This technique uses a windows dll then get that dll version number.

double get_os()
{
	int ret = 0.0;
	NTSTATUS(WINAPI *RtlGetVersion)(LPOSVERSIONINFOEXW);
	OSVERSIONINFOEXW osInfo;

	*reinterpret_cast<FARPROC*>(&RtlGetVersion) = GetProcAddress(GetModuleHandleA("ntdll"), "RtlGetVersion");

	if (nullptr != RtlGetVersion)
	{
		osInfo.dwOSVersionInfoSize = sizeof osInfo;
		RtlGetVersion(&osInfo);
		ret = osInfo.dwMajorVersion;
	}
	return ret;
}

rbwdenny avatar Nov 26 '19 12:11 rbwdenny

This was the right method, Your function needed some tweaks which I added and now I can detect Windows 10 all the way down to 2000, Below Windows 2000, The output gets weird but that's okay as the proper minor Version is then returned.

Note : This function still needs some work.

std::string get_os()
{
	std::string os;
	std::ostringstream ds;
	int ret = 0.0;
	NTSTATUS(WINAPI *RtlGetVersion)(LPOSVERSIONINFOEXW);
	OSVERSIONINFOEXW osInfo;

	*reinterpret_cast<FARPROC*>(&RtlGetVersion) = GetProcAddress(GetModuleHandleA("ntdll"), "RtlGetVersion");

	if (nullptr != RtlGetVersion)
	{
		osInfo.dwOSVersionInfoSize = sizeof osInfo;
		RtlGetVersion(&osInfo);
		ret = osInfo.dwMajorVersion;
	}

	int mw = osInfo.dwMinorVersion;
	if(ret == 5){
		switch (mw)
		{
		case 0:
			// 5.0 = Windows 2000
			os = "Windows 2000";
			break;
		case 1:
			// 5.1 = Windows XP
			os = "Windows XP";
			break;
		
		case 2:
			os = "Windows XP Professional";
			break;
		
		default:
			ds.str(""); ds.clear(); 
			ds << "Windows " << mw;
			os = ds.str();
			break;
		}
	} else if(ret == 6){
		switch (mw)
		{
		case 0:
			os = "Windows Vista";
			break;
		case 1:
			os = "Windows 7";
			break;
		case 2:
			os = "Windows 8";
			break;
		case 3:
			os = "Windows 8.1";
			break;
		
		default:
			ds.str(""); ds.clear(); 
			ds << "Windows " << mw;
			os = ds.str();
			break;
		}
	} else if(ret == 10){
			os = "Windows 10";
	} else {
		ds.str(""); ds.clear(); 
		ds << "Windows " << mw;
		os = ds.str();
	}
	return os;
	
}

Thank you for your help! I should ask your permission before adding your name to contributors list, Shall I?

quantumcore avatar Nov 26 '19 15:11 quantumcore

Windows versions are available here; https://docs.microsoft.com/en-us/windows/win32/api/winnt/ns-winnt-osversioninfoexw

quantumcore avatar Nov 26 '19 15:11 quantumcore

Happy to help u along the way. dennyvsdev is my username.

rbwdenny avatar Nov 27 '19 08:11 rbwdenny