Public Declare Function GetLastError Lib "kernel32" Alias "GetLastError" () As Long 例子: 'Create a new project and add this code to Form1 Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100 Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000 Const LANG_NEUTRAL = &H0 Const SUBLANG_DEFAULT = &H1 Const ERROR_BAD_USERNAME = 2202& Private Declare Function GetLastError Lib "kernel32" () As Long Private Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long) 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 Sub Form_Load() Dim Buffer As String 'Create a string buffer Buffer = Space(200) 'Set the error number SetLastError ERROR_BAD_USERNAME 'Format the message string FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, GetLastError, LANG_NEUTRAL, Buffer, 200, ByVal 0& 'Show the message MsgBox Buffer End Sub
例子:
'Create a new project and add this code to Form1
Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Const LANG_NEUTRAL = &H0
Const SUBLANG_DEFAULT = &H1
Const ERROR_BAD_USERNAME = 2202&
Private Declare Function GetLastError Lib "kernel32" () As Long
Private Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long)
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 Sub Form_Load()
Dim Buffer As String
'Create a string buffer
Buffer = Space(200)
'Set the error number
SetLastError ERROR_BAD_USERNAME
'Format the message string
FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, GetLastError, LANG_NEUTRAL, Buffer, 200, ByVal 0&
'Show the message
MsgBox Buffer
End Sub