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:
Dim Hnd As Integer
Dim nBytesToRead As Integer
Dim Buffer(512) As Byte
Dim lowbyte As Integer
Dim highbyte As Integer
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
Dim count As Integer = 0
If (Hnd <> INVALID_HANDLE_VALUE) Then
While True
ReadFile(Hnd, Buffer, 512, nBytesToRead, New System.Threading.NativeOverlapped)
If buffer(0)= 255 and buffer(1) = 216 and buffer (2) = 255 Then
count += 1
Form1.lbl_RecoveredCount.Text = count + 1
Application.DoEvents()
End If
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.