FileGet exception RRS feed

  • Question

  • Hi,
    I am a displaced person from VB6. The program ran in VB6 until in failed after a recent Windows 10 update. The  original code has now been converted using VB2008.

    Now running in VB2019

    The code appears to get the length of the file and then fail because the length is wrong.

    Private Function OpenJPGFile(ByVal inFile As String) As Object
    		Dim fFile As Short
    		fFile = FreeFile
    		FileOpen(fFile, inFile, OpenMode.Binary)
            ReDim ExifTemp(LOF(fFile) + 1)
    		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
    		FileGet(fFile, ExifTemp)
    	End Function

      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.
       at Microsoft.VisualBasic.FileSystem.FileGet(Int32 FileNumber, Array& Value, Int64 RecordNumber, Boolean ArrayIsDynamic, Boolean StringIsFixedLength)
       at SavePhotos4Net.ExifReader.OpenJPGFile(String inFile) in J:\Allwork\Visual Studio 2019\SavePhotosADO.NET\Backup\ExifReader.vb:line 260
       at SavePhotos4Net.ExifReader.Load(String picFile) in J:\Allwork\Visual Studio 2019\SavePhotosADO.NET\Backup\ExifReader.vb:line 233
       at SavePhotos4Net.Form1.GetNewNameExif(String& strInFullName) in J:\Allwork\Visual Studio 2019\SavePhotosADO.NET\Backup\Form1.vb:line 508
       at SavePhotos4Net.Form1.ProcessFilesInFolder(Folder& fol) in J:\Allwork\Visual Studio 2019\SavePhotosADO.NET\Backup\Form1.vb:line 424
       at SavePhotos4Net.Form1.ProcessFilesInFolder(Folder& fol) in J:\Allwork\Visual Studio 2019\SavePhotosADO.NET\Backup\Form1.vb:line 458
       at SavePhotos4Net.Form1.ProcessMainFolders() in J:\Allwork\Visual Studio 2019\SavePhotosADO.NET\Backup\Form1.vb:line 391
       at SavePhotos4Net.Form1.cmdProcessMedia_Click(Object eventSender, EventArgs eventArgs) in J:\Allwork\Visual Studio 2019\SavePhotosADO.NET\Backup\Form1.vb:line 154
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at SavePhotos4Net.Form1.Main()

    Saturday, October 17, 2020 8:26 PM


  • Maybe try ‘-’ instead of ‘+’: ReDim ExifTemp(LOF(fFile) - 1), but also consider more upgrades.

    • Marked as answer by Graham Skan Monday, October 26, 2020 11:04 AM
    Sunday, October 18, 2020 8:05 AM

All replies

  • Hi

    Not sure what you require.

    Here is some code that shows one way to get the Bytes of a file, which may be close to what you want?

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		Dim filepath As String = "C:\Users\lesha\Desktop\Images\Architecture X.jpg"
    		Dim bytes() As Byte = GetBytes(filepath)
    		Stop ' mouse hover over 'bytes' 
    	End Sub
    	Function GetBytes(fn As String) As Byte()
    		Return IO.File.ReadAllBytes(fn)
    	End Function
    End Class

    Regards Les, Livingston, Scotland

    • Edited by leshay Saturday, October 17, 2020 9:26 PM
    Saturday, October 17, 2020 9:26 PM
  • Hello,

    Although VB 6 and .NET have similar methods there are better .NET classes and methods. For instance to get a byte array for a binary file we have System.IO.File class which has a method ReadAllBytes.

    Here I read an existing .JPG file in the same folder as the executable. You don't have to use a class, it's really up to you but I tend to place code into classes.

    Public Class FileOperations
        Public Shared Function OpenJpgFile(fileName As String) As Byte()
            Return File.ReadAllBytes(fileName)
        End Function
    End Class

    Called from a Button Click event.

    Private Sub ReadFileBytesButton_Click(sender As Object, e As EventArgs) Handles ReadFileBytesButton.Click
        Dim bytes As Byte() = FileOperations.OpenJpgFile("Summit1.jpg")
    End Sub

    If the file was under another folder.

    Dim bytes As Byte() = FileOperations.OpenJpgFile("C:\UserData\Pictures\Summit1.jpg")

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Sunday, October 18, 2020 12:32 AM
  • Maybe try ‘-’ instead of ‘+’: ReDim ExifTemp(LOF(fFile) - 1), but also consider more upgrades.

    • Marked as answer by Graham Skan Monday, October 26, 2020 11:04 AM
    Sunday, October 18, 2020 8:05 AM
  • Thank you both.

    I was trying to understand what was wrong with the code. It seems to create a buffer with a size equal to the file length, but which turns out to be too small.

    I will try the ReadAllBytes method.

    Tuesday, October 20, 2020 10:33 AM
  • Sorry
    (MCC, MVP).
    I overlooked your one-liner. I will, of course see what that does as well.
    Tuesday, October 20, 2020 10:40 AM
  • Hi Graham Skan,

    How is the question going? If your question has been answered then please click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.

    Besides, if you need further assistance, please let me know.

    Best Regards,

    Xingyu Zhao

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 21, 2020 2:01 AM
  • Sorry for the delay. Been having PC problems.
    The change from '+' to '-'  worked. Thank you.
    The code had been through two converters - VB2008 to get it to .net and VB2019 before I actually tried it
    Monday, October 26, 2020 11:03 AM