FTP API Problems

I have a VB6 program that is supposed to search through a bunch of FTP directories and download any files that it finds in them.
There are problems where it fails on the FtpGetFile even when the file exists and the connection works.
It also seems to pause or freeze sometimes in the middle of a loop.

Private hConnection As Long
Private hOpen As Long

Function amdDownloadStart()
    Dim WFD As WIN32_FIND_DATA
    Dim sPath As String
    Dim hFind As Long
    Dim sFileSize As String
    Dim tmp As String
    Dim isTesting As String
   
    Dim Temp() As String
    Dim FileName As String
    Dim sOrgPath As String
    Dim hFindConnect As Long
    Dim itmX As ListItem
    Dim rSelect As New Recordset
   
    'open an internet connection
    hOpen = InternetOpen("Download", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)

    'connect to the FTP server
    hConnection = InternetConnect(hOpen, FTPSite, INTERNET_DEFAULT_FTP_PORT, Username, Password, INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)

    StatusBar.Panels(1).Text = "Connected"
    DoEvents
   
    With rSelect
        .Open "Select * From tblScanusers Where Not TracksID = 0", myApp.MyCn, adOpenForwardOnly, adLockReadOnly
        Do While Not .EOF

            StatusBar.Panels(1).Text = "Connected. Checking: " & !UserName
            DownloadFiles !TracksID, !UserName, "new"
            DoEvents
            DownloadFiles !TracksID, !UserName, "corrected"
            DoEvents
            .MoveNext
        Loop
        .Close
    End With
   
    StatusBar.Panels(1).Text = "Done"
    DoEvents
   
    'close the internet connection
    InternetCloseHandle hConnection
   
    InternetCloseHandle hOpen
   
    StatusBar.Panels(1).Text = "Disconnected. T-5"
    DoEvents
   
End Function

Function DownloadFiles(CustomerID As Long, UserName As String, Path As String)
    Dim WFD As WIN32_FIND_DATA
    Dim sPath As String
    Dim hFind As Long
    Dim sFileSize As String
    Dim tmp As String
   
    Dim Temp() As String
    Dim FileName As String
    Dim sOrgPath As String  'hConnection As Long, hOpen As Long,
    Dim hFindConnect As Long
    Dim itmX As ListItem
    Dim rSelect As New Recordset
    Dim fileget As String
    Dim LocalName As String
    Dim LocalDirectory As String
    Dim fFlag As Boolean
    fFlag = False
   
    LocalName = localUserName
   
    hFindConnect = InternetConnect(hOpen, FTPSite, INTERNET_DEFAULT_FTP_PORT, Username, Password, INTERNET_SERVICE_FTP, INTERNET_FLAG_EXISTING_CONNECT Or INTERNET_FLAG_PASSIVE, &H0)

    'create a buffer to store the original directory
    sOrgPath = String(MAX_PATH, 0)
   
    'get the directory
    FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
   
    LocalDirectory = "C:\Documents and Settings\" & LocalName & "\Desktop\Incoming\" & UserName & "\" & Path & "\"
   
    'set connection for getting the file names
    If hFindConnect Then
        sPath = "public/dealers/" & UserName & "/" & Path & "/" '"/public/dealers/andyd273/new"
        FtpSetCurrentDirectory hConnection, "public/dealers/" & UserName & "/" & Path & "/"
        DoEvents
        hFind = FtpFindFirstFile(hFindConnect, sPath, WFD, INTERNET_FLAG_RELOAD Or INTERNET_FLAG_NO_CACHE_WRITE, 0&)
        If hFind Then
            Do
                tmp = StripNull(WFD.cFileName)
               
               DoEvents
               If Len(tmp) Then
                    If WFD.dwFileAttributes And Not vbDirectory Then
               
                        If Not DirExists(LocalDirectory) Then '
                            NewDirectory LocalDirectory
                        End If
                            DoEvents
                       
                        fileget = ""
                        Do
                            StatusBar.Panels(1).Text = "Downloading File " & tmp
                            DoEvents
                            fileget = FtpGetFile(hConnection, tmp, LocalDirectory & tmp, True, 0, FTP_TRANSFER_TYPE_UNKNOWN, 0)
                            DoEvents
                        Loop While fileget = ""
                        If fileget = "True" Then
                            DoEvents
                            amdSaveFile CustomerID, LocalDirectory & tmp, Path, False
                            ListView1.ListItems.Add , LocalDirectory & tmp, LocalDirectory & tmp
                            DoEvents
                            FtpDeleteFile hConnection, tmp
                            StatusBar.Panels(1).Text = "Download Finished"
                            DoEvents
                        Else
                            StatusBar.Panels(1).Text = "Download Failed"
                            MsgBox "Download Failed: " & LocalDirectory & tmp
                        End If
                    End If
                End If
                DoEvents
            Loop While InternetFindNextFile(hFind, WFD)
        End If
    End If

    InternetCloseHandle hFind
    InternetCloseHandle hFindConnect
   
    hFindConnect = 0
    hFind = 0

End Function
andyd273Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

 
tonym001Commented:
Try changing the INTERNET_FLAG_PASSIVE value in the InternetOpen function from false to true or vise-versa
0
 
andyd273Author Commented:
I'm really not sure how to do this...

it gets set with:
Const INTERNET_FLAG_PASSIVE = &H8000000                    ' used for FTP connections

to turn it off would I just set it to 0?
0
 
tonym001Commented:
Sorry was not very clear there. What I should have said was this
You need a boolean whose value you set before calling the InternetConnect function .
For instance
Dim blnPassive as boolean
'set the value
blnPassive = true
hConnection = InternetConnect(hOpen, FTPSite, INTERNET_DEFAULT_FTP_PORT, Username, Password, INTERNET_SERVICE_FTP, IIf(blnPassive, INTERNET_FLAG_PASSIVE, _
      0), 0)

Changing the blnPassive value may be the solution as I have encountered a similar problem but it is just guesswork on my part. Hope it works

0
 
andyd273Author Commented:
Sorry it took me a few days to check this...
I've tried it several ways and it still failed, but it seemed to help some.
Then I put a do while loop with 20000 iterations

                                Do While Not hConnection And Count < 20000
                                    DoEvents
                                    Count = Count + 1
                                Loop

which seems to help more.

But now I'm running into another problem, which is that it seems that the connections aren't terminating.
I downloaded some files, and then tried going in through a normal ftp program to upload some more, and got a message that there were to many connections (15).

I have some stuff to close the connections
    InternetCloseHandle hFind
    InternetCloseHandle hFindConnect
    InternetCloseHandle hConnection
    InternetCloseHandle hOpen
but is this the best way to do it?
0
 
tonym001Commented:
Yes it is the best way. As long as you close the search handles, the FTP connection and the Internet connection in the order you opened them you should be fine.  

The problem you have is this
Do While Not .EOF
            StatusBar.Panels(1).Text = "Connected. Checking: " & !UserName
            DownloadFiles !TracksID, !UserName, "new"
            DoEvents
            Problem -> DownloadFiles !TracksID, !UserName, "corrected"
            DoEvents
            .MoveNext
        LoStatusBar.Panels(1).Text = "Done"
    DoEvents
Loop
    'close the internet connection
    InternetCloseHandle hConnection
     InternetCloseHandle hOpen

Every time you call the downloadfiles function you are opening a new internet connection and new FTP connection, however, you only close these connections after you have completed the Do Loop. So if you perform 16 searches you will leave 15 connections open and only close the last one.
 
Another problem I see in the code is you are not checking whether the FtpSetCurrentDirectory has worked.

0

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.