Skip to content

Instantly share code, notes, and snippets.

@hsiboy
Created April 16, 2022 21:02
Show Gist options
  • Save hsiboy/e04db2e88a8167e8d045fe90b56d429e to your computer and use it in GitHub Desktop.
Save hsiboy/e04db2e88a8167e8d045fe90b56d429e to your computer and use it in GitHub Desktop.

Revisions

  1. hsiboy created this gist Apr 16, 2022.
    44 changes: 44 additions & 0 deletions read_floppy.vb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    'Ripped from a post by Dragon <[email protected]> 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