在系统目录C:\Windows\System32下有个程序ntoskrnl.exe,该程序的版本与系统版本一致,我们可以获取该程序的版本号,以此在我们的软件中获取系统的正确版本号,下面是Delphi的实现代码
type PVerInfo = ^TVS_FIXEDFILEINFO; TVS_FIXEDFILEINFO = record dwSignature: longint; dwStrucVersion: longint; dwFileVersionMS: longint; dwFileVersionLS: longint; dwFileFlagsMask: longint; dwFileFlags: longint; dwFileOS: longint; dwFileType: longint; dwFileSubtype: longint; dwFileDateMS: longint; dwFileDateLS: longint; end; var ExeNames: array[0..255] of char; zKeyPath: array[0..255] of Char; VerInfo: PVerInfo; Buf: pointer; Sz: word; L, Len: Cardinal; begin ExeNames :='C:\Windows\System32\ntoskrnl.exe'; Sz := GetFileVersionInfoSize(ExeNames, L); if Sz=0 then begin WindowVerson := -1; Exit; end; try GetMem(Buf, Sz); try GetFileVersionInfo(ExeNames, 0, Sz, Buf); if VerQueryValue(Buf, '\', Pointer(VerInfo), Len) then begin WindowVerson:=HIWORD(VerInfo.dwFileVersionMS); end; finally FreeMem(Buf); end; except WindowVerson := -1; end end;