VERSION 5.00
Begin VB.Form Form10 
   Caption         =   " Physical Disk Write"
   ClientHeight    =   3855
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   3480
   Icon            =   "physicaldiskwrite.frx":0000
   LinkTopic       =   "Form10"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   3855
   ScaleWidth      =   3480
   StartUpPosition =   3  'Windows Default
   Begin VB.OptionButton Option3 
      Caption         =   "Write current sector"
      Height          =   255
      Left            =   240
      TabIndex        =   4
      Top             =   1320
      Value           =   -1  'True
      Width           =   2535
   End
   Begin VB.OptionButton Option2 
      Caption         =   "Write from current byte position"
      Height          =   255
      Left            =   240
      TabIndex        =   3
      Top             =   1005
      Width           =   2535
   End
   Begin VB.OptionButton Option1 
      Caption         =   "Write from beginning of the file"
      Height          =   255
      Left            =   240
      TabIndex        =   2
      Top             =   720
      Width           =   2535
   End
   Begin VB.TextBox Text1 
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   9.75
         Charset         =   162
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   360
      Left            =   1800
      MaxLength       =   10
      TabIndex        =   6
      Text            =   "0"
      Top             =   1800
      Width           =   1095
   End
   Begin VB.TextBox Text2 
      Enabled         =   0   'False
      BeginProperty Font 
         Name            =   "MS Sans Serif"
         Size            =   9.75
         Charset         =   162
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   360
      Left            =   1800
      MaxLength       =   7
      TabIndex        =   8
      Text            =   "1"
      Top             =   2280
      Width           =   855
   End
   Begin VB.CommandButton Command1 
      Caption         =   "Write"
      Height          =   375
      Left            =   1800
      TabIndex        =   10
      Top             =   3000
      Width           =   1455
   End
   Begin VB.CommandButton Command2 
      Caption         =   "Cancel"
      Height          =   375
      Left            =   240
      TabIndex        =   9
      Top             =   3000
      Width           =   1335
   End
   Begin VB.ComboBox Combo1 
      Height          =   315
      Left            =   1560
      TabIndex        =   1
      Text            =   "PhysicalDrive0"
      Top             =   240
      Width           =   1695
   End
   Begin VB.Label Label2 
      Caption         =   "Beginning Sector :"
      Height          =   255
      Left            =   240
      TabIndex        =   5
      Top             =   1860
      Width           =   1335
   End
   Begin VB.Label Label3 
      Caption         =   "Number of Sectors :"
      Height          =   255
      Left            =   240
      TabIndex        =   7
      Top             =   2340
      Width           =   1455
   End
   Begin VB.Label Label1 
      Caption         =   "Physical  Drive :"
      Height          =   255
      Left            =   240
      TabIndex        =   0
      Top             =   285
      Width           =   1215
   End
End
Attribute VB_Name = "Form10"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim C1LI As Integer
Dim CSectors As Long
Dim DriveName As String

Private Sub Combo1_Change()
Combo1.ListIndex = C1LI
End Sub

Private Sub Combo1_Click()
C1LI = Combo1.ListIndex
End Sub

Private Sub Combo1_LostFocus()
DriveName = Combo1.Text
End Sub

Private Sub Command1_Click()
Rem MsgBox ("Program is not ready to direct disk write, yet...")
Rem Exit Sub
Rem 8-11-2008 Erdogan Tan
Rem Emergency Stop, Temporary
Rem If DriveName = "PhysicalDrive0" Then Exit Sub
Rem On Error GoTo ErrMsg
Rem Close TmpFileNo
Rem SF = -1
Rem CX = 512
Rem TmpFileNo = FreeFile
Rem Open "BINFILEHEX.TMP" For Binary As TmpFileNo Len = 1
Rem FL = FileLen("BINFILEHEX.TMP")
Rem FP = 0
Rem FF = 1
Rem SFP = 0
Rem SL = 0
Dim Y As Long
Dim Z As Long
If Option3.Value = True Then
   Dim I As Integer
   Dim T As String
   Dim T1 As String
   Dim T2 As String
   Dim Y1 As Integer
   Dim Y2 As Integer
   Dim Y0 As Integer
   Y = CLng(Val(Text1.Text))
   If MsgBox("Do you want to write current sector on Drive " + DriveName + " sector " + CStr(Y), 36, " ! Warning ! Direct Disk Write is about beginning !") <> 6 Then Exit Sub
   Rem 20-5-2009 Erdogan Tan
   ReDim abResult(511)
   If EditMode = True Then
      Form1.Text1(LastIndex).BackColor = 0
      Form1.Text2(LastIndex).BackColor = 0
      EditMode = False
      Form1.Text1(LastIndex).Text = Form1.Text2(LastIndex).Tag
      Form1.Text2(LastIndex).Text = Chr$(Form1.HexToDecimal(Form1.Text2(LastIndex).Tag))
   End If
   For I = 0 To 511
       T = Form1.Text1(I).Text
       If Trim$(T) = "" Then T = "00"
       T1 = Left$(T, 1)
       T2 = Right$(T, 1)
       Y1 = Asc(T1)
       If Val(T1) = 0 And T1 <> "0" Then
          If Y1 >= Asc("A") And Y1 <= Asc("F") Then
             Y1 = 10 + Y1 - Asc("A")
          Else
             Y1 = 0
          End If
       Else
          Y1 = Y1 - Asc("0")
       End If
       Y2 = Asc(T2)
       If Val(T2) = 0 And T2 <> "0" Then
          If Y2 >= Asc("A") And Y2 <= Asc("F") Then
             Y2 = 10 + Y2 - Asc("A")
          Else
             Y2 = 0
          End If
       Else
          Y2 = Y2 - Asc("0")
       End If
       Y0 = (Y1 * 16) + Y2
       abResult(I) = CByte(Y0)
    Next I
    Z = DirectWriteDriveNT(DriveName, Y, 0, abResult(), 512)
    If Z < 1 Then GoTo MsgInvalidHandle
    MsgBox ("Current 512 bytes have been written to sector " + CStr(Y) + " of Drive " + DriveName + " ..."), 0, " Physical Disk Write"
    For I = 0 To 511
       Form1.Text1(I).BackColor = &HFFFFFF
       Form1.Text1(I).ForeColor = 0
       Form1.Text2(I).BackColor = &HFFFFFF
       Form1.Text2(I).ForeColor = &H808080
    Next I
    GoTo Cikis
Else
Dim X As Long
Dim sc As Long
Dim cs As Long
Dim bc As Long
   If Option2.Value = True Then
      cs = FF
   End If
End If
bs = Val(Text1.Text)
If bs < 0 Then
   bs = 0
   Text1.Text = 0
End If
sc = Val(Text2.Text)
If sc < 1 Then
   sc = 1
   Text2.Text = 1
Else
   If sc > CSectors Then
      sc = CSectors
      Text2.Text = sc
   End If
End If
If sc > 1 Then
   If MsgBox("Do you want to write " + CStr(sc) + " sectors on Drive " + DriveName + " ... from sector " + CStr(bs) + " ...", 36, " ! Warning ! Direct Disk Write is about beginning !") <> 6 Then Exit Sub
Else
   If MsgBox("Do you want to write sector " + CStr(bs) + " on Drive " + DriveName, 36, " ! Warning ! Direct Disk Write is about beginning !") <> 6 Then Exit Sub
End If
Rem 8-11-2008 Erdogan Tan
ReDim abResult(511)
For Y = bs To bs + sc - 1
    bc = FL - cs
    If bc > 512 Then
       bc = 512
    Else
       If bc < 1 Then Exit For
    End If
    For X = 0 To bc - 1
        cs = cs + 1
        Get TmpFileNo, cs, abResult(X)
    Next X
    Z = DirectWriteDriveNT(DriveName, Y, 0, abResult(), bc)
    If Z < 1 Then Exit For
Next Y
If cs > 0 Then
   If sc > 1 Then
      MsgBox (CStr(cs) + " bytes in " + CStr(sc) + " sectors have been written on Drive " + DriveName + " ... from sector " + CStr(bs) + " ..."), 0, " Physical Disk Write"
   Else
      MsgBox (CStr(cs) + " bytes have been written to sector " + CStr(bs) + " of Drive " + DriveName + " ..."), 0, " Physical Disk Write"
   End If
   DF = True
   DRV = DriveName
   Call Form1.ShowFileBinHex
Else
MsgInvalidHandle:
   MsgBox ("Invalid handle value !" + Chr$(13) + Chr$(10) + "(Disk not ready or write error.)"), 48, " Physical Disk " + Right(DriveName, 1) + " Write Error !"
   DF = False
End If
Cikis:
Unload Me
Exit Sub
ErrMsg:
Beep
MsgBox ("Drive could not write!..." + Chr$(13) + Chr$(13) + "Error Number: " + Str$(Err)), 48, "! ERROR !"
Resume Cikis
End Sub

Private Sub Command2_Click()
Unload Me
End Sub

Private Sub Form_Load()
Rem 4-11-2008
Combo1.AddItem "PhysicalDrive0"
Combo1.AddItem "PhysicalDrive1"
Combo1.AddItem "PhysicalDrive2"
Combo1.AddItem "PhysicalDrive3"
Combo1.ListIndex = 0
DriveName = "PhysicalDrive0"
On Error Resume Next
If DF = True Then
   Text1.Text = CStr(Sector + CInt((FP - 1) / 512))
End If
End Sub

Private Sub Option1_Click()
Text2.Enabled = True
End Sub

Private Sub Option2_Click()
Text2.Enabled = True
End Sub

Private Sub Option3_Click()
Text2.Enabled = False
End Sub

Private Sub Text1_GotFocus()
Text1.SelLength = Text1.MaxLength
End Sub

Private Sub Text2_GotFocus()
Text2.SelLength = Text2.MaxLength
End Sub
