Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. ReadFile not working in VB.NET

ReadFile not working in VB.NET

Scheduled Pinned Locked Moved Visual Basic
helpcsharp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Roman Nurik
    wrote on last edited by
    #1

    Hey, i am trying to figure out why ReadFile isn't working the way it should in VB.NET. The problem is that I call ReadFile on some file that i open with CreateFile, and it fills a buffer with the first couple of bytes from the file.. that is fine. but when i call ReadFile again, it still returns the same first bytes.. as if it didnt increment the file pointer. Then, i tried checking the file pointer value before and after each call to readfile.. and what seems to happen is that every time readfile is called, the file pointer is reset to 0, because after the first, or second call to readfile the file pointer is correct (the number of bytes i read using the function).. so i guess it gets reset to 0 before it fills the buffer. here is the code if anyone can help: Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Integer Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer Declare Function ReadFile Lib "kernel32" (ByVal hFile As Integer, ByVal destBuffer() As Byte, ByVal nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesRead As Integer, ByRef lpOverlapped As OVERLAPPED) As Integer ... Dim hDisk As Integer Dim bytesReturned As Integer hDisk = CreateFile("E:\somebigfile.dat", _ GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, _ Nothing, OPEN_EXISTING, 0, 0) If hDisk = INVALID_HANDLE_VALUE Then Exit Sub me.Text = SetFilePointer(hDisk, 0, Nothing, FILE_CURRENT) Dim dest() As Byte ReDim dest(1023) Dim destLen As Integer = dest.Length ReadFile(hDisk, dest, 1024, bytesReturned, Nothing) PrintData(dest, txt1) 'just a function that writes the data to a textbox txt1 me.Text &= " " & SetFilePointer(hDisk, 0, Nothing, FILE_CURRENT) ReadFile(hDisk, dest, 1024, bytesReturned, Nothing) PrintData(dest, txt2) me.Text &= " " & SetFilePointer(hDisk, 0, Nothing, FILE_CURRENT) ReadFile(hDisk, dest, 1024, bytesReturned, Nothing) PrintData(dest, txt3) Thank you for any help!!!! This is driving me nuts!!!

    r

    D 1 Reply Last reply
    0
    • R Roman Nurik

      Hey, i am trying to figure out why ReadFile isn't working the way it should in VB.NET. The problem is that I call ReadFile on some file that i open with CreateFile, and it fills a buffer with the first couple of bytes from the file.. that is fine. but when i call ReadFile again, it still returns the same first bytes.. as if it didnt increment the file pointer. Then, i tried checking the file pointer value before and after each call to readfile.. and what seems to happen is that every time readfile is called, the file pointer is reset to 0, because after the first, or second call to readfile the file pointer is correct (the number of bytes i read using the function).. so i guess it gets reset to 0 before it fills the buffer. here is the code if anyone can help: Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Integer Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer Declare Function ReadFile Lib "kernel32" (ByVal hFile As Integer, ByVal destBuffer() As Byte, ByVal nNumberOfBytesToRead As Integer, ByRef lpNumberOfBytesRead As Integer, ByRef lpOverlapped As OVERLAPPED) As Integer ... Dim hDisk As Integer Dim bytesReturned As Integer hDisk = CreateFile("E:\somebigfile.dat", _ GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, _ Nothing, OPEN_EXISTING, 0, 0) If hDisk = INVALID_HANDLE_VALUE Then Exit Sub me.Text = SetFilePointer(hDisk, 0, Nothing, FILE_CURRENT) Dim dest() As Byte ReDim dest(1023) Dim destLen As Integer = dest.Length ReadFile(hDisk, dest, 1024, bytesReturned, Nothing) PrintData(dest, txt1) 'just a function that writes the data to a textbox txt1 me.Text &= " " & SetFilePointer(hDisk, 0, Nothing, FILE_CURRENT) ReadFile(hDisk, dest, 1024, bytesReturned, Nothing) PrintData(dest, txt2) me.Text &= " " & SetFilePointer(hDisk, 0, Nothing, FILE_CURRENT) ReadFile(hDisk, dest, 1024, bytesReturned, Nothing) PrintData(dest, txt3) Thank you for any help!!!! This is driving me nuts!!!

      r

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      First, according to the MSDN docs on ReadFile, your buffer should be passed ByRef, not ByVal. Second, are you checking the value of bytesReturned after each read? Why are you P/Invoking theres functions in the first place? They're wrapped by the System.IO namespace. Using the classes found there is MUCH easier than what your doing. I only say this because your posted code isn't doing anything that these classes couldn't handle. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

      R 1 Reply Last reply
      0
      • D Dave Kreskowiak

        First, according to the MSDN docs on ReadFile, your buffer should be passed ByRef, not ByVal. Second, are you checking the value of bytesReturned after each read? Why are you P/Invoking theres functions in the first place? They're wrapped by the System.IO namespace. Using the classes found there is MUCH easier than what your doing. I only say this because your posted code isn't doing anything that these classes couldn't handle. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

        R Offline
        R Offline
        Roman Nurik
        wrote on last edited by
        #3

        Well, the buffer is an array, which seems to automatically act as a pointer when it is passed to C functions, also I saw the declaration with ByVal many places and it seemed to work. I checked bytesReturned after each call, it is 1024. I also tried clearing the array before each call to ReadFile, and it still fills the buffer each time with the first bytes of the file. I realize now I can use the System.IO namespace and FileStream and such, but this has become more of a curiosity on my part. I am befuddled as to why API functions (I had similar problems calling ZLib DLL functions from VB.NET) don't work properly in VB.NET. Maybe it is something with the managed code heap or something? Thank you for your reply, however.. if anyone finds anything more on the subject please let me know.

        r -€

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups