API Error Trapping

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
 
P

Paul Overway

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top