在VB里err.Description里的错误信息有时与在VB环境下调试出来的错误信息不一样,API里有没有能返回的错误信息更详细的呢?

解决方案 »

  1.   

    好象你可以参考一下 Getlasterror
    不知有没写错
      

  2.   

    我看了,但是反回的是一个LONG参数,并没有详细的错误内容,请指点一下,谢谢
      

  3.   

    请问Getlasterror如何使用,最好给范例代码,谢谢
      

  4.   

    GetLastError返回的是API函数出错后的错误代码,可以用FormatMessage获得相关的错误描述
    。VB的Err.LastDllError属性和GetLastError返回相同的值。GetLastError只有API出错时才有相关的返回值,而且并非所有的API函数出错时都会设置LastError的,如果你的错误是由VB代码造成的,用GetLastError没有作用。
      

  5.   

    '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()
        'KPD-Team 1999
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        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