none
Problem with InternetWriteFile

    Question

  • I have converted a project from VB6 to VB2005.  The code works in VB6 but is not working in VB2005.  I am receiving the following error when the last chunk (100 bytes) of the file is being read (FileGet(piFileHandle, bData)).  I realize that the last chunk of bytes is less than 100 bytes as declared.  The documentation seems to indicate if the array is bigger that no error should occur.  Any insight into this behavior is greatly appreciated.

    Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.

    Private Function DoFTPInternetWriteFile(ByVal piFileHandle As Integer, ByVal plFile As Long) As Integer

    'declare local variables

    Dim iWriteFile As Integer

    Dim iWritten As Integer

    Dim bData(99) As Byte ' array of 100 elements 0 to 99

    Dim fErrorRecoverySuccess As Boolean

    mtRoutineName = "DoFTPInternetWriteFile"

    fErrorRecoverySuccess = True

    iWriteFile = 0

    FileGet(piFileHandle, bData)

    Do While fErrorRecoverySuccess = True

    iWriteFile = InternetWriteFile(CInt(plFile), bData(0), 100, iWritten)

    mlSum = mlSum + iWritten

    If iWriteFile = 0 Then

    fErrorRecoverySuccess = ErrorOut(Err.LastDllError, "ActivateFTPSession", mtRoutineName)

    Else

    Exit Do

    End If

    System.Windows.Forms.Application.DoEvents()

    Loop

    DoFTPInternetWriteFile = iWriteFile

    End Function

    Thursday, February 23, 2006 2:03 AM

All replies

  • Hi

    "The documentation seems to indicate if the array is bigger ...."

    I did not find that in MSDN. Could you post a link. I think if the array is bigger, it will read to the end of the file. You may use LOF(piFileHandle) to check how may bytes in the file and redim your array.

    Thursday, February 23, 2006 2:44 AM
  • Hi

    What is the error message? I had the problem before. Please refer to http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=83420&SiteID=1

    Thursday, February 23, 2006 3:51 AM
  • The documentation I was referring to:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vafctGet.asp

    The error message:

    "Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection."

    I reread the documentation for FileGet and you may be right that when reading from a binary file into an array that the number of bytes can not be exceeded.  I was assuming that if the array was bigger then the unused elements would be padded with 0s.  I think my problem is similar to the problem you had.  I tried doing:

    ReDim Preserve mbData(99)

    I also tried:

    <VBFixedArray(99)> Private mbData() As Byte ' array of 100 elements 0 to 99

    I get the same error when the code is trying to read the last chunk of the file.  I didn't really want to keep track of the remaining bytes and redim for each iteration.  Is that my only choice?

    Thanks

    Thursday, February 23, 2006 2:50 PM