Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok, so wonder if I can get a few pointers on this, pun intended :-)

I have thrown some code together which creates a file from a drive letter and then attempts to read it in 512 byte blocks.

I then want to look at the first three bytes of the buffer and compare them to a jpeg header. If a jpeg header is found, increase a counter, update a label on my form and then advance the read by 512 bytes using SetFilePointer.

The code I have is as follows:

VB
Dim Hnd As Integer
       Dim nBytesToRead As Integer
       'Dim bResult As Integer
       Dim Buffer(512) As Byte

       Dim lowbyte As Integer       ' low dword of file pointer position
       Dim highbyte As Integer      ' high dword of file pointer position

       Hnd = CreateFile("\\.\" & Mid(strDrive, 1, 2), GENERIC_READ Or GENERIC_WRITE,      FILE_SHARE_READ Or FILE_SHARE_WRITE, _
       Nothing, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_OVERLAPPED, IntPtr.Zero)

       Dim loopcount As Integer = 0 ' to keep track of how many 512 blocks are being read
       Dim count As Integer = 0 ' count the number of files found

       If (Hnd <> INVALID_HANDLE_VALUE) Then

           While True

               ReadFile(Hnd, Buffer, 512, nBytesToRead, New System.Threading.NativeOverlapped)

               ' find magic number
                   If buffer(0)= 255 and buffer(1) = 216 and buffer (2) = 255 Then
                   ' means we have the start of a new file

                       ' if already have a file open, close it
                       ' TODO

                       ' increase the file count
                       count += 1
                       Form1.lbl_RecoveredCount.Text = count + 1 ' +1 because its base-zero
                       Application.DoEvents()
                   End If

               'advance to the next 512 block
               loopcount += 1
               lowbyte = 512 * loopcount
               highbyte = 0
               SetFilePointer(Hnd, lowbyte, highbyte, FILE_BEGIN)

               Array.Clear(Buffer, Buffer.GetLowerBound(0), Buffer.Length)

           End While
           CloseHandle(Hnd)
       Else
           Return False
       End If

       Return True

   End Function


At the minute, the SetFilePointer I dont think advances, or if it does, Buffer does not update. Can anyone see what I'm missing?

Much appreciated.

p.s. I'm aware I havent handled potential exceptions yet, I'll cover this (hopefully) when the code is working.
Posted
Updated 18-Mar-13 6:24am
v2
Comments
Richard MacCutchan 18-Mar-13 12:12pm    
You do appreciate that after reading the first 512 bytes, you set the file pointer to the exact same position it is already at. Maybe a better explanation of the format of the file and what you are trying to do would help.
bigcdh 18-Mar-13 12:16pm    
Thanks Richard. I'm looking to read in the contents of a drive, not a particular file. The idea is to look for all jpeg headers within.

Appreciate the spot with setting the position to where I'm currently at, however as I'm in a while loop, shouldnt it increment on each iteration as I'm telling it to read 512, 1024, 1536 ... bytes from the start by multiplying the count by 512..
bigcdh 18-Mar-13 12:28pm    
p.s. just thinking about it, as its an overlapped file thats used, I thought I'd have to manually move the pointer, so despite reading the first 512 bytes initially, I'd still need to advance the pointer to the end of that 512 bytes to read the next lot. Not sure...
Richard MacCutchan 18-Mar-13 13:35pm    
If you read 512 bytes then the file pointer will be set at 512; so the next read will be from that point. I'm still not quite sure what you are trying to do with this code other than looking for a starting sequence of 3 bytes in each block of 512.
bigcdh 18-Mar-13 13:47pm    
Hi Richard, thats exactly what I want to do. Ready three bytes at the start of every 512 block.

Reading the man pages for Windows API, I'm sure I have to advance the pointer manually.

http://vmd.myxomop.com/apires/ref/r/readfile.html

ReadFile reads the desired amount of bytes from a file. The file must of course have been opened with at least read-level access. If the file is synchronous (not overlapped), the function begins reading the file from the current position of the file pointer, and the function automatically adjusts the file pointer to point to the byte immediately after the last byte read. If the file is asynchronous (overlapped), the structure passed as lpOverlapped identifies the point to begin reading from, and the program calling the function is responsible for updating the file pointer.

1 solution

It looks like you are setting the point to the same position for each time it loops into the file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900