我知道我可以调用GetVersionEx Win32 API函数来检索Windows版本。在大多数情况下,返回值反映了我的Windows版本,但有时并非如此。
如果用户在兼容层下运行我的应用程序,则GetVersionEx将不会报告真实版本,而是报告兼容层强制执行的版本。例如,如果我正在运行Vista并在“Windows NT 4”兼容模式下执行我的程序,则GetVersionEx将不会返回版本6.0而是4.0。
有没有办法绕过这种行为并获得真正的Windows版本?
答案 0 :(得分:29)
我知道的最佳方法是检查是否从某些DLL导出特定的API。每个新的Windows版本都添加了新功能,通过检查这些功能的存在,可以判断运行应用程序的操作系统。例如,Vista从kernel32.dll导出GetLocaleInfoEx,而之前的Windowses没有。{/ p>
简而言之,这里有一个这样的列表,只包含来自kernel32.dll的导出。
> *function: implemented in* > GetLocaleInfoEx: Vista > GetLargePageMinimum: Vista, Server 2003 GetDLLDirectory: Vista, Server 2003, XP SP1 GetNativeSystemInfo: Vista, Server 2003, XP SP1, XP ReplaceFile: Vista, Server 2003, XP SP1, XP, 2000 OpenThread: Vista, Server 2003, XP SP1, XP, 2000, ME GetThreadPriorityBoost: Vista, Server 2003, XP SP1, XP, 2000, NT 4 IsDebuggerPresent: Vista, Server 2003, XP SP1, XP, 2000, ME, NT 4, 98 GetDiskFreeSpaceEx: Vista, Server 2003, XP SP1, XP, 2000, ME, NT 4, 98, 95 OSR2 ConnectNamedPipe: Vista, Server 2003, XP SP1, XP, 2000, NT 4, NT 3 Beep: Vista, Server 2003, XP SP1, XP, 2000, ME, 98, 95 OSR2, 95
编写函数来确定真正的操作系统版本很简单;从最新操作系统到最旧操作系统,并使用GetProcAddress检查导出的API。用任何语言实现这一点应该是微不足道的。
Delphi中的以下代码是从免费DSiWin32库中提取的:
TDSiWindowsVersion = (wvUnknown, wvWin31, wvWin95, wvWin95OSR2, wvWin98,
wvWin98SE, wvWinME, wvWin9x, wvWinNT3, wvWinNT4, wvWin2000, wvWinXP,
wvWinNT, wvWinServer2003, wvWinVista);
function DSiGetWindowsVersion: TDSiWindowsVersion;
var
versionInfo: TOSVersionInfo;
begin
versionInfo.dwOSVersionInfoSize := SizeOf(versionInfo);
GetVersionEx(versionInfo);
Result := wvUnknown;
case versionInfo.dwPlatformID of
VER_PLATFORM_WIN32s: Result := wvWin31;
VER_PLATFORM_WIN32_WINDOWS:
case versionInfo.dwMinorVersion of
0:
if Trim(versionInfo.szCSDVersion[1]) = 'B' then
Result := wvWin95OSR2
else
Result := wvWin95;
10:
if Trim(versionInfo.szCSDVersion[1]) = 'A' then
Result := wvWin98SE
else
Result := wvWin98;
90:
if (versionInfo.dwBuildNumber = 73010104) then
Result := wvWinME;
else
Result := wvWin9x;
end; //case versionInfo.dwMinorVersion
VER_PLATFORM_WIN32_NT:
case versionInfo.dwMajorVersion of
3: Result := wvWinNT3;
4: Result := wvWinNT4;
5:
case versionInfo.dwMinorVersion of
0: Result := wvWin2000;
1: Result := wvWinXP;
2: Result := wvWinServer2003;
else Result := wvWinNT
end; //case versionInfo.dwMinorVersion
6: Result := wvWinVista;
end; //case versionInfo.dwMajorVersion
end; //versionInfo.dwPlatformID
end; { DSiGetWindowsVersion }
function DSiGetTrueWindowsVersion: TDSiWindowsVersion;
function ExportsAPI(module: HMODULE; const apiName: string): boolean;
begin
Result := GetProcAddress(module, PChar(apiName)) <> nil;
end; { ExportsAPI }
var
hKernel32: HMODULE;
begin { DSiGetTrueWindowsVersion }
hKernel32 := GetModuleHandle('kernel32');
Win32Check(hKernel32 <> 0);
if ExportsAPI(hKernel32, 'GetLocaleInfoEx') then
Result := wvWinVista
else if ExportsAPI(hKernel32, 'GetLargePageMinimum') then
Result := wvWinServer2003
else if ExportsAPI(hKernel32, 'GetNativeSystemInfo') then
Result := wvWinXP
else if ExportsAPI(hKernel32, 'ReplaceFile') then
Result := wvWin2000
else if ExportsAPI(hKernel32, 'OpenThread') then
Result := wvWinME
else if ExportsAPI(hKernel32, 'GetThreadPriorityBoost') then
Result := wvWinNT4
else if ExportsAPI(hKernel32, 'IsDebuggerPresent') then //is also in NT4!
Result := wvWin98
else if ExportsAPI(hKernel32, 'GetDiskFreeSpaceEx') then //is also in NT4!
Result := wvWin95OSR2
else if ExportsAPI(hKernel32, 'ConnectNamedPipe') then
Result := wvWinNT3
else if ExportsAPI(hKernel32, 'Beep') then
Result := wvWin95
else // we have no idea
Result := DSiGetWindowsVersion;
end; { DSiGetTrueWindowsVersion }
---更新2009-10-09
事实证明,在Vista SP1及更高版本上进行“未记录的”操作系统检测变得非常困难。查看API changes表明所有Windows 2008功能也在Vista SP1中实现,并且所有Windows 7功能也在Windows 2008 R2中实现。太糟糕了:(
---更新结束
FWIW,这是我在实践中遇到的一个问题。我们(我工作的公司)有一个程序,当Vista发布时(以及之后几周......),它还没有真正适用于Vista。它也不在兼容层下工作。 (一些DirectX问题。不要问。)
我们不想让自己太聪明的用户在Vista上运行这个应用程序 - 兼容模式与否 - 所以我必须找到一个解决方案(一个比我聪明的人指出我正确的方向;上面的东西不是我的想法)。现在我发布它是为了你的乐趣,并帮助所有将来必须解决这个问题的可怜的灵魂。谷歌,请索引这篇文章!
如果您有更好的解决方案(或我的升级和/或修复),请在此处发布答案...
答案 1 :(得分:26)
WMI Query:
"Select * from Win32_OperatingSystem"
编辑:其实更好的是:
"Select Version from Win32_OperatingSystem"
您可以在Delphi中实现这一点,如下所示:
function OperatingSystemDisplayName: string;
function GetWMIObject(const objectName: string): IDispatch;
var
chEaten: Integer;
BindCtx: IBindCtx;
Moniker: IMoniker;
begin
OleCheck(CreateBindCtx(0, bindCtx));
OleCheck(MkParseDisplayName(BindCtx, PChar(objectName), chEaten, Moniker));
OleCheck(Moniker.BindToObject(BindCtx, nil, IDispatch, Result));
end;
function VarToString(const Value: OleVariant): string;
begin
if VarIsStr(Value) then begin
Result := Trim(Value);
end else begin
Result := '';
end;
end;
function FullVersionString(const Item: OleVariant): string;
var
Caption, ServicePack, Version, Architecture: string;
begin
Caption := VarToString(Item.Caption);
ServicePack := VarToString(Item.CSDVersion);
Version := VarToString(Item.Version);
Architecture := ArchitectureDisplayName(SystemArchitecture);
Result := Caption;
if ServicePack <> '' then begin
Result := Result + ' ' + ServicePack;
end;
Result := Result + ', version ' + Version + ', ' + Architecture;
end;
var
objWMIService: OleVariant;
colItems: OleVariant;
Item: OleVariant;
oEnum: IEnumvariant;
iValue: LongWord;
begin
Try
objWMIService := GetWMIObject('winmgmts:\\localhost\root\cimv2');
colItems := objWMIService.ExecQuery('SELECT Caption, CSDVersion, Version FROM Win32_OperatingSystem', 'WQL', 0);
oEnum := IUnknown(colItems._NewEnum) as IEnumVariant;
if oEnum.Next(1, Item, iValue)=0 then begin
Result := FullVersionString(Item);
exit;
end;
Except
// yes, I know this is nasty, but come what may I want to use the fallback code below should the WMI code fail
End;
(* Fallback, relies on the deprecated function GetVersionEx, reports erroneous values
when manifest does not contain supportedOS matching the executing system *)
Result := TOSVersion.ToString;
end;
答案 2 :(得分:10)
如何获取系统文件的版本?
最好的文件是kernel32.dll,位于%WINDIR%\ System32 \ kernel32.dll中。
有API可以获取文件版本。例如:我正在使用Windows XP - &gt; “5.1.2600.5512(xpsp.080413-2111)”
答案 3 :(得分:8)
另一种解决方案:
阅读以下注册表项:
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName
或
中的其他键HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion
答案 4 :(得分:5)
PEB过程信息块上的实际版本存储。
Win32应用程序示例(Delphi代码)
unit RealWindowsVerUnit;
interface
uses
Windows;
var
//Real version Windows
Win32MajorVersionReal: Integer;
Win32MinorVersionReal: Integer;
implementation
type
PPEB=^PEB;
PEB = record
InheritedAddressSpace: Boolean;
ReadImageFileExecOptions: Boolean;
BeingDebugged: Boolean;
Spare: Boolean;
Mutant: Cardinal;
ImageBaseAddress: Pointer;
LoaderData: Pointer;
ProcessParameters: Pointer; //PRTL_USER_PROCESS_PARAMETERS;
SubSystemData: Pointer;
ProcessHeap: Pointer;
FastPebLock: Pointer;
FastPebLockRoutine: Pointer;
FastPebUnlockRoutine: Pointer;
EnvironmentUpdateCount: Cardinal;
KernelCallbackTable: PPointer;
EventLogSection: Pointer;
EventLog: Pointer;
FreeList: Pointer; //PPEB_FREE_BLOCK;
TlsExpansionCounter: Cardinal;
TlsBitmap: Pointer;
TlsBitmapBits: array[0..1] of Cardinal;
ReadOnlySharedMemoryBase: Pointer;
ReadOnlySharedMemoryHeap: Pointer;
ReadOnlyStaticServerData: PPointer;
AnsiCodePageData: Pointer;
OemCodePageData: Pointer;
UnicodeCaseTableData: Pointer;
NumberOfProcessors: Cardinal;
NtGlobalFlag: Cardinal;
Spare2: array[0..3] of Byte;
CriticalSectionTimeout: LARGE_INTEGER;
HeapSegmentReserve: Cardinal;
HeapSegmentCommit: Cardinal;
HeapDeCommitTotalFreeThreshold: Cardinal;
HeapDeCommitFreeBlockThreshold: Cardinal;
NumberOfHeaps: Cardinal;
MaximumNumberOfHeaps: Cardinal;
ProcessHeaps: Pointer;
GdiSharedHandleTable: Pointer;
ProcessStarterHelper: Pointer;
GdiDCAttributeList: Pointer;
LoaderLock: Pointer;
OSMajorVersion: Cardinal;
OSMinorVersion: Cardinal;
OSBuildNumber: Cardinal;
OSPlatformId: Cardinal;
ImageSubSystem: Cardinal;
ImageSubSystemMajorVersion: Cardinal;
ImageSubSystemMinorVersion: Cardinal;
GdiHandleBuffer: array [0..33] of Cardinal;
PostProcessInitRoutine: Cardinal;
TlsExpansionBitmap: Cardinal;
TlsExpansionBitmapBits: array [0..127] of Byte;
SessionId: Cardinal;
end;
//Get PEB block current win32 process
function GetPDB: PPEB; stdcall;
asm
MOV EAX, DWORD PTR FS:[30h]
end;
initialization
//Detect true windows wersion
Win32MajorVersionReal := GetPDB^.OSMajorVersion;
Win32MinorVersionReal := GetPDB^.OSMinorVersion;
end.
答案 5 :(得分:4)
以下适用于Windows 10但没有应用程序清单中列出的Windows 10 GUID:
uses
System.SysUtils, Winapi.Windows;
type
NET_API_STATUS = DWORD;
_SERVER_INFO_101 = record
sv101_platform_id: DWORD;
sv101_name: LPWSTR;
sv101_version_major: DWORD;
sv101_version_minor: DWORD;
sv101_type: DWORD;
sv101_comment: LPWSTR;
end;
SERVER_INFO_101 = _SERVER_INFO_101;
PSERVER_INFO_101 = ^SERVER_INFO_101;
LPSERVER_INFO_101 = PSERVER_INFO_101;
const
MAJOR_VERSION_MASK = $0F;
function NetServerGetInfo(servername: LPWSTR; level: DWORD; var bufptr): NET_API_STATUS; stdcall; external 'Netapi32.dll';
function NetApiBufferFree(Buffer: LPVOID): NET_API_STATUS; stdcall; external 'Netapi32.dll';
type
pfnRtlGetVersion = function(var RTL_OSVERSIONINFOEXW): LONG; stdcall;
var
Buffer: PSERVER_INFO_101;
ver: RTL_OSVERSIONINFOEXW;
RtlGetVersion: pfnRtlGetVersion;
begin
Buffer := nil;
// Win32MajorVersion and Win32MinorVersion are populated from GetVersionEx()...
ShowMessage(Format('GetVersionEx: %d.%d', [Win32MajorVersion, Win32MinorVersion])); // shows 6.2, as expected per GetVersionEx() documentation
@RtlGetVersion := GetProcAddress(GetModuleHandle('ntdll.dll'), 'RtlGetVersion');
if Assigned(RtlGetVersion) then
begin
ZeroMemory(@ver, SizeOf(ver));
ver.dwOSVersionInfoSize := SizeOf(ver);
if RtlGetVersion(ver) = 0 then
ShowMessage(Format('RtlGetVersion: %d.%d', [ver.dwMajorVersion, ver.dwMinorVersion])); // shows 10.0
end;
if NetServerGetInfo(nil, 101, Buffer) = NO_ERROR then
try
ShowMessage(Format('NetServerGetInfo: %d.%d', [Buffer.sv101_version_major and MAJOR_VERSION_MASK, Buffer.sv101_version_minor])); // shows 10.0
finally
NetApiBufferFree(Buffer);
end;
end.
更新:NetWkstaGetInfo()
可能也有效,类似于&#39; NetServerGetInfo()`,但我还没有尝试过。
答案 6 :(得分:1)
注意: Gabr正在询问一种可以绕过GetVersionEx
限制的方法。 JCL代码使用GetVersionEx,因此受兼容性层的限制。此信息仅供不需要绕过兼容性层的人使用。
使用Jedi JCL,您可以添加单元JclSysInfo,并调用函数GetWindowsVersion
。它返回枚举类型TWindowsVersion。
目前JCL包含所有已发布的Windows版本,并且每次Microsoft在一个盒子中发布新版本的Windows时都会更改:
TWindowsVersion =
(wvUnknown, wvWin95, wvWin95OSR2, wvWin98, wvWin98SE, wvWinME,
wvWinNT31, wvWinNT35, wvWinNT351, wvWinNT4, wvWin2000, wvWinXP,
wvWin2003, wvWinXP64, wvWin2003R2, wvWinVista, wvWinServer2008,
wvWin7, wvWinServer2008R2);
如果您想知道您是在运行64位Windows 7而不是32位,请致电JclSysInfo.IsWindows64
。
请注意,JCL还可以处理版本,如Pro,Ultimate等。对于调用GetWindowsEdition,它会返回其中一个:
TWindowsEdition =
(weUnknown, weWinXPHome, weWinXPPro, weWinXPHomeN, weWinXPProN, weWinXPHomeK,
weWinXPProK, weWinXPHomeKN, weWinXPProKN, weWinXPStarter, weWinXPMediaCenter,
weWinXPTablet, weWinVistaStarter, weWinVistaHomeBasic, weWinVistaHomeBasicN,
weWinVistaHomePremium, weWinVistaBusiness, weWinVistaBusinessN,
weWinVistaEnterprise, weWinVistaUltimate, weWin7Starter, weWin7HomeBasic,
weWin7HomePremium, weWin7Professional, weWin7Enterprise, weWin7Ultimate);
对于历史兴趣,您也可以使用NtProductType函数检查NT级版本,它返回:
TNtProductType = (ptUnknown, ptWorkStation, ptServer, ptAdvancedServer,
ptPersonal, ptProfessional, ptDatacenterServer,
ptEnterprise, ptWebEdition);
请注意,上面检测到“N个版本”。这是由欧盟反垄断法规创建的欧盟(欧洲)版Windows。这是JCL内部非常精细的检测级别。
这是一个示例函数,可帮助您检测Vista,并在Vista上执行一些特殊操作。
function IsSupported:Boolean;
begin
case GetWindowsVersion of
wvVista: result := false;
else
result := true;
end;
end;
请注意,如果您想要“大于”检查,那么您应该使用其他技术。另请注意,版本检查通常可能是未来破损的来源。我通常选择警告用户并继续,以便我的二进制代码在将来不会成为破坏的实际来源。
最近我试图安装一个应用程序,安装程序检查了我的驱动器可用空间,并且无法安装,因为我有超过2千兆字节的可用空间。安装程序中的32位整数签名值变为负数,从而破坏了安装程序。我不得不将它安装到VM中以使其工作。添加“智能代码”通常会使您的应用程序“愚蠢”。要小心。
顺便说一句,我发现从命令行可以运行WMIC.exe,并输入path Win32_OperatingSystem
(“Select * from Win32_OperatingSystem”对我来说不起作用)。将来也许可以将JCL扩展为使用WMI信息。
答案 7 :(得分:1)
基本上回答重复的问题:Getting OS major, minor, and build versions for Windows 8.1 and up in Delphi 2007
从W2K开始,您可以使用NetServerGetInfo。 NetServerGetInfo在W7和W8.1上返回正确的信息,无法在W10上进行测试..
function GetWinVersion: string;
var
Buffer: PServerInfo101;
begin
Buffer := nil;
if NetServerGetInfo(nil, 101, Pointer(Buffer)) = NO_ERROR then
try
Result := <Build You Version String here>(
Buffer.sv101_version_major,
Buffer.sv101_version_minor,
VER_PLATFORM_WIN32_NT // Save since minimum support begins in W2K
);
finally
NetApiBufferFree(Buffer);
end;
end;
答案 8 :(得分:1)
关于使用NetServerGetInfo()的一个注意事项,它仍可在Windows 10(10240.th1_st1)上运行...
https://msdn.microsoft.com/en-us/library/windows/desktop/aa370903%28v=vs.85%29.aspx
sv101_version_major
主要版本号和服务器类型。
指定了操作系统的主要版本号 在最低有效4位。服务器类型在。中指定 最重要的4位。 MAJOR_VERSION_MASK中定义的位掩码 应用程序应使用Lmserver.h头{0x0F}来获取 该成员的主要版本号。
换句话说,(sv101_version_major&amp; MAJOR_VERSION_MASK)。