VS2010中遇到_WIN32_WINNT not defined

本文介绍了如何解决在Visual Studio 2010编程过程中遇到的_WIN32_WINNT未定义的问题。通过在stdafx.h文件中添加特定宏定义来指定操作系统版本,确保程序正确编译。

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

 

VS2010中编程时遇到这个问题

_WIN32_WINNT not defined. Defaulting to _WIN32_WINNT_MAXVER (see WinSDKVer.h)

解决办法:

 

在stdafx.h中添加宏定义

#define _WIN32_WINNT 0x0502

备注:必须在stdafx.h中所有#include 文件之前添加此代码。

 

#ifndef _SYSTEM_DETECTOR_H_ #define _SYSTEM_DETECTOR_H_ #ifdef _WIN32 //#ifndef _WIN32_WINNT_WIN7 //#define _WIN32_WINNT_WIN7 0x0601 //#endif // !_WIN32_WINNT_WIN7 // 检查编译环境SDK是否支持 //#if (_WIN32_WINNT < _WIN32_WINNT_WIN7) //#error "This application requires Windows SDK 7.0 or higher. Please update your Windows SDK." //#error "此应用程序需要 Windows SDK 7.0 或更高版本进行编译." //#endif #define _CRT_SECURE_NO_WARNINGS #define NO_WARN_MBCS_MFC_DEPRECATION #include <SDKDDKVer.h> #include <afxwin.h> //#include <versionhelpers.h> namespace D2DRENDER { // 枚举操作系统平台类型 enum class OSPlatform { Windows7, Windows8, Windows8_1, Windows10, Windows11, Unknown }; // 系统检测类 class CSystemDetector { public: CSystemDetector(); OSPlatform m_OSPlatform; int m_nCurrentBuild; }; extern CSystemDetector g_SystemDetector; //以下函数都是读取, 不用加锁, 线程安全 //判断是Windows 11 bool IsWin11(); // 判断是Windows 10 bool IsWin10(); // 判断是Windows 8.1 bool IsWin8_1(); // 判断是否Windows 8 bool IsWin8(); // 判断是否Windows 7 bool IsWin7(); // 判断是否win8.1 或以上系统 bool IsWin8_1orGreater(); } #endif // !_WIN32 #endif // !_SYSTEM_DETECTOR_H_ #include "SystemDetector.h" #pragma comment(linker, "/DELAYLOAD:dcomp.dll")//延迟加载dcomp.dll, 运行时才加载,配合LoadLibrary就能知道当前系统有没有dcomp.dll了 D2DRENDER::CSystemDetector g_SystemDetector; D2DRENDER::CSystemDetector::CSystemDetector() :m_OSPlatform(OSPlatform::Unknown), m_nCurrentBuild(0) { // 检测当前操作系统并返回平台类型 HKEY hKey; LONG lResult = RegOpenKeyEx( HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_READ, &hKey); if (lResult == ERROR_SUCCESS) { DWORD dwType; DWORD dwSize = 16; char szCurrentBuild[16] = { 0 }; RegQueryValueEx(hKey, "CurrentBuild", NULL, &dwType, (LPBYTE)szCurrentBuild, &dwSize); RegCloseKey(hKey); m_nCurrentBuild = atoi(szCurrentBuild); } if (m_nCurrentBuild >= 7600 && m_nCurrentBuild < 9200) { m_OSPlatform = OSPlatform::Windows7; } else if (m_nCurrentBuild >= 9200 && m_nCurrentBuild < 9600) { m_OSPlatform = OSPlatform::Windows8; } else if (m_nCurrentBuild >= 9600 && m_nCurrentBuild < 10240) { m_OSPlatform = OSPlatform::Windows8_1; } else if (m_nCurrentBuild >= 10240 && m_nCurrentBuild < 22000) { m_OSPlatform = OSPlatform::Windows10; } else if (m_nCurrentBuild >= 22000) { m_OSPlatform = OSPlatform::Windows11; } else { m_OSPlatform = OSPlatform::Unknown;//win7 以下都是未知系统 } } //判断是Windows 11 bool D2DRENDER::IsWin11() { return g_SystemDetector.m_OSPlatform == OSPlatform::Windows11; } // 判断是Windows 10 bool D2DRENDER::IsWin10() { return g_SystemDetector.m_OSPlatform == OSPlatform::Windows10; } // 判断是Windows 8.1 bool D2DRENDER::IsWin8_1() { return g_SystemDetector.m_OSPlatform == OSPlatform::Windows8_1; } // 判断是否Windows 8 bool D2DRENDER::IsWin8() { return g_SystemDetector.m_OSPlatform == OSPlatform::Windows8; } // 判断是否Windows 7 bool D2DRENDER::IsWin7() { return g_SystemDetector.m_OSPlatform == OSPlatform::Windows7; } // 判断是否win8.1 或以上系统 bool D2DRENDER::IsWin8_1orGreater() { return g_SystemDetector.m_nCurrentBuild >= 9600; }
最新发布
08-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值