Public Function ReadDrive(BytesToRead As Long)
Dim dh As Long, ret As Long
Dim strBuffer As String
Dim lngRead As Long
Dim bBytes() As Byte
Dim x As Long
ReDim bBytes(1 To BytesToRead) As Byte
dh = CreateFile("\\.\D:", GENERIC_READ + GENERIC_WRITE, FILE_SHARE_READ + FILE_SHARE_WRITE, ByVal CLng(0), OPEN_EXISTING, ByVal FILE_FLAG_NO_BUFFERING, 0)
If dh = -1 Then
MsgBox "Could not open drive D:"
End
End If
ReadFile dh, bBytes(1), UBound(bBytes), ret, ByVal 0&
Dim HexV As String
Dim StrV As String
Text1.Text = ""
Text2.Text = ""
For x = 1 To UBound(bBytes)
HexV = Hex(bBytes(x))
Text1.Text = Text1.Text & HexV & ","
StrV = Chr(bBytes(x))
Text2.Text = Text2.Text & StrV
Next x
End Function
In fact a little Read Sector Program that can read a sector off any HD. (READ ONLY) for safety
Code:
Const FILE_BEGIN = 0
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const CREATE_NEW = 1
Const OPEN_EXISTING = 3
Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Any) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal 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
Public Function ReadSector(Sector As Long, Drive As String)
Dim dh As Long, ret As Long
Dim strBuffer As String
Dim bBytes(512) As Byte
Dim HexV As String
Dim StrV As String
Dim x As Long
Sector = Sector * 512
Drive = UCase(Drive)
dh = CreateFile("\\.\" & Drive & ":", GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
If dh = -1 Then
MsgBox "Could not open drive " & Drive & ":"
End
End If
SetFilePointer dh, Sector, 0, FILE_BEGIN
ReadFile dh, bBytes(1), UBound(bBytes), ret, ByVal 0&
CloseHandle (dh)
Text1.Text = ""
Text2.Text = ""
For x = 1 To UBound(bBytes)
HexV = Hex(bBytes(x))
Text1.Text = Text1.Text & HexV & ","
StrV = Chr(bBytes(x))
Text2.Text = Text2.Text & StrV
Next x
End Function
Private Sub Command1_Click()
Dim tmp As String
tmp = Mid$(Drive1.Drive, 1, 1)
If (IsNumeric(SectTxt.Text) = False) Then
MsgBox "bad number)"
Exit Sub
End If
ReadSector SectTxt.Text, tmp
End Sub
Hello Atomsoft,
Thanks very much for the response and the sample code.
I did try the code and was able to read a USB stick. Great.
Next step is to write to the USB :-) and put some safetychecks in to prevent writing to the C-drive
Djuskal