ActiveX FTP Upload control with status

I am trying to create an ActiveX control to be used in IE that will enable uploading of multiple files via FTP.  I need to provide an escape mechanism ('cancel' button) to be accessable during the upload also.  This has caused me to start working with the wininet.dll more specifically the functions FtpOpenFile and InternetWriteFile.  I have downloaded the simpleFTP program (vbsmpftp proj) from Microsoft, and the code supplied there works for uploading.  However, when I try to put it into an ActiveX control to be used within IE, I end up only getting garbage data (looks like possible memory values) in the resulting file that was uploaded.  

If more points are warrented, I'll up them later.

Thanks,
 Jon


Here is the code:

If hOpen = 0 Then
      ErrorOut Err.LastDllError, "InternetOpen"
    End If
   
    If hConnection <> 0 Then
        InternetCloseHandle hConnection
    End If
   
    hConnection = InternetConnect(hOpen, "vijay", INTERNET_INVALID_PORT_NUMBER, _
                                  "user", "user_passwd", INTERNET_SERVICE_FTP, 0, 0)
   
    If hConnection = 0 Then
        ErrorOut Err.LastDllError, "InternetConnect"
        GoTo CLOSEOUT
    End If
           
   Dim i As Long
   Dim ct As Long
   Dim path As String
   Dim filename As String
   
   ct = UBound(filesToUpload)

   Dim Data(99) As Byte ' array of 100 elements 0 to 99
   Dim Written As Long
   Dim Size As Long
   Dim Sum As Long
   Dim j As Long
   
   Sum = 0
   j = 0
   hfile = FtpOpenFile(hConnection, "cds.txt", &H40000000, FTP_TRANSFER_TYPE_BINARY, 0)
   If hfile = 0 Then
       ErrorOut Err.LastDllError, "FtpOpenFile"
       GoTo CLOSEOUT
   End If
   Open "C:\desktop\cds.txt" For Binary Access Read As #1
   Size = LOF(1)
   For j = 1 To Size \ 100
       Get #1, , Data
       If (InternetWriteFile(hfile, Data(0), 100, Written) = 0) Then
           ErrorOut Err.LastDllError, "InternetWriteFile"
           GoTo CLOSEOUT
       End If
       'DoEvents
       Sum = Sum + 100
       'Label7.Caption = str(Sum)
   Next j
   Get #1, , Data
    If (InternetWriteFile(hfile, Data(0), Size Mod 100, Written) = 0) Then
           ErrorOut Err.LastDllError, "InternetWriteFile"
           GoTo CLOSEOUT
   End If
   Sum = Sum + (Size Mod 100)
   'Label7.Caption = str(Sum)
   
CLOSEOUT:
   
   Close #1
   InternetCloseHandle hfile
   InternetCloseHandle hConnection
End Sub
punkriderAsked:
Who is Participating?
 
PaulHewsConnect With a Mentor Commented:
What is your declaration for InternetWriteFile?
0
 
Richie_SimonettiIT OperationsCommented:
0
 
punkriderAuthor Commented:
Paul,

I looked at the declarations between the two and don't you know it, my declaration that I got off the net somewhere had the wrong type.

You hit the nail on the head.  I guess the moral of this story is 'do not trust anything on the net'.  Thanks.

Here was the wrong declaration:

Public Declare Function InternetWriteFile Lib "Wininet.dll" _
           (ByVal hfile As Long, _
            ByVal sBuffer As String, _
            ByVal lNumberOfBytesToWrite As Long, _
            lNumberOfBytesWritten As Long) As Integer

and the correct version:

Public Declare Function InternetWriteFile Lib "Wininet.dll" _
           (ByVal hfile As Long, _
            ByRef sBuffer As Byte, _
            ByVal lNumberOfBytesToWrite As Long, _
            lNumberOfBytesWritten As Long) As Integer
0
 
PaulHewsCommented:
That declaration (the wrong one) may work if you are using a string instead of a byte array to store the file contents you are uploading.  Glad you got it working.  

Do you have a link to the MS sample project?  I went hunting, but I couldn't find it.
0
 
gr53Commented:
You can download the MS sample project here:
http://download.microsoft.com/download/ie4095/vbsmpftp/1/w9xnt4/en-us/VBSMPFTP.exe

There was a support article on this support.microsoft.cok #Q195653 but it seems to be gone now.

I had the same problem, glad I found this. :)

0
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.