码农的笔记

Delphi虽好,但已不流行; 博客真好,可以做笔记

博客园 首页 新随笔 联系 订阅 管理

 

--------------------D7

----抄来的,哈哈

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

 

---------------Unit

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,Registry,comobj,ActiveX ;

type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
uses WindowsSysVersion;
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
version:TWinVer;

TPPrev, TP: TTokenPrivileges;
TokenHandle: THandle;
dwRetLen: DWORD;
lpLuid: TLargeInteger;
begin

{OpenProcessToken(GetCurrentProcess, TOKEN_ALL_ACCESS, TokenHandle);
if (LookupPrivilegeValue(nil, 'SeDebugPrivilege', lpLuid)) then
begin
TP.PrivilegeCount := 1;
TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
TP.Privileges[0].Luid := lpLuid;
AdjustTokenPrivileges(TokenHandle, False, TP, SizeOf(TPPrev), TPPrev, dwRetLen);
if GetLastError<>ERROR_SUCCESS then
ShowMessage('權限獲取失敗!!');
end;
CloseHandle(TokenHandle);}

//if GetSystemDefaultLangID=3082 then
//Edit1.Text :=IntToStr(GetSystemDefaultLangID) ;
version:=GetWindowsSystemVersion;
if version=Win7 then
begin
Edit1.Text:='Win7';
end
else if version=Win10 then
begin
Edit1.Text:='Win10';
end
end;

procedure TForm1.Button2Click(Sender: TObject);
var
mm:TRegistry;
cpu:string;
begin
mm:=TRegistry.Create;
with mm do
begin
RootKey:=HKEY_LOCAL_MACHINE;
if OpenKeyReadOnly('\Hardware\Description\System\CentralProcessor\0') then
begin
cpu := ReadString('ProcessorNameString');
Memo1.Lines.Add('处理器:'+cpu);
end;
end;
//Memo1.Lines.Add('系统版本:'+TOSVersion.ToString);
end;

function GetWMIProperty(WMIType, WMIProperty: string): string;
const
WbemUser ='';
WbemPassword ='';
WbemComputer ='localhost';
var
FSWbemLocator, FWMIService, FWbemObjectSet, Obj: OleVariant;
C: Cardinal;
i,Len:integer;
tempItem:IEnumVariant;
count : integer;
msg : string;
begin
try
result := '';
FSWbemLocator:= CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);
FWbemObjectSet := FWMIService.ExecQuery('Select * from Win32_' + WMIType);
tempItem := IEnumVariant(IUnknown(FWbemObjectSet._NewEnum));
Result:='';
count := 0;
while (tempItem.Next(1, obj, c) = S_OK) do
begin
Obj := Obj.Properties_.Item(WMIProperty, 0).Value;
if not VarIsNull(obj) then
begin
if(count > 0) then
result := result + ',';
Result := Result + trim(Obj);
Inc(count);
end;
end;
except
On E : Exception do
begin
msg := Format('GetWMIProperty Error,WMIType:%s, WMIProperty:%s, Msg:%s',
[WMIType, WMIProperty, E.Message ]);//E.ToString
ShowMessage(msg);
end;
end;
if(lowercase(result) = 'none') then
result := '';
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
Edit1.Text:=GetWMIProperty('OperatingSystem', 'Version');
end;

procedure wbNewWindow3(ASender: TObject; var ppDisp: IDispatch;
var Cancel: WordBool; dwFlags: Cardinal; const bstrUrlContext,
bstrUrl: WideString);
begin
// dwFlags <=4 for auto popup
Cancel := dwFlags <= 4;
end;

end.

--------------------unit结束

 

-----------Form

object Form1: TForm1
Left = 549
Top = 280
Width = 523
Height = 410
Caption = '获取操作系统版本'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Edit1: TEdit
Left = 40
Top = 32
Width = 257
Height = 21
ImeName = '中文(简体) - 搜狗拼音输入法'
TabOrder = 0
end
object Button1: TButton
Left = 72
Top = 80
Width = 97
Height = 25
Caption = '获取Win版本'
TabOrder = 1
Visible = False
OnClick = Button1Click
end
object Button2: TButton
Left = 24
Top = 144
Width = 75
Height = 25
Caption = 'Button2'
Constraints.MaxHeight = 25
Constraints.MaxWidth = 75
Constraints.MinHeight = 25
Constraints.MinWidth = 75
TabOrder = 2
Visible = False
OnClick = Button2Click
end
object Memo1: TMemo
Left = 16
Top = 120
Width = 185
Height = 17
ImeName = '中文(简体) - 搜狗拼音输入法'
Lines.Strings = (
'Memo1')
TabOrder = 3
Visible = False
end
object Button3: TButton
Left = 200
Top = 80
Width = 105
Height = 25
Caption = '获取版本号'
TabOrder = 4
OnClick = Button3Click
end
object Button4: TButton
Left = 256
Top = 320
Width = 75
Height = 25
Caption = 'Button4'
TabOrder = 5
end
end

------------------Form结束

 

 

 

 

 

------------------------------------抄来的--------------

unit WindowsSysVersion;

interface

uses
windows ;
{$IFDEF CONDITIONALEXPRESSIONS}
{$IF Defined(TOSVersionInfoEx)}
{$DEFINE TOSVERSIONINFOEX_DEFINED}
{$IFEND}
{$ENDIF}
{$IFNDEF TOSVERSIONINFOEX_DEFINED}
type
POSVersionInfoEx = ^TOSVersionInfoEx;

TOSVersionInfoEx = packed record
dwOSVersionInfoSize: DWORD;
dwMajorVersion: DWORD;
dwMinorVersion: DWORD;
dwBuildNumber: DWORD;
dwPlatformId: DWORD;
szCSDVersion: array[0..127] of AnsiChar;
wServicePackMajor: Word;
wServicePackMinor: Word;
wSuiteMask: Word;
wProductType: Byte;
wReserved: Byte;
end;
type
TWinVer = (WinNone, Win95, Win98, WinMe, Win2000, WinServer2000, WinXp, WinXp64, WinServer2003, WinHomeServer, WinServer2003R2, WinVista, WinServer2008, WinServer2008R2, Win7,Win10);

const
VER_SERVER_NT = $80000000;
{$EXTERNALSYM VER_SERVER_NT}
VER_WORKSTATION_NT = $40000000;
{$EXTERNALSYM VER_WORKSTATION_NT}
VER_SUITE_SMALLBUSINESS = $00000001;
{$EXTERNALSYM VER_SUITE_SMALLBUSINESS}
VER_SUITE_ENTERPRISE = $00000002;
{$EXTERNALSYM VER_SUITE_ENTERPRISE}
VER_SUITE_BACKOFFICE = $00000004;
{$EXTERNALSYM VER_SUITE_BACKOFFICE}
VER_SUITE_COMMUNICATIONS = $00000008;
{$EXTERNALSYM VER_SUITE_COMMUNICATIONS}
VER_SUITE_TERMINAL = $00000010;
{$EXTERNALSYM VER_SUITE_TERMINAL}
VER_SUITE_SMALLBUSINESS_RESTRICTED = $00000020;
{$EXTERNALSYM VER_SUITE_SMALLBUSINESS_RESTRICTED}
VER_SUITE_EMBEDDEDNT = $00000040;
{$EXTERNALSYM VER_SUITE_EMBEDDEDNT}
VER_SUITE_DATACENTER = $00000080;
{$EXTERNALSYM VER_SUITE_DATACENTER}
VER_SUITE_SINGLEUSERTS = $00000100;
{$EXTERNALSYM VER_SUITE_SINGLEUSERTS}
VER_SUITE_PERSONAL = $00000200;
{$EXTERNALSYM VER_SUITE_PERSONAL}
VER_SUITE_BLADE = $00000400;
{$EXTERNALSYM VER_SUITE_BLADE}
VER_SUITE_EMBEDDED_RESTRICTED = $00000800;
{$EXTERNALSYM VER_SUITE_EMBEDDED_RESTRICTED}
VER_SUITE_SECURITY_APPLIANCE = $00001000;
{$EXTERNALSYM VER_SUITE_SECURITY_APPLIANCE}
VER_SUITE_WH_SERVER = $00008000;
{$EXTERNALSYM VER_SUITE_WH_SERVER}
PROCESSOR_ARCHITECTURE_AMD64 = 9;
{$EXTERNALSYM PROCESSOR_ARCHITECTURE_AMD64}
SM_SERVERR2 = 89;
{$EXTERNALSYM SM_SERVERR2}
const
VER_NT_WORKSTATION = $0000001;
{$EXTERNALSYM VER_NT_WORKSTATION}
VER_NT_DOMAIN_CONTROLLER = $0000002;
{$EXTERNALSYM VER_NT_DOMAIN_CONTROLLER}
VER_NT_SERVER = $0000003;
{$EXTERNALSYM VER_NT_SERVER}

{$ENDIF} // TOSVERSIONINFOEX_DEFINED

//取操作系统信息填充到结构
function GetOSVersionInfo(var Info: TOSVersionInfoEx): Boolean;
//windows系统类型 0表示取不到 1表示非服务器 2表示服务器
function GetWindowsSystemType: integer;
//取windows系统版本信息,主函数
function GetWindowsSystemVersion: Twinver;

implementation

{
OSVersionInfoEx.wProductType 类型说明

代码 值 说明
---------------------------------------------------------------------------------------------------------------------------------------
VER_NT_DOMAIN_CONTROLLER 0x0000002 装的是个域服务器系统(win2000server,2003server,2008server)
VER_NT_SERVER 0x0000003 装的是服务器系统(win2000server,2003server,2008server)
VER_NT_WORKSTATION 0x0000001 非服务器版本(Vista, XP Professional, XP Home Edition, 2000)
}
//取操作系统类型 0未取到或出错 1表示非服务器 2表示服务器

function GetWindowsSystemType: integer;
var
info: TOSVersionInfoEx;
begin
result := 0;
if (GetOSVersionInfo(info) = false) then exit;
case info.wProductType of
VER_NT_WORKSTATION:
begin
Result := 1; //非服务器
end;
VER_NT_SERVER:
begin
Result := 2; //服务器版
end;
VER_NT_DOMAIN_CONTROLLER:
begin
Result := 2; //域服务器
end;
end;
end;


//系统 版本号 其它条件
//-------------------------------------------------------------------------------------------------------
//Windows 7 6 1 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
//Windows Server 2008 R2 6 1 OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION
//Windows Server 2008 6 0 OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION
//Windows Vista 6 0 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION
//Windows Server 2003 R2 5 2 GetSystemMetrics(SM_SERVERR2) != 0
//Windows Home Server 5 2 OSVERSIONINFOEX.wSuiteMask == VER_SUITE_WH_SERVER
//Windows Server 2003 5 2 GetSystemMetrics(SM_SERVERR2) == 0
//Windows XP x64 Edition 5 2 (OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION) && (SYSTEM_INFO.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
//Windows XP 5 1
//Windows 2000 5 0
//Windows Me 4.9
//Windows 98 4.1
//Windows 95 4.0
//取windows系统版本信息

function GetWindowsSystemVersion: Twinver;
var
info: TOSVersionInfoEx;
sysInfo: Tsysteminfo;
begin
Result := WinNone;
windows.GetSystemInfo(sysInfo); //系统信息
try
if (GetOSVersionInfo(info) = false) then exit;
case info.dwMajorVersion of //主版本
4:
begin
case info.dwMinorVersion of //次版本
0: Result := Win95;
1: Result := Win98;
9: Result := WinMe;
end;
end;
5: begin
case info.dwMinorVersion of
0:
begin
if info.wProductType = VER_NT_WORKSTATION then
Result := Win2000 else Result := WinServer2000;
end;
1: Result := WinXp;
2:
begin
if ((info.wProductType = VER_NT_WORKSTATION) and (sysinfo.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_AMD64)) then //PROCESSOR_ARCHITECTURE_AMD64
Result := WinXp64;
//SM_SERVERR2
if GetSystemMetrics(SM_SERVERR2) = 0 then
Result := WinServer2003
else
Result := WinServer2003R2;
if info.wSuiteMask = VER_SUITE_WH_SERVER then
Result := WinHomeServer;
end;
end;
end;
6: begin
case info.dwMinorVersion of
0:
begin
if info.wProductType = VER_NT_WORKSTATION then
Result := WinVista else Result := WinServer2008;
end;
1:
begin
if info.wProductType = VER_NT_WORKSTATION then
Result := Win7 else Result := WinServer2008R2;
end;
end;
end;
end;
except
exit;
end;
end;

function GetOSVersionInfo(var Info: TOSVersionInfoEx): Boolean;
begin
FillChar(Info, SizeOf(TOSVersionInfoEx), 0);
Info.dwOSVersionInfoSize := SizeOf(TOSVersionInfoEx);
Result := GetVersionEx(TOSVersionInfo(Addr(Info)^));
if (not Result) then
Info.dwOSVersionInfoSize := 0;
end;
end.

 

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

 

 

 win10的不一样,获取的方式不一样

 

posted on 2021-04-07 10:22  码农的笔记  阅读(732)  评论(3编辑  收藏  举报