怎样获得WINDOWS操作系统的版本号?(100分)

  • 主题发起人 主题发起人 williame
  • 开始时间 开始时间
W

williame

Unregistered / Unconfirmed
GUEST, unregistred user!
最好有例子
 
抄来的:
var
v:DWORD;
major,minor:BTYE;

v:=GetVersion();

if(v and 0x80000000)then
//is NT
else
begin
major:=BYTE(v);
minor:=WORD(v) shr 8;
if major<4 then
//win32s(win3.1)
else
begin
if major=4 then
begin
if(minor<1)then
//win95
else
//win98
end
else
//更高版,不知道什么东西了
end
end
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

const strOsType: array[0..7] of string=('osUnknown','osWin95','osWin98','osWin98se','osWinme','osWinnt4','osWin2k','osWinxp');
type OSType=(osUnknown,osWin95,osWin98,osWin98se,osWinme,osWinnt4,osWin2k,osWinxp);

var
Form1: TForm1;

implementation

{$R *.DFM}

Function GetOSVersion : OSType;
Var
osVerInfo : TOSVersionInfo;
majorVer, minorVer : Integer;
Begin

//Result := osUnknown;
osVerInfo.dwOSVersionInfoSize := SizeOf( TOSVersionInfo );
If ( GetVersionEx( osVerInfo ) ) Then
Begin
majorVer := osVerInfo.dwMajorVersion;
minorVer := osVerInfo.dwMinorVersion;
Case ( osVerInfo.dwPlatformId ) Of
VER_PLATFORM_WIN32_NT : { Windows NT/2000 }
Begin
If ( majorVer <= 4 ) Then
Result := osWinnt4
Else
If ( ( majorVer = 5 ) And ( minorVer= 0 ) ) Then
Result := osWin2k
Else
If ( ( majorVer = 5) And ( minorVer = 1 ) ) Then
Result := osWinxp
Else
Result := OsUnknown;
End;
VER_PLATFORM_WIN32_WINDOWS : { Windows 9x/ME }
Begin

If ( ( majorVer = 4 ) And ( minorVer = 0 ) ) Then
Result := osWin95
Else If ( ( majorVer = 4 ) And ( minorVer = 10 ) ) Then
Begin
If ( osVerInfo.szCSDVersion[ 1 ] = 'A' ) Then
Result := osWin98se
Else
Result := osWin98;
End
Else If ( ( majorVer = 4) And ( minorVer = 90 ) ) Then
Result := OsWinME
Else
Result := OsUnknown;

End;
Else
Result := OsUnknown;
End; //end of case
End Else
Result := OsUnknown;

End;

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(strOsType[integer(GetOSVersion)]);
end;

end.
 
抄的,保证正常

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
Panel1: TPanel;
StaticText1: TStaticText;
StaticText2: TStaticText;
StaticText3: TStaticText;
StaticText4: TStaticText;
StaticText5: TStaticText;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
OSVI:OSVERSIONINFO;
s:string;
is98orlater:boolean;
begin
OSVI.dwOSVersionInfoSize:=sizeof(OSVERSIONINFO);
//设置版本信息结构的大小
GetVersionEx(OSVI);
//获取版本信息
is98orlater:=
//判断是否98或以后版本
(osvi.dwPlatformId=VER_PLATFORM_WIN32_WINDOWS) and
((osvi.dwMajorVersion>4) or
((osvi.dwMajorVersion=4) and (osvi.dwMinorVersion>0)));

//下面开始显示信息
case OSVI.dwPlatformId of
//根据OSVI.dwPlatformId的数值的不同显示具体的平台描述
VER_PLATFORM_WIN32s:
// Windows 3.1平台
s:='Windows 3.1';
VER_PLATFORM_WIN32_WINDOWS:
// Windows 95/98平台
if(is98orlater) then
//98
s:='Windows 98'
else
//95
s:='Windows 95';
VER_PLATFORM_WIN32_NT:
// Windows NT平台
s:='Windows NT';
end;

Edit1.Text:=s;
Edit2.Text:=IntToStr(OSVI.dwMajorVersion);
Edit3.Text:=IntToStr(OSVI.dwMinorVersion);
case OSVI.dwPlatformId of
//根据平台的不同具体处理OSVI.dwBuildNumber信息
VER_PLATFORM_WIN32_WINDOWS:
// Windows 95/98平台则取OSVI.dwBuildNumber的低位字
Edit4.Text:=IntToStr(LOWORD(OSVI.dwBuildNumber));
VER_PLATFORM_WIN32_NT:
// Windows NT平台则取所有位的值
Edit4.Text:=IntToStr(OSVI.dwBuildNumber);
else
Edit4.Text:='';
// Windows 3.1平台此值位空
end;
Edit5.Text:=OSVI.szCSDVersion;
end;

end.
 
人家要获得WINDOWS操作系统的版本号,不是产品名称.不过产品名称也是可以获得的

function GetWindowsProductName:string;
begin
Result:=GetRegistryValue(HKEY_LOCAL_MACHINE,'/SOFTWARE/Microsoft/Windows NT/CurrentVersion','ProductName','');
if Result='' then Result:=GetRegistryValue(HKEY_LOCAL_MACHINE,'/SOFTWARE/Microsoft/Windows/CurrentVersion','ProductName','');
end;

function GetWindowsProductVersion:string;
begin
Result:=Format('%d.%d.%d',[Win32MajorVersion,Win32MinorVersion,Win32BuildNumber]);
end;

 
用API GetVersion ,不过结果被编码了:
Type
TGetVer = record
WinVer,
WinRev,
DosRev,
DosVer: Byte;
end;
const
VerStr = '%d.%d';
procedure TForm1.Button1Click(Sender: TObject);
var
AllVersions: TGetVer;
begin
AllVersions := TGetVer(GetVersion);
Edit1.Text := Format(VerStr, [AllVersions.WinVer, AllVersions.WinRev]);
Edit2.Text := Format(VerStr, [AllVersions.DOSVer, AllVersions.DOSRev]);
end;
注:返回的版本和真实版本未必相同;
Note2: Win32应用程序需要用GetVersionEx



--------------------------------------------------------------------------------
{$IFDEF WIN32}
function GetVersionEx(lpOs : pointer) : BOOL; stdcall;
external 'kernel32' name 'GetVersionExA';
{$ENDIF}

procedure GetWindowsVersion(var Major : integer;
var Minor : integer);
var
{$IFDEF WIN32}
lpOS, lpOS2 : POsVersionInfo;
{$ELSE}
l : longint;
{$ENDIF}
begin
{$IFDEF WIN32}
GetMem(lpOS, SizeOf(TOsVersionInfo));
lpOs^.dwOSVersionInfoSize := SizeOf(TOsVersionInfo);
while getVersionEx(lpOS) = false do begin
GetMem(lpos2, lpos^.dwOSVersionInfoSize + 1);
lpOs2^.dwOSVersionInfoSize := lpOs^.dwOSVersionInfoSize + 1;
FreeMem(lpOs, lpOs^.dwOSVersionInfoSize);
lpOS := lpOs2;
end;
Major := lpOs^.dwMajorVersion;
Minor := lpOs^.dwMinorVersion;
FreeMem(lpOs, lpOs^.dwOSVersionInfoSize);
{$ELSE}
l := GetVersion;
Major := LoByte(LoWord(l));
Minor := HiByte(LoWord(l));
{$ENDIF}
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Major : integer;
Minor : integer;
begin
GetWindowsVersion(Major, Minor);
Memo1.Lines.Add(IntToStr(Major));
Memo1.Lines.Add(IntToStr(Minor));
end;



--------------------------------------------------------------------------------
if Win32Platform = VER_PLATFORM_WIN32_WINDOWS then
ShowMessage('win9x');
if Win32Platform = VER_PLATFORM_WIN32_NT then
ShowMessage('winNT');



--------------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
var
OS : TOSVersionInfo;
begin
OS.dwOSVersionInfoSize := sizeof(OS);
GetVersionEx(OS);
with OS do
case dwPlatformId of
VER_PLATFORM_WIN32s :
label1.caption := 'Windows 3.1x/32s';
VER_PLATFORM_WIN32_WINDOWS :
Begin
if (dwMajorVersion = 4) and (dwMinorVersion > 0) then
label1.caption := 'Windows 98'
else
label1.caption := 'Windows 95';
end;
VER_PLATFORM_WIN32_NT :
label1.caption := 'Windows NT' ;
end;
end;



--------------------------------------------------------------------------------


Const

cOsUnknown : Integer = -1;

cOsWin95 : Integer = 0;

cOsWin98 : Integer = 1;

cOsWin98SE : Integer = 2;

cOsWinME : Integer = 3;

cOsWinNT : Integer = 4;

cOsWin2000 : Integer = 5;

cOsWhistler : Integer = 6;



Function GetOSVersion : Integer;

Function GetOSName( OSCode : Integer ) : String;



Function GetOSVersion : Integer;

Var

osVerInfo : TOSVersionInfo;

majorVer, minorVer : Integer;

Begin

Result := cOsUnknown;

osVerInfo.dwOSVersionInfoSize := SizeOf( TOSVersionInfo );

If ( GetVersionEx( osVerInfo ) ) Then Begin

majorVer := osVerInfo.dwMajorVersion;

minorVer := osVerInfo.dwMinorVersion;

Case ( osVerInfo.dwPlatformId ) Of

VER_PLATFORM_WIN32_NT : { Windows NT/2000 }

Begin

If ( majorVer <= 4 ) Then

Result := cOsWinNT

Else

If ( ( majorVer = 5 ) And ( minorVer= 0 ) ) Then

Result := cOsWin2000

Else

If ( ( majorVer = 5) And ( minorVer = 1 ) ) Then

Result := cOsWhistler

Else

Result := cOsUnknown;

End;

VER_PLATFORM_WIN32_WINDOWS : { Windows 9x/ME }

Begin

If ( ( majorVer = 4 ) And ( minorVer = 0 ) ) Then

Result := cOsWin95

Else If ( ( majorVer = 4 ) And ( minorVer = 10 ) ) Then Begin

If ( osVerInfo.szCSDVersion[ 1 ] = 'A' ) Then

Result := cOsWin98SE

Else

Result := cOsWin98;

End Else If ( ( majorVer = 4) And ( minorVer = 90 ) ) Then

Result := cOsWinME

Else

Result := cOsUnknown;

End;

Else

Result := cOsUnknown;

End;

End Else

Result := cOsUnknown;

End;



Function GetOSName( OSCode : Integer ) : String;

Begin

If ( OSCode = cOsUnknown ) Then

Result := '(sconosciuto)'

Else If ( OSCode = cOsWin95 ) Then

Result := 'Microsoft Windows 95'

Else If ( OSCode = cOsWin98 ) Then

Result := 'Microsoft Windows 98'

Else If ( OSCode = cOsWin98SE ) Then

Result := 'Microsoft Windows 98 Second Edition'

Else If ( OSCode = cOsWinME ) Then

Result := 'Microsoft Windows Millennium Edition'

Else If ( OSCode = cOsWinNT ) Then

Result := 'Microsoft Windows New Technology'

Else If ( OSCode = cOsWin2000 ) Then

Result := 'Microsoft Windows 2000 / NT 5'

Else

Result := 'Microsoft Windows';

End;


 
楼上说干了:(来晚了
其实你在MSDN收一下GetVersionEx就可以得到详细帮助。
 
天下文章一大抄,copy&Paste好骗分,
测试一下都不干,管他是对还是错。
 
别人要版本号,大家都给他判断操作系统的代码干什么?其实楼上大梦的第五种方法就
已经囊括了获取版本号以及操作系统类型的功能
 
多人接受答案了。
 
后退
顶部