Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Visual Basic Programming Code Examples

Visual Basic > API and Miscellaneous Code Examples

Get the error message associated with and API error number

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
Get the error message associated with and API error number Most API calls will return an error number indicating whether the call was successfully made or not. To convert these error numbers into more meaningful error messages use the following function: 'Error message API Private Declare Function LoadLibraryEx Lib "kernel32" Alias "LoadLibraryExA" (ByVal lpLibFileName As String, ByVal hFile As Long, ByVal dwFlags As Long) As Long Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, ByVal lpSource As Long, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Any) As Long 'Purpose : Converts an error number to an error description 'Inputs : lErrorNumber The API error number 'Outputs : Returns a descriptive error message Function ErrorDescription(ByVal lErrorNumber As Long) As String Const FORMAT_MESSAGE_FROM_HMODULE = &H800, FORMAT_MESSAGE_FROM_SYSTEM = &H1000 Const NERR_BASE = 2100, MAX_NERR = NERR_BASE + 899 Const LOAD_LIBRARY_AS_DATAFILE = &H2 Dim sMsg As String Dim sRtrnCode As String Dim lFlags As Long Dim hModule As Long Dim lRet As Long hModule = 0 sRtrnCode = Space$(256) lFlags = FORMAT_MESSAGE_FROM_SYSTEM 'If lRet is in the network range, load the message source If (lErrorNumber >= NERR_BASE And lErrorNumber <= MAX_NERR) Then hModule = LoadLibraryEx("netmsg.dll", 0&, LOAD_LIBRARY_AS_DATAFILE) If (hModule <> 0) Then lFlags = lFlags Or FORMAT_MESSAGE_FROM_HMODULE End If End If 'Call FormatMessage to allow for message text to be acquired 'from the system or the supplied module handle. lRet = FormatMessage(lFlags, hModule, lErrorNumber, 0&, sRtrnCode, 256&, 0&) If (hModule <> 0) Then 'Unloaded message source FreeLibrary hModule End If ErrorDescription = "ERROR: " & lErrorNumber & " - " & sRtrnCode 'Clean message lRet = InStr(1, ErrorDescription, vbNullChar) If lRet Then ErrorDescription = Left$(ErrorDescription, lRet - 1) End If lRet = InStr(1, ErrorDescription, vbNewLine) If lRet Then ErrorDescription = Left$(ErrorDescription, lRet - 1) End If End Function