I'm trying to read a physical hard drive but SetFilePointer throws an overflow error trying to read past 2GB using this code:
Code:Option Explicit Private Const GENERIC_READ = &H80000000 Private Const GENERIC_WRITE = &H40000000 Private Const FILE_SHARE_READ = &H1 Private Const FILE_SHARE_WRITE = &H2 Private Const OPEN_EXISTING = 3 Private Const INVALID_HANDLE_VALUE = -1& Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) Private Const FILE_BEGIN = 0 Private Const FILE_CURRENT = 1 Private BytesPerSector As Integer Public Function Direct_Read_Drive(ByVal sDrive As String, ByVal iStartSec As Long, ByVal iOffset As Long, ByVal cBytes As Long) As Variant Dim hDevice As Long Dim abBuff() As Byte Dim abResult() As Byte Dim nSectors As Long Dim nRead As Long BytesPerSector = 512 nSectors = Int((iOffset + cBytes - 1) / BytesPerSector) + 1 hDevice = CreateFile(sDrive, GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0&, 0&) If hDevice = INVALID_HANDLE_VALUE Then Exit Function Call SetFilePointer(hDevice, iStartSec * BytesPerSector, 0, FILE_BEGIN) ReDim abResult(cBytes - 1) ReDim abBuff(nSectors * BytesPerSector - 1) Call ReadFile(hDevice, abBuff(0), UBound(abBuff) + 1, nRead, 0&) CloseHandle hDevice CopyMemory abResult(0), abBuff(iOffset), cBytes Direct_Read_Drive = abResult(0) If Direct_Read_Drive = 0 Then Direct_Read_Drive = "00" End FunctionCode:Private Sub Form_Load() Dim retVal As Long retVal = Direct_Read_Drive("\\.\PhysicalDrive0", 0, 0, 1) MsgBox retVal End Sub
Any ideas how to work around this? SetFilePointer can accept FILE_CURRENT and I'm thinking about some kind of loop 2gb chunks at a time but I'm not quite sure if that would be the best/quickest way or how to even pass a huge number to it.




Reply With Quote