VB中調用Windows API函數檢測當前系統環境
發表于:2007-07-14來源:作者:點擊數:
標簽:
楊洪勇 (山東農業大學) 摘要 本文介紹了在Visual Basic中用 Declare語句聲明所要調用的Windows API的方法,以及如何調用API函數來檢測當前的系統環境。用Visual Basic能夠直接控制和處理計算機的系統參數和硬件資源,增加了程序設計人員在Windows環境中 開
楊洪勇
(山東農業大學)
摘要 本文介紹了在Visual Basic中用 Declare語句聲明所要調用的Windows API的方法,以及如何調用API函數來檢測當前的系統環境。用Visual Basic能夠直接控制和處理計算機的系統參數和硬件資源,增加了程序設計人員在Windows環境中
開發軟件的靈活性,使軟件與
Windows系統達到了最完美的結合。本文最后給出了一個調用的Windows API的檢測系統環境實例。
關鍵字 Visual Basic Declare 語句 Windows API函數 系統
前言
Visual Basic是一個Windows系統下的應用程序開發平臺。方便的界面設計、強大的擴充能力使
程序員能節省大量時間,把主要精力集中在應用程序核心代碼的編寫上。Visual Basic目前已成為Windows系統下一種高效靈活的開發工具,而調用API(Application Program Interface,應用程序接口)函數正是對Visual Basic功能的強有力擴充,它使得Visual Basic能夠直接控制和處理計算機的系統參數和硬件資源。借助于API使得Visual Basic能克服Windows編程的難點,同時又增加供了使用Windows環境的靈活性。
利用Visual Basic調用API函數的方法:
(1)用Declare語句聲明所要調用的API函數,若該函數無返回值,可聲明為Sub過程;若有返回值,則可聲明為Function函數。
(2)一旦聲明了某一個API函數后,就可以象調用Visual Basic的函數一樣。但如果參數傳遞不對,可能會導致死機。
一、檢測系統參數的API函數
檢測系統環境的參數所需要的Windows API函數有GetWindowsDirectory,GetWinFlags,GetVersion,GetKeyboardType等,具體的使用方法見下面聲明。
1.GetWinflags聲明
Declare Function GetWinflags Lib "kernel32" () As Long
功能:該函數返回Windows運行系統上的系統配置。
返回標志值
含意
WF_80X87
Intel數字協處理器
WF_CPU386
80386 CPU
WF_CPU486
80486 CPU
WF_ENHANCED
Windows 系統運行在386增強模式
WF_PMODE
Windows 系統運行在保護模式
WF_STANDARD
Windows 系統運行在標準模式
WF_WLO
運行在OS/2下
2.GetKeyboardType聲明
Declare Function GetKeyboardType Lib "user32" (ByVal nTypeFlag As Long) As Long
功能:該函數得到系統鍵盤類型,nTypeFlag=0時返回鍵盤類型。
返回值
含意
1
IBM PC/XT 或兼容鍵盤
2
Olivetti "ICO" 鍵盤(102個鍵)
3
IBM 或兼容鍵盤(84個鍵)
4
IBM 增強型或相似鍵盤(101或102個鍵)
5
Nokia1050或相似鍵盤
6
Nokia9140或相似鍵盤
7
日本鍵盤
3.GetWindowsDirectory聲明
Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
功能:該函數獲取Windows目錄的路徑。
4.GetVersion聲明
Declare Function GetVersion Lib "kernel32" () As Long
功能:該函數返回當前Windows版本號和DOS版本號。返回值的低位字節說明Windows主版本號,返回值的低位字的高位字節說明Windows副版本號,高位字的低位字節說明DOS副版本號,高位字的高位字節說明DOS主版本號。
5.GetSystemDirectory聲明
Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA"
(ByVal lpBuffer As String, ByVal nSize As Long) As Long
功能:該函數獲取Windows系統子目錄的路徑。
二、檢測系統參數程序實例
有了上面對所需Windows API函數的聲明以后,就可以使用這些API函數,編寫下面的用戶自定義函數。
1.檢測系統子目錄函數
Function WinDir()
Temp = Space$(255)
StringLen = GetWindowsDirectory(Temp, 255)
WinDir = Left$(Temp, StringLen)
End Function
2.檢測系統鍵盤函數
Function KeyType()
KbType = GetKeyboardType(0)
Select Case KbType
Case 1
KeyType="IBM PC\XT,or compatible"
Case 2
KeyType="Olivetti 'ICO'(102key)"
Case 3
KeyType="IBM AT/similar(84keys)"
Case 4
KeyType = "IBM Enhance(101/102 keys)"
Case 5
KeyType = "Nokia1050/similar"
Case 6
KeyType = "Nokia9140/similar"
Case 7
KeyType = "Japan Keyboard"
End Select
End Function
3.檢測Windows版本號函數
Function WinVer()
ver& = GetVersion()
winhigh = ver& Mod 256
winlow = Int(ver& / 256) Mod 256
WinVer = ((winhigh * 100) + winlow) / 100
End Function
4.檢測系統CPU函數
Function CPU()
Flags&=GetWinFlags()
Match=1
Select Case Match
Case (Flags& And &H8)\&H8
CPU=486
Case (Flags& And &H4)\&H4
CPU=386
End Select
End Function
5.檢測Windows運行模式函數
Function Mode()
Flags&=GetWinFlags()
If flags& And &H20 Then
Mode="Enhanced"
Else
Mode="Standard"
End If
End Function
(二) 建 立 窗 體 文 件Form1.frm
Form1.Caption="System Information"
Sub Form_Load()
Cls
Print
Print , "System Information"
Print
Print , "WindowsDir: ", WinDir()
Print , "KeyboardType: ", KeyType()
Print , "WindowsVersion: ", WinVer()
Print , "SystemCpu: ", CPU()
Print , "WindowsMode: ", Mode()
End Sub
在Visual Basic中用 Declare語句聲明所要調用的Windows API函數, 使得程序設計者在Visual Basic中能夠直接控制和處理計算機的系統參數和硬件資源,增加了程序設計者在Windows環境中開發軟件的靈活性,使軟件與Windows系統達到了最完美的結合。
原文轉自:http://www.anti-gravitydesign.com