Skip to content

Instantly share code, notes, and snippets.

@hsiboy
Created April 16, 2022 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hsiboy/e04db2e88a8167e8d045fe90b56d429e to your computer and use it in GitHub Desktop.
Save hsiboy/e04db2e88a8167e8d045fe90b56d429e to your computer and use it in GitHub Desktop.
read from a floppy drive using native visual basic (calling kernel32)
'Ripped from a post by Dragon <thedragon@dewieden.org> 2003
' - Also thanks to the KPD-Team and Arkadiy Olovyannikov
'
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 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 Sub Command1_Click()
Dim hDevice As Long
iOffSet = 0
cBytes = 512
BytesPerSector = 512
Dim abBuff() As Byte
Dim abResult() As Byte
Dim nSectors As Long
Dim nRead As Long
Dim writer As String
nSectors = Int((iOffSet + cBytes - 1) / BytesPerSector) + 1
hDevice = CreateFile("\\.\A:", GENERIC_READ Or GENERIC_WRITE, 0, ByVal 0, OPEN_EXISTING, 0, 0)
For i = 0 To (2880 - 1)
DoEvents
If hDevice = INVALID_HANDLE_VALUE Then Exit Sub
Call SetFilePointer(hDevice, i * BytesPerSector, 0, FILE_BEGIN)
ReDim abResult(cBytes - 1)
ReDim abBuff(nSectors * BytesPerSector - 1)
Call ReadFile(hDevice, abBuff(0), UBound(abBuff) + 1, nRead, 0&)
CopyMemory abResult(0), abBuff(iOffSet), cBytes
Open "c:\floppy.bin" For Binary Access Write As #1
writer = StrConv(abResult, vbUnicode)
Put #1, i * 512 + 1, writer
Close #1
Next i
CloseHandle hDevice
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment