【vs2015】Windows NT实现系统版本识别

本文介绍了一个用于检测不同版本Windows操作系统的程序。通过调用RtlGetVersion函数获取操作系统版本信息,并根据主要和次要版本号判断具体Windows版本。该程序支持从Windows 2000到Windows 10的不同版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.原理Windows NT

Microsoft Windows 2000 (Windows NT 5.0) (1999) (2000-2010) 10年
Microsoft Windows XP (Windows NT 5.1) (2001-2014.4.8) 13年
Microsoft Windows Server 2003 (Windows NT 5.2) (2003-2015) 12年
Microsoft Windows Server 2003 R2 (Windows NT 5.2) (2006-2015) 9年
Microsoft Windows Vista (Windows NT 6.0) (2006-2017) 11年
Microsoft Windows Server 2008 (Windows NT 6.0) (2008-2018) 10年
Microsoft Windows 7 (Windows NT 6.1) (2009-2020) 11年
Microsoft Windows Server 2008 R2 (Windows NT 6.1) (2009-2018) 9年
Microsoft Windows 8.0 (Windows NT 6.2) (2012-2016) 4年
Microsoft Windows Server 2012(Windows NT 6.2) (2012-2023) 11年
Microsoft Windows Phone 8 (Windows NT 6.2) (2012-2014) 2年[2] 
Microsoft Windows 8.1 (未安装更新的)(Windows NT 6.3) (2013-2014) 1年
Microsoft Windows 8.1 (已安装更新的)(Windows NT 6.3) (2013-2023)10年
Microsoft Windows Server 2012 R2 (Windows NT 6.3) (2013-2023) 11年
Microsort Windows 10【Windows NT 10.0

2.VS2015 新建项目->win32 控制台应用程序

以下操作实现跨平台
(1).配置属性-常规-MFC的使用->使用标准Windows库或者在静态库中使用MFC
(2).配置属性-c/c++-代码生成-运行库->多线程(/MT)

3.头文件

(1).stdafx.h
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//

#pragma once
//#define _AFXDLL
#include "targetver.h"

#include <stdio.h>
#include <tchar.h>


// TODO:  在此处引用程序需要的其他头文件

#include <afxwin.h>         // MFC core and standard components
#include <SetupAPI.h>
#pragma comment(lib, "Setupapi.lib")
(2).targetver.h保持不变
(3).新建AutorunDlg.h
#pragma once
#include "afxwin.h"
class CAutorunDlg : public CDialog
{
public:
	LPTSTR GetOperatorSystemVer(void);
	BOOL GetOsVersion(RTL_OSVERSIONINFOEXW* pk_OsVer);
};


4.主程序ConsoleApplication1.cpp

#include "StdAfx.h"
//#include "GetLanaguagAndOsver.h"
#pragma comment(lib,"Kernel32.lib")
#include <Mmsystem.h> 
#pragma comment(lib,"winmm.lib")
#include "AutorunDlg.h"
#include <afxwin.h>
//#pragma warning(disable:4996)
LPTSTR CAutorunDlg::GetOperatorSystemVer(void)
{
	//LPTSTR os;
	LPTSTR os = (LPTSTR)new char[20];
	ZeroMemory(os,40);
	RTL_OSVERSIONINFOEXW os_ver;
	GetOsVersion(&os_ver);
	if ((os_ver.dwMajorVersion == 5) && (os_ver.dwMinorVersion == 0))
	{
		os = _T("2000");
	}
	else if ((os_ver.dwMajorVersion == 5) && (os_ver.dwMinorVersion == 1))
	{	
		os = _T("xp");
	}
	else if ((os_ver.dwMajorVersion == 5) && (os_ver.dwMinorVersion == 2))
	{
		os = _T("2003 or xp64");//2003  // xp64 ,		
	}
	else if ((os_ver.dwMajorVersion == 6) && (os_ver.dwMinorVersion == 0))
	{
		os = _T("VISTA");;//AfxMessageBox(_T("VISTA"));
	}
	else if ((os_ver.dwMajorVersion == 6) && (os_ver.dwMinorVersion == 1))
	{
		//AfxMessageBox(_T("WIN7"));
		os = _T("win7");
	}
	else if ((os_ver.dwMajorVersion == 6) && (os_ver.dwMinorVersion == 2))
	{
		os = _T("WIN8");	//AfxMessageBox(_T("WIN8"));
	}
	else if ((os_ver.dwMajorVersion == 6) && (os_ver.dwMinorVersion == 3))
	{
		os = _T("WIN81");	//AfxMessageBox(_T("WIN81"));
	}
	else if ((os_ver.dwMajorVersion == 10) && (os_ver.dwMinorVersion == 0))
	{
		os = _T("WIN10");//AfxMessageBox(_T("WIN10"));
		//printf("%d", iVer);
	}

	return os;
}

BOOL CAutorunDlg::GetOsVersion(RTL_OSVERSIONINFOEXW* pk_OsVer)
{
	typedef LONG(WINAPI* tRtlGetVersion)(RTL_OSVERSIONINFOEXW*);

	memset(pk_OsVer, 0, sizeof(RTL_OSVERSIONINFOEXW));
	pk_OsVer->dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOEXW);

	HMODULE h_NtDll = GetModuleHandleW(L"ntdll.dll");
	tRtlGetVersion f_RtlGetVersion = (tRtlGetVersion)GetProcAddress(h_NtDll, "RtlGetVersion");

	if (!f_RtlGetVersion)
		return FALSE; // This will never happen (all processes load ntdll.dll)

	LONG Status = f_RtlGetVersion(pk_OsVer);
	return Status == 0; // STATUS_SUCCESS;
}

	int main()
	{
		CAutorunDlg OSver;
		LPTSTR system;
		system = OSver.GetOperatorSystemVer();
		printf("the system is %S\n", system);
		//getchar();			
	return  0;
}


5.效果


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值