幾個獲取Windows系統信息的Delphi程序

謝建華

本文所有的窗體界面略去,讀者可根據程序自行添加各窗口組件。
1、獲取windows版本信息
可以通過Windows API函數GetVersionEx來獲得。
具體程序如下:
Procedure Tform1.Button1Click(sender:TObject);
Var
OSVI:OSVERSIONINFO;
begin
OSVI.dwOSversioninfoSize:=Sizeof(OSVERSIONINFO);
GetVersionEx(OSVI);
label1.Caption:=IntToStr(OSVI.dwMinorVersion)+','
+IntToStr(OSVI.dwMinorVersion)+','
+IntToStr(OSVI.dwBuildNumber)+','
+IntToStr(OSVI.dwPlatformId)+','
+OSVI.szCSDVersion;
end;

end.

2、獲取CPU信息
可以通過Windows API函數GetSystemInfo來獲得有關信息。
具體程序如下:
procedure TForm1.Button1Click(Sender: TObject);
Var
SysInfo:SYSTEM_INFO;
begin
GetSystemInfo(Sysinfo);
Edit1.Text:='系統中有'+IntToStr(Sysinfo.dwNumberOfProcessors)+'個CPU'
+',類型爲'+IntToStr(Sysinfo.dwProcessorType);
end;

end.

3、獲取內存信息
可以通過Windows API函數GlobalMemoryStatus來獲得內存信息。
具體程序如下:
procedure TForm1.Button1Click(Sender: TObject);
Var
MemInfo:MEMORYSTATUS;
begin
MemInfo.dwLength:=sizeof(MEMORYSTATUS);
GlobalMemoryStatus(MemInfo);
memo1.Lines.Add(IntToStr(MemInfo.dwMemoryLoad)+'%的內存正在使用') ;
memo1.Lines.Add('物理內存共有'+IntToStr(MemInfo.dwTotalPhys)+'字節');
memo1.Lines.Add('可使用的物理內存有'+IntToStr(MemInfo.dwAvailPhys)+'字節');
memo1.Lines.Add('交換文件總大小爲'+IntToStr(MemInfo.dwTotalPageFile)+'字節') ;
memo1.Lines.Add('尚可交換文件大小爲'+IntToStr(MemInfo.dwAvailPageFile)+'字節');
memo1.Lines.Add('總虛擬內存有'+IntToStr(MemInfo.dwTotalVirtual)+'字節');
memo1.Lines.Add('未用虛擬內存有'+IntToStr(MemInfo.dwAvailVirtual)+'字節');
end;

end.

或用以下代碼:
memo1.Text:=IntToStr(MemInfo.dwMemoryLoad)+'%的內存正在使用'+#13#10
+'可使用的物理內存有'+IntToStr(MemInfo.dwAvailPhys)+'字節'+#13#10
+'交換文件總大小爲'+IntToStr(MemInfo.dwTotalPageFile)+'字節'+#13#10
+'尚可交換文件大小爲'+IntToStr(MemInfo.dwAvailPageFile)+'字節'+#13#10
+'總虛擬內存有'+IntToStr(MemInfo.dwTotalVirtual)+'字節'+#13#10
+'未用虛擬內存有'+IntToStr(MemInfo.dwAvailVirtual)+'字節';
來替代memo1.line.add(…)部分。

4、獲取Windows和系統路徑
可以通過Windows API函數來獲得
具體程序如下:
procedure TForm1.Button1Click(Sender: TObject);
Var
SysDir:array[0..128] of char;
begin
GetWindowsDirectory(SysDir,128);
Edit1.Text:='Windows 路徑:'+SysDir;
GetSystemDirectory(SysDir,128);
Edit1.Text:=Edit1.Text+'; 系統路徑:'+SysDir;
end;

end.
其中,筆者通過更改數列的值:發現其中的128可更改爲人以不小於16的的數值,若小於或等於16均出現異常(筆者的操作系統爲Windows2000)。讀者朋友不妨試試。

5、獲取用戶註冊信息
我們都知道,一般在軟件安裝過程中,它都會提示用戶,要求輸入系列號或產品號和用戶的一些註冊信息(用戶的公司名稱、用戶名等)以及安裝的目錄和路徑等。
通過以下代碼可查看用戶註冊信息:
procedure TForm1.Button1Click(Sender: TObject);
Var
Reg:TRegistry;
begin
Reg:=TRegistry.Create;
Reg.RootKey:=HKEY_LOCAL_MACHINE;
Reg.OpenKey('SoftwareMicrosoftWindows NTCurrentVersion',False);
Edit1.Text:='當前路徑:'+Reg.CurrentPath;
Edit2.Text:='產品系列號:'+Reg.ReadString('ProductId');
Edit3.Text:='產品名:'+Reg.ReadString('ProductName');
Edit4.Text:='註冊公司名稱:'+Reg.ReadString('RegisteredOrganization');
Edit5.Text:='用戶名:'+Reg.ReadString('RegisteredOwner');
Edit6.Text:='軟件類型:'+Reg.ReadString('SoftwareType');
Reg.CloseKey;
Reg.Free;
end;

end.
注意:在程序編譯之前,必須在USES語句下添加registry單元。

6、關閉Widows
可以通過Windows API函數ExitWindowsEx來關閉Widows。
procedure TForm1.Button1Click(Sender: TObject);
begin
if RadioButton1.Checked=true then
ExitWindowsEx(EWX_LOGOFF,0) //以其他用戶身份登錄
else if RadioButton2.Checked=true then
ExitWindowsEx(EWX_SHUTDOWN,1) //安全關機
else if RadioButton3.Checked=true then
ExitWindowsEx(EWX_REBOOT,2) //重新啓動計算機
else if RadioButton4.Checked=true then
ExitWindowsEx(EWX_FORCE,4) //強行關機
else if RadioButton5.Checked=true then
ExitWindowsEx(EWX_POWEROFF,8); //關閉系統並關閉電源

end;

end.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章