API Error Trapping

  • Thread starter Thread starter Wayne Huxman
  • Start date Start date
W

Wayne Huxman

Does anyone have any input on the most effective way trap potential API
errors, including the API DLL not being available., etc. Any clues would be
appreciated. Thanks.

Wayne Huxman
 
API functions should be declared private and called by a public function
that includes error trapping. You don't mention which API function(s) you
are using, and errors will vary depending on the function. Therefore, the
error trapping requirements will vary.

i.e.,

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Function NetUserName() As String
Dim lngLen As Long, lngX As Long
Dim strUserName As String

On Error Resume Next

strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If ( lngX > 0 ) Then
NetUserName = Left$(strUserName, lngLen - 1)
Else
NetUserName = vbNullString
End If
End Function

Also, some API functions, if used inappropriately, will crash your
system...no error handling will save you. So, you must be careful when
using API functions.
 
Back
Top