ftp in VB6 - URGENT

So I have serious problems.  I am on vacation in Mexico and my employees back in the United States report that they can't fill orders.

I've found the suspect code to be this function:

Public Sub FtpPutFileEx( _
  ByVal szServer As String, _
  ByVal szUser As String, _
  ByVal szPassword As String, _
  ByVal szLocalFile As String, _
  ByVal szServerFile As String)
  Dim dwStatus As Long
  Dim dwLoFileSize As Long
  Dim dwHiFileSize As Long
  Dim dwPercent As Long
  '// init
  hOpen = InternetOpenW(0, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0)
  '// connect to the ftp server
  hConnect = InternetConnectW(hOpen, StrPtr(szServer), INTERNET_DEFAULT_FTP_PORT, StrPtr(szUser), StrPtr(szPassword), INTERNET_SERVICE_FTP, 0, 0)
  If hConnect = 0 Then
    Debug.Print "InternetConnectW()" & Err.LastDllError
    Exit Sub
  End If
  '// get handle for filename that will be written to the ftp server
  hInternet = FtpOpenFileW(hConnect, StrPtr(szServerFile), GENERIC_WRITE, FTP_TRANSFER_TYPE_UNKNOWN, 0)
  If hInternet = 0 Then
    Debug.Print "FtpOpenFile()" & Err.LastDllError
    Exit Sub
  End If
  '// get handle for local file to read bytes
  hFile = CreateFileW(StrPtr("\\?\" & szLocalFile), GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0)
    MsgBox "CreateFileW()" & Err.LastDllError
  End If
  ' // get local file size for progress. This example supports
  '    only up to 2GB of status reporting progress.
  dwLoFileSize = GetFileSize(hFile, dwHiFileSize)
  '// read local file, write server file bytes
    If ReadFile(hFile, VarPtr(Buffer(0)), BUF_SIZE, dwReadBytes, 0) Then
      If InternetWriteFile(hInternet, VarPtr(Buffer(0)), dwReadBytes, dwWrittenBytes) Then
        ' Track the amount of bytes written and percentage.
        dwStatus = (dwStatus + dwWrittenBytes)
        dwPercent = (dwStatus / dwLoFileSize) * 100
        'Label1.Caption = dwPercent
      End If
      Exit Do
    End If
  Loop Until dwReadBytes = 0
  Debug.Print "Done"
  '// cleanup
  Erase Buffer
  'MsgBox "Done Uploading"
End Sub

Open in new window

This has worked just fine for years.

I don't know what would cause it to stop working.  And it's doing the same thing on multiple machines.  It doesn't throw an error, it simply hangs for like 5 minutes and then continues as if it uploaded the file, but it did't.

Does anyone know how to fix this or possibly even scrap it and do another method of ftp from within VB6?
Who is Participating?
TamilvananrajaConnect With a Mentor Commented:
Hi alex..

try this code..


i hope it will be useful to you

AlexPaceConnect With a Mentor Commented:
If it worked fine for years and then stopped working on multiple computers then something probably changed on the network.  

Hopefully it is on YOUR side and not on the server side because at least if it is on your side it is something that can be fixed on your side... if that makes sense.

Anyway, to troubleshoot it you'll want to see what happens when you use a real FTP client.  Probably the same thing will will that you see in your VB6 program.  Use an FTP client that can produce a protocol-level trace log.  The trace log will show where the failure is happening and know what is failing will usually make the solution obvious.

Long term, you can't be the only dude that can fix it or stuff like this will keep happening to you.  I would split the FTP step out of the rest of the project and use supported commercial software to do it.  The FTP market is mature and the process of sending a file to a server is pretty straightforward.  It would be good for your employees to be able to open a support ticket with a software vendor instead of calling you on vacation with an emergency.  

I've used Robo-FTP from within VB6 with good results over COM/ActiveX but, I've actually had even better luck using it as a stand-alone service and then feeding it files from a VB app.  What I'm trying to say is that rather than create an entire FTP session in the VB app, I just make the VB app move or copy files into a specific network share.  Then I have Robo-FTP running its own script that watches the same folder and automatically uploads any files in the folder.  When the upload is complete it moves the files to an archive folder...  So the folder acts as a sort of Out Box except for FTP instead of mail.

This offered several benefits.  First and foremost, if the employees have an issue with it that local IT can't fix then they open a ticket with RoboFTP instead of calling me.  Second, any user on the network can drop files into the OutBox (assuming their account has permission) and the file will be automatically sent.   So later when Teri wants to send directly from an Excel macro there is no extra integration.  Third, if you want you can set it up so that certain files go to one site and other go to other sites or subfolders or whatever.  Anyway, the trick to running it as a Windows service is to make the  FTP script into an infinite loop.
hrolsonsConnect With a Mentor Author Commented:
Ended up taking a different route:


But thank you for your Robo FTP suggestion, I will be looking into that.
hrolsonsAuthor Commented:
The alternate way that I did is working great.
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.