Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject 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 WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Const INVALID_HANDLE_VALUE = -1
Private Const OPEN_EXISTING = 3
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const FILE_CURRENT = 1
Public hDevice As Long
Public Function rawread(ByVal rDrive As String, ByVal SectorToRead As Long, ByVal LastSectorToRead As Boolean) As String
If Len(rDrive) < 4 Then rDrive = Left(rDrive, 2)
Dim abBuff() As Byte
Dim abResult() As Byte
Dim nSectors As Long
Dim nRead As Long
iOffset = SectorToRead
cBytes = 512
BytesPerSector = 512
If hDevice < 1 Then hDevice = CreateFile("\\.\" & rDrive, GENERIC_READ Or GENERIC_WRITE, 0, ByVal 0, OPEN_EXISTING, 0, 0)
If hDevice = INVALID_HANDLE_VALUE Then Exit Function
Call SetFilePointer(hDevice, SectorToRead * BytesPerSector, 0, FILE_BEGIN)
ReDim abResult(cBytes - 1)
ReDim abBuff(1 * BytesPerSector - 1)
Call ReadFile(hDevice, abBuff(0), UBound(abBuff) + 1, nRead, 0&)
CopyMemory abResult(0), abBuff(0), cBytes
rawread = StrConv(abResult, vbUnicode)
If LastSectorToRead = True Then CloseHandle hDevice: MsgBox "end"
End Function
Public Function rawwrite(ByVal rDrive As String, ByVal SectorToWrite As Integer, ByVal writestring As String, ByVal LastSectorToWrite As Boolean) As Integer
If Len(rDrive) < 4 Then rDrive = Left(rDrive, 2)
Dim BytesPerSector As Long
Dim abBuff() As Byte
Dim abResult() As Byte
Dim nSectors As Long
Dim nWritten As Long
abResult = writestring
iOffset = SectorToWrite
cBytes = 512
BytesPerSector = 512
If hDevice < 1 Then hDevice = CreateFile("\\.\A:", GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
If hDevice = INVALID_HANDLE_VALUE Then rawwrite = 0: Exit Function
ReDim abBuff(1 * BytesPerSector - 1)
CopyMemory abBuff(0), abResult(0), Len(abResult(0))
Call SetFilePointer(hDevice, SectorToWrite * BytesPerSector, 0, FILE_BEGIN)
Call WriteFile(hDevice, abResult(0), BytesPerSector, nWritten, ByVal 0&)
rawwrite = nWritten
If LastSectorToWrite = True Then CloseHandle hDevice
End Function