Click here to Skip to main content
15,888,238 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,

I have written a code to retrieve the error code of an FTP failure, but i am unable to retrieve the error message from it. I tried all the possible ways shown in google but iam not able to do it. Can anyone please share the code snippet or tell me how to do this. Please check the below code and tell em if i have done anything wrong:

The scenario iam trying is i am trying to retrieve a file from a particular location which is not present, using FTPGetFile API which raises error code "12003" but when iam trying to get the error message iam getting only blank msg.

My CODE:
VB
'test starts


Public 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


'Purpose     :  Return the error message associated with LastDLLError
'Inputs      :  [lLastDLLError]               The error number of the last DLL error (from Err.LastDllError)
'Outputs     :  Returns the error message associated with the DLL error number


Public Function ErrorDescriptionDLL(Optional ByVal lLastDLLError As Long) As String
    Dim sBuff As String * 1024
    Dim lCount As Long
    Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100, FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000
    Const FORMAT_MESSAGE_FROM_HMODULE = &H800, FORMAT_MESSAGE_FROM_STRING = &H400
    Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000, FORMAT_MESSAGE_IGNORE_INSERTS = &H200
    Const FORMAT_MESSAGE_MAX_WIDTH_MASK = &HFF

    If lLastDLLError = 0 Then
        'Use Err object to get dll error number
        lLastDLLError = Err.LastDllError
        'strMsg = FormatMessageFromModule("WinHttp.dll", lLastDLLError)
    End If

    lCount = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS, 0, lLastDLLError, 0&, sBuff, Len(sBuff), ByVal 0)
    If lCount Then
        ErrorDescriptionDLL = Left$(sBuff, lCount - 2)    'Remove line feeds
    End If

End Function
'test ends


Please help me out. I also tried the GetLastError method but thats not even returning me error code unlike Err.LastDLLError
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900