Skip to content

Instantly share code, notes, and snippets.

@hsiboy
Created April 16, 2022 20:58
Show Gist options
  • Save hsiboy/eb0c67749a00d3f2958da177e9588bcb to your computer and use it in GitHub Desktop.
Save hsiboy/eb0c67749a00d3f2958da177e9588bcb to your computer and use it in GitHub Desktop.
Write to a floppy disk using native visual basic (calling kernel32)
'Ripped from a post by The Dragon (Alexander) 2003
'This code is free to use but pls contact me when you use or like it
'This only works under NT/2k/XP (all NT based OS's I think)
'u can contact me for support
'[email protected]
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
Private Sub Command1_Click()
Dim hDevice As Long
Dim BytesPerSector As Long
iOffset = 0
cBytes = 512
BytesPerSector = 512
Dim abBuff() As Byte
Dim abResult() As Byte
Dim nSectors As Long
Dim nWritten As Long
Dim writer As String
nSectors = Int((iOffset + cBytes - 1) / BytesPerSector) + 1
hDevice = CreateFile("\\.\A:", GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
For i = 0 To (2880 - 1)
DoEvents
If hDevice = INVALID_HANDLE_VALUE Then Exit Sub
Open "d:\floppy.bin" For Binary Access Read As #1
writer = Space(512)
Get #1, i * 512 + 1, writer
abResult = StrConv(writer, vbFromUnicode)
Close #1
ReDim abBuff(nSectors * BytesPerSector - 1)
CopyMemory abBuff(iOffset), abResult(0), Len(abResult(0))
Call SetFilePointer(hDevice, i * BytesPerSector, 0, FILE_BEGIN)
Call WriteFile(hDevice, abResult(0), BytesPerSector, nWritten, ByVal 0&)
Next i
CloseHandle hDevice
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment