Attribute VB_Name = "Affxh"
Global Const CLibId = 163402    'don't change this constant!

Declare Function DiskOpInit Lib "fformatx.dll" (ByVal Drive As Long, ByVal FloppyType As Long, _
    ByVal VolumeLab As String, ByVal NeedVerify As Boolean, ByRef LibId As Long) As Long
Declare Function FormatCylinder Lib "fformatx.dll" (ByVal Cyl As Long) As Long
Declare Function DiskCompletion Lib "fformatx.dll" (ByVal NeedFatsAndRoot As Boolean, ByVal LibId As Long) As Long
Declare Function GetFloppyType Lib "fformatx.dll" (ByVal Drive As Long, ByRef ErrNo As Long) As Long

Public Function FloppyFormat() As String

'------------------------------------------------------------
    Dim Res, Cylinder As Long
    Dim DriveChar, FormatErrStr, VolLabel As String
    Dim FloppyTypeStr As String
    Dim NeedVerify, NeedFatsAndRoot As Boolean
    Dim DriveByte, FloppyType, LibId, ErrNo As Long
'------------------------------------------------------------
  
    DriveChar = "a"
    VolLabel = Form1.Text1.Text
    NeedVerify = (Form1.Check1.Value = 1)
    LibId = CLibId
    
    FloppyType = GetFloppyType(DriveByte, ErrNo)
    
    Select Case FloppyType
    Case 1
        FloppyTypeStr = "360KB"
    Case 2
        FloppyTypeStr = "1.2MB"
    Case 3
        FloppyTypeStr = "720KB"
    Case 4
        FloppyTypeStr = "1.44MB"
    Case 5
        FloppyTypeStr = "1.72MB"
    Case 6
        FloppyTypeStr = "DMF1"
    Case 7
        FloppyTypeStr = "DMF2"
    Case Else
        FloppyTypeStr = "Unknown"
    End Select
    
    Select Case ErrNo
    Case 0
        Form1.Label1.Caption = "Old capacity: " + FloppyTypeStr + ".  Disk is empty."
    Case 1
        Form1.Label1.Caption = "Old capacity: " + FloppyTypeStr + ".  Disk is NOT empty."
    Case 2
        Form1.Label1.Caption = "No disk in the drive OR disk is not formatted."
    Case 3
        Form1.Label1.Caption = "No disk in the drive."
    End Select
    
    If ErrNo <> 3 Then
        'FloppyType = 3   '720KB
        FloppyType = 4   '1.44MB
        'FloppyType = 6   'DMF1
        'FloppyType = 7   'DMF2
        
    Select Case FloppyType
    Case 3
        FloppyTypeStr = "720KB"
    Case 4
        FloppyTypeStr = "1.44MB"
    Case 6
        FloppyTypeStr = "DMF1"
    Case 7
        FloppyTypeStr = "DMF2"
    Case Else
        FloppyTypeStr = "Not supported!"
    End Select
    
    Form1.Label2.Caption = "New capacity: " + FloppyTypeStr
        
    Res = DiskOpInit(DriveByte, FloppyType, VolLabel, NeedVerify, LibId)
        
    If Res <> 0 Then
        Form1.Label5.Caption = "Initialization error!"
    Else
        Form1.Label5.Caption = "Initialization completed."
        Cylinder = 0
        Res = FormatCylinder(Cylinder)
      
        If Res = 0 Then Form1.Label3.Caption = "0"
  
        Select Case Res
        Case 1
            ErrStr = "The drive is not ready."
        Case 2
            ErrStr = "No disk in the drive."
        Case 3
            ErrStr = "The disk is write protected."
        Case Else
            Form1.Label5.Caption = "Formatting..."
            While (Cylinder < 79) And (Res = 0)
                Cylinder = Cylinder + 1
                Res = FormatCylinder(Cylinder)
                Form1.Label3.Caption = Cylinder
            Wend
            If Res = 0 Then
                FormatErrStr = "End of Tracks Formatting..."
            Else
                FormatErrStr = "Unknown error"
            End If
        End Select
        
        ErrNo = DiskCompletion(True, LibId)
        
        Form1.Label5.Caption = FormatErrStr
    End If
 
    If Res = 0 Then
       FloppyFormat = "Ok"
    Else
       FloppyFormat = "Error"
    End If
    
    End If
    
End Function

