Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

How to display API Error Description.
[ All Languages » VB »  Errors]

Total Hit ( 3958)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


When you execute any system API generally it returns success/failure using return value. To find out any error you have to use GetLastError api or Err.LastDllError property of Err object in VB. But it will return only ErrorCode not description. You can use following code to get description of API Error.

For Quick Demo

1. Start a new project in Visual Basic. Form1 is created by default.
2. Add the following code to the Form1 code window

Click here to copy the following block
'//To diaplay API Error
Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Const LANG_NEUTRAL = &H0
Const SUBLANG_DEFAULT = &H1
Const ERROR_ACCESS_DENIED = 5&

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
Function GetAPIErrorDesc(ErrorCode As Long) As String
  Dim Buffer As String
  Buffer = Space(200)
  FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, ErrorCode, LANG_NEUTRAL, Buffer, 200, ByVal 0&
  GetAPIErrorDesc = Buffer
End Function

Private Sub Form_Load()
  'Set any error number just for testing
  SetLastError ERROR_ACCESS_DENIED
  MsgBox GetAPIErrorDesc(Err.LastDllError), vbCritical
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.