Initialising a floppy disk using Win32 API

Windows specific forum
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Initialising a floppy disk using Win32 API

Post by Dude »

fryquez wrote:why in the year 2016 you need floppies
Virtual PCs and emulators. They use floppy images.
User avatar
JHPJHP
Addict
Addict
Posts: 2258
Joined: Sat Oct 09, 2010 3:47 am

Re: Initialising a floppy disk using Win32 API

Post by JHPJHP »

Hi Dreamland Fantasy,

Here is the script mentioned in my previous post; not sure if it offers the control you require?

I read your post that you've solved the "Format Tracks" problem, but for completeness I decided to include my untested version anyway.

Code: Select all

Enumeration MEDIA_TYPE
  #Unknown
  #F5_1Pt2_512
  #F3_1Pt44_512
  #F3_2Pt88_512
  #F3_20Pt8_512
  #F3_720_512
  #F5_360_512
  #F5_320_512
  #F5_320_1024
  #F5_180_512
  #F5_160_512
  #RemovableMedia
  #FixedMedia
  #F3_120M_512
  #F3_640_512
  #F5_640_512
  #F5_720_512
  #F3_1Pt2_512
  #F3_1Pt23_1024
  #F5_1Pt23_1024
  #F3_128Mb_512
  #F3_230Mb_512
  #F8_256_128
  #F3_200Mb_512
  #F3_240M_512
  #F3_32M_512
EndEnumeration

#IOCTL_DISK_FORMAT_TRACKS = $7C018

Structure FORMAT_PARAMETERS
  MediaType.l
  StartCylinderNumber.l
  EndCylinderNumber.l
  StartHeadNumber.l
  EndHeadNumber.l
EndStructure

Procedure.s TestForError()
  dwMessageId = GetLastError_()
  *lpBuffer = AllocateMemory(#MAX_PATH)
  FormatMessage_(#FORMAT_MESSAGE_FROM_SYSTEM, #Null, dwMessageId, #Null, *lpBuffer, MemorySize(*lpBuffer), #Null)
  dwDescription.s = PeekS(*lpBuffer, -1, #PB_Unicode)
  Result.s = "Error: " + Str(dwMessageId) + " - " + Left(dwDescription, StringByteLength(dwDescription) - 1)
  FreeMemory(*lpBuffer)
  ProcedureReturn Result
EndProcedure

Procedure.s FormatTracks(lpFileName.s)
  hDevice = CreateFile_("\\?\" + lpFileName, #GENERIC_READ | #GENERIC_WRITE, #FILE_SHARE_READ | #FILE_SHARE_WRITE, #Null, #OPEN_EXISTING, 0, #Null)

  If hDevice <> #INVALID_HANDLE_VALUE
    lpInBuffer.FORMAT_PARAMETERS
    lpInBuffer\MediaType = #F3_1Pt44_512
    lpInBuffer\StartCylinderNumber = 0
    lpInBuffer\EndCylinderNumber = 79
    lpInBuffer\StartHeadNumber = 0
    lpInBuffer\EndHeadNumber = 1
    DeviceIoControl_(hDevice, #IOCTL_DISK_FORMAT_TRACKS, @lpInBuffer, SizeOf(lpInBuffer), #Null, 0, #Null, #Null)
    CloseHandle_(hDevice)
    ProcedureReturn TestForError()
  Else
    ProcedureReturn "Invalid Handle"
  EndIf
EndProcedure

Procedure.s FormatDisk(DriveLetter.s, FormatOptions.s)
  lpCommandLine.s = "format.com "
  *lpCommandLine = AllocateMemory(StringByteLength(lpCommandLine, #PB_Unicode) +
                                  ((StringByteLength(DriveLetter, #PB_Unicode) + 1) << 1) +
                                  (StringByteLength(FormatOptions, #PB_Unicode) << 1))
  lpCommandLine + DriveLetter + " " + FormatOptions
  CopyMemory(@lpCommandLine, *lpCommandLine, MemorySize(*lpCommandLine))
  lpStartupInfo.STARTUPINFO
  lpProcessInformation.PROCESS_INFORMATION

  If CreateProcess_(#Null, *lpCommandLine, #Null, #Null, #True, #NORMAL_PRIORITY_CLASS | #CREATE_NO_WINDOW, #Null, #Null, @lpStartupInfo, @lpProcessInformation)
    WaitForSingleObject_(lpProcessInformation\hProcess, #INFINITE)
    CloseHandle_(lpProcessInformation\hProcess)
    CloseHandle_(lpProcessInformation\hThread)
  EndIf
  ProcedureReturn TestForError()
EndProcedure

DriveLetter.s = "D:"
DriveFormat.s = "FAT32"
FormatOptions.s = "/FS:" + DriveFormat + " /Q /V:JHPJHP /X /Y"
rtnMessage = MessageRequester("Format Drive", "Do you want to format drive [ " + DriveLetter + " ] to [ " + DriveFormat + " ] ?", #PB_MessageRequester_YesNo | #MB_ICONQUESTION | #MB_DEFBUTTON2)

If rtnMessage = #PB_MessageRequester_Yes
  Debug FormatTracks(DriveLetter)
  Debug FormatDisk(DriveLetter, FormatOptions)
EndIf

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
User avatar
Dreamland Fantasy
Enthusiast
Enthusiast
Posts: 335
Joined: Fri Jun 11, 2004 9:35 pm
Location: Glasgow, UK
Contact:

Re: Initialising a floppy disk using Win32 API

Post by Dreamland Fantasy »

Thanks for that JHPJHP. :)

I am trying to do it through the API, but it could be a good workaround if I can't get it working.

Kind regards,

Francis
Post Reply