WinINet problem with InternetGetLastResponseInfo method.  Program ends without error.

Posted on 2007-08-10
Last Modified: 2013-11-26
I have an unattended process that pull TIFF files from a remote server.  I'm using the WinINet.dll for the FTP functions.  I use the FtpGetFile function to copy down the file, and if successful, I use the FtpDeleteFile function to remove the file from the source directory.  This process has to run thru a proxy server, and during certain periods of heavy usage, the transfers are extremely slow.  As a result, the proxy sometimes drops the connection while the file is transferring.  Although the transfer completes, the attempt to delete the source file fails because the connection is no longer active.  I'm trying to find a reliable way to check the connection following the transfer before attempting the delete, or at least capture the error information when FtpDeleteFile fails.

I'm trying to use the InternetGetLastResponseInfo function to get any useful error numbers and text so that I can proceed accordingly.  The following is a copy of my DeleteFile method.:

Public Function DeleteFile(FileToDelete As String) As Boolean
Dim lBuffSize As Long
Dim lError As Long
Dim sErrDesc As String
   On Error GoTo ErrorHandler
    'Delete the file
    If Not FtpDeleteFile(m_hConnection, FileToDelete) Then
        'Attempt to retrieve FTP error info.
        If Err.LastDllError = ERROR_INTERNET_EXTENDED_ERROR Then
            InternetGetLastResponseInfo lError, vbNullString, lBuffSize
            sErrDesc = String$(lBuffSize + 1, vbNullChar)
            InternetGetLastResponseInfo lError, sErrDesc, lBuffSize                '***********
            Err.Raise ERR_ON_DELETE, , sErrDesc & vbCr & vbCr & _
                "Error occured while trying to delete file " & FileToDelete & " on the remote server."
            lError = Err.LastDllError
            sErrDesc = GetDLLErrorText(lError)
            Err.Raise lError, , sErrDesc
        End If
    End If
    If Err.Number <> 0 Then
        m_lErrNum = Err.Number
        m_sErrDesc = Err.Description
        DeleteFile = False
    End If
End Function

The variable m_hConnection is the handle returned by the original OpenConnection call.  My problem is that the second call to InternetGetLastResponseInfo completely aborts my program (without any error message).  In my IDE, when I step thru the execution, same thing.  No error, nothing, IDE simply closes.

The WinINet.dll version I have is 6.0.2800.1596.  Both my dev machine and the server that the application runs on are Win2000 SP4 (5.0.2195)
Question by:BMarq
    LVL 19

    Expert Comment

    Have you looked at the FtpWebRequest class?  It is much easier to use in that it automatically opens a new connection if one does not already exist.  Since it's part of the .NET framework, error handling is also much easier.  It might be faster to switch out the FTP code than to try to diagnose these issues with WinINet.

    Docs are at is you are interested.

    Simple delete file example from docs:

        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
        request.Method = WebRequestMethods.Ftp.DeleteFile;
        FtpWebResponse response = (FtpWebResponse) request.GetResponse();
        Console.WriteLine("Delete status: {0}",response.StatusDescription);  

    If you set the KeepAlive property the connection will not close and the next FTP request will reuse it to the same server.

    Author Comment

    Currently, I'm limited to VB6 for this particular application.  I will discuss with the customer about converting part or all of the application to .NET, but in the meantime, need to get this one going.
    LVL 19

    Expert Comment

    The only thing I see that might be a problem with your code is that lBuffSize should be incremented prior to the second call to InternetGetLastResponseInfo because you have (correctly) allocated lBuffSize+1 characters on the string.  Doesn't seem that would cause termination of your program, though.

    The other issue is how you have declared the InternetGetLastResponseInfo function for use by VB.  Maybe it is not correct somehow.  Using an incorrect pointer could case termination of your program.

    And if you are in VB6 you should post in the Visual Basic section rather than then VB.NET section to avoid responses like my first one - it really is easier, though ;)

    Author Comment

    I apologize for the post in the wrong zone, this is my first time.  Although I think I should have gotten it to both zones, I'll go back and check.

    I tried the increment, no change in behavior.  Here is my API declaration:

    Private Declare Function InternetGetLastResponseInfo _
        Lib "WinInet.dll" Alias "InternetGetLastResponseInfoA" _
        (ByRef lpdwError As Long, ByRef lpszErrorBuffer As String, _
         ByRef lpdwErrorBufferLength As Long) As Boolean
    LVL 19

    Expert Comment

    It's been a while since I did this kind of stuff, but does it let you define the String parameter as ByVal?
    LVL 19

    Accepted Solution

    I did a quick search on Google and it appears that changing the String param to ByVal is correct.  That's probably what is crashing your program.

    Author Comment

    The change to ByVal did the trick.  Thank you so much.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    This article will show, step by step, how to integrate R code into a R Sweave document
    This is an explanation of a simple data model to help parse a JSON feed
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now