打印本文 打印本文 关闭窗口 关闭窗口
用VB对磁盘的物理扇区数据读/写操作
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2038  更新时间:2009/4/23 15:43:29  文章录入:mintao  责任编辑:mintao
                   ByVal 0&, 0, _
                                ByVal 0&, 0, _
                                dwOutBytes, _
                                ByVal 0&)
    UnlockVolume = bResult
End Function


Public Function DismountVolume() As Boolean
''''// 将卷卸下,使系统重新辨识磁盘,等效于重新插盘
    Dim dwOutBytes As Long
    Dim bResult As Boolean
   
    bResult = DeviceIoControl(hDisk, _
                                FSCTL_DISMOUNT_VOLUME, _
                                ByVal 0&, 0, _
                                ByVal 0&, 0, _
                                dwOutBytes, _
                                ByVal 0&)
    DismountVolume = bResult
End Function


Public Function ReadDisk(ByVal Cylinders As Long, _
                    ByVal Tracks As Long, _
                    db() As Byte) As Boolean
''''//按柱面和磁道来读取磁盘数据
    Dim iPos    As Long
    Dim lRead   As Long
   
    iPos = Cylinders * Tracks * lBufferSize
   
    If SeekAbsolute(0, iPos) Then
        ReadDisk = ReadBytes(lBufferSize, db(), lRead)
    End If
End Function

Public Function WriteDisk(ByVal Cylinders As Long, _
                     ByVal Tracks As Long, _
                     db() As Byte) As Boolean
''''//按柱面和磁道来写磁盘数据
    Dim iPos    As Long
    Dim lRead   As Long
   
    iPos = Cylinders * Tracks * lBufferSize
   
    If SeekAbsolute(0, iPos) Then
        WriteDisk = WriteBytes(lBufferSize, db())
    End If
End Function


''''/////////////////////////////////////////////////////////////////////////////////////
''''//file system

Private Function SeekAbsolute(ByVal HighPos As Long, ByVal LowPos As Long) As Boolean
''''//seek file
    ''''//Notice: when you set LowPos=5, the read/write will begin with the 6th(LowPos+1) byte
    LowPos = SetFilePointer(hDisk, LowPos, HighPos, FILE_BEGIN)
    If LowPos = -1 Then
        SeekAbsolute = (Err.LastDllError = ERROR_SUCCESS)
    Else
        SeekAbsolute = True
    End If
   
End Function


Private Function ReadBytes(ByVal ByteCount As Long, ByRef DataBytes() As Byte, ByRef ActuallyReadByte As Long) As Boolean
''''//read data to array
    Dim RetVal    As Long
    RetVal = ReadFile(hDisk, DataBytes(0), ByteCount, ActuallyReadByte, 0)
    ''''ActuallyReadByte =>> if the bytesRead=0 mean EOF
    ReadBytes = Not (RetVal = 0)
   
End Function

Private Function WriteBytes(ByVal ByteCount As Long, ByRef DataBytes() As Byte) As Boolean
''''//write data from array
    Dim RetVal As Long
    Dim BytesToWrite As Long
    Dim BytesWritten As Long
   
    RetVal = WriteFile(hDisk, DataBytes(0), ByteCount, BytesWritten, 0)
   
    WriteBytes = Not (RetVal = 0)
End Function

上一页  [1] [2] 

打印本文 打印本文 关闭窗口 关闭窗口