很多編程愛好者在VB的API編程中經常遇到API調用中的錯誤代碼(Error Code,在API調用中 遇到錯誤時使用GetLastError函數可以得到)。但是很多的時候錯誤代碼并沒有多大用處,因為 你并不知道代碼所代表的含義。而實際上,在Windows中為每個錯誤碼提供了一個錯誤提示,而 且適應不同的語言版本(既如果你使用中文版Windows,提示也是中文的)。只要通過API編程就 可以獲得詳細的錯誤提示。 下面通過程序來介紹,運行下面的程序,首先要在Form中加入一個ListBox和CommandButton 在將下面的代碼加入到form的代碼窗口中。 Private Declare Function FormatMessage Lib "kernel32" _ Alias "FormatMessageA" (ByVal dwFlags As Long, _ lpSource As Any, ByVal dwMessageId As Long, _ ByVal dwLanguageId As Long, ByVal lpBuffer _ As String, ByVal nSize As Long, Arguments As _ Long) As Long Private Declare Function GetLastError Lib "kernel32" _ () As Long Private Declare Function CloseHandle Lib "kernel32" _ (ByVal hObject As Long) As Long
Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000 Const FORMAT_MESSAGE_IGNORE_INSERTS = &H200
Private Sub Command1_Click() Dim ErrID As Long Dim astr As String Dim bstr As String Dim l As Long astr = String$(256, 20) '獲得具體的錯誤信息 For ErrID = 0 To 8191 l = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM Or _ FORMAT_MESSAGE_IGNORE_INSERTS, 0&, ErrID, 0&, _ astr, Len(astr), ByVal 0) If l Then bstr = Left$(astr, InStr(astr, Chr(10)) - 2) '將錯誤信息加入列表框 List1.AddItem Str(ErrID) + " " + bstr End If Next ErrID End Sub 運行程序,點擊Command1,錯誤代碼和向對應的錯誤提示信息就全部列在ListBox中了。 以上程序在Win95,VB5.0下運行通過。
|