用VB对磁盘的物理扇区数据读/写操作

原创 2004年08月13日 01:37:00

Option Explicit

'/////////////////////////////////////////
'// 对磁盘的物理扇区数据读/写操作
'// last update: 2004-8-7
'// Kwanhong Young
'/////////////////////////////////////////


'//file system
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Long, 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 ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long '//declare has changed
Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long '//declare has changed
Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long

Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000

Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2
Private Const OPEN_EXISTING = 3

Private Const INVALID_HANDLE_VALUE = -1

'//file seek
Private Const FILE_BEGIN = 0
Private Const FILE_CURRENT = 1
Private Const FILE_END = 2

Private Const ERROR_SUCCESS = 0&

'//device io control
Private Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, lpInBuffer As Any, ByVal nInBufferSize As Long, lpOutBuffer As Any, ByVal nOutBufferSize As Long, lpBytesReturned As Long, ByVal lpOverlapped As Long) As Long

Private Const IOCTL_DISK_GET_DRIVE_GEOMETRY As Long = &H70000   '458752
Private Const IOCTL_STORAGE_GET_MEDIA_TYPES_EX  As Long = &H2D0C04
Private Const IOCTL_DISK_FORMAT_TRACKS As Long = &H7C018
Private Const FSCTL_LOCK_VOLUME As Long = &H90018
Private Const FSCTL_UNLOCK_VOLUME As Long = &H9001C
Private Const FSCTL_DISMOUNT_VOLUME As Long = &H90020
Private Const FSCTL_GET_VOLUME_BITMAP = &H9006F

'//type
Private Type LARGE_INTEGER
    lowpart As Long
    highpart As Long
End Type

Private Enum 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
End Enum

Private Type DISK_GEOMETRY
    Cylinders           As LARGE_INTEGER
    MediaType           As MEDIA_TYPE
    TracksPerCylinder   As Long
    SectorsPerTrack     As Long
    BytesPerSector      As Long
End Type

'//private vars
Private hDisk           As Long             'disk handle
Private lpGeometry      As DISK_GEOMETRY    'disk info
Private lBufferSize     As Long             'the buffer size of read/write

Public Function OpenDisk(ByVal FileName As String) As Boolean
'// 打开磁盘
    hDisk = CreateFile(FileName, _
                        GENERIC_READ Or GENERIC_WRITE, _
                        FILE_SHARE_READ Or FILE_SHARE_WRITE, _
                        ByVal 0&, _
                        OPEN_EXISTING, _
                        0, _
                        0)
    OpenDisk = Not (hDisk = INVALID_HANDLE_VALUE)
End Function

Public Function CloseDisk() As Boolean
'//关闭磁盘
    CloseDisk = CloseHandle(hDisk)
End Function

Public Function GetDiskGeometry() As Boolean
'//获取磁盘参数
    Dim dwOutBytes      As Long
    Dim bResult         As Boolean
   
    bResult = DeviceIoControl(hDisk, _
                                IOCTL_DISK_GET_DRIVE_GEOMETRY, _
                                ByVal 0&, 0, _
                                lpGeometry, Len(lpGeometry), _
                                dwOutBytes, _
                                ByVal 0&)
   
    If bResult Then lBufferSize = lpGeometry.BytesPerSector * lpGeometry.SectorsPerTrack
    GetDiskGeometry = bResult
End Function

Public Sub GetDiskInfo(MediaType As Long, _
                        Cylinders As Long, _
                        TracksPerCylinder As Long, _
                        SectorsPerTrack As Long, _
                        BytesPerSector As Long)
'//返回磁盘的参数
    MediaType = lpGeometry.MediaType
    Cylinders = lpGeometry.Cylinders.lowpart
    TracksPerCylinder = lpGeometry.TracksPerCylinder
    SectorsPerTrack = lpGeometry.SectorsPerTrack
    BytesPerSector = lpGeometry.BytesPerSector

End Sub

Public Property Get BufferSize() As Long
'//返回每次读/写的缓冲大小
    BufferSize = lBufferSize
End Property


Public Function LockVolume() As Boolean
'// 将卷锁定
    Dim dwOutBytes  As Long
    Dim bResult     As Boolean
   
    bResult = DeviceIoControl(hDisk, _
                                FSCTL_LOCK_VOLUME, _
                                ByVal 0&, 0, _
                                ByVal 0&, 0, _
                                dwOutBytes, _
                                ByVal 0&)
    LockVolume = bResult
End Function


Public Function UnlockVolume() As Boolean
'// 将卷解锁
    Dim dwOutBytes As Long
    Dim bResult As Boolean
   
    bResult = DeviceIoControl(hDisk, _
                                FSCTL_UNLOCK_VOLUME, _
                                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

[转贴] VB对磁盘的物理扇区数据读/写操作

Option Explicit ///////////////////////////////////////// // 对磁盘的物理扇区数据读/写操作 // last update: 2004-8-...
  • lxcom
  • lxcom
  • 2008-06-20 17:34:00
  • 324

物理扇区和逻辑扇区(相对扇区)的关系

物理扇区(就是磁盘里的扇区划分):分为柱面(也就是磁道,竖着一摞,同一条上的磁道就是柱面了),磁头(也叫盘面),扇区。 柱面、磁头、扇区都有按数字编号,柱面号和磁头号从0开始,扇区号从1开始,假定划...
  • hhdfh
  • hhdfh
  • 2016-06-05 19:20:52
  • 2506

Linux中如何读写硬盘上指定物理扇区

读指定物理扇区: dd  if=  of=   skip=  bs=512 count=1 写指定物理扇区: dd   if=   of=   seek=  bs=512 c...
  • xshalk
  • xshalk
  • 2013-12-30 22:15:08
  • 6919

VB顺序文件读写操作实例

  • 2015年07月27日 20:54
  • 6KB
  • 下载

linux读取硬盘指定扇区

主要参考:http://www.linuxforum.net/forum/showflat.php?Cat=&Board=linuxK&Number=288776&fpart=all http://...
  • enlangs
  • enlangs
  • 2013-12-12 12:06:36
  • 1393

Windows物理磁盘扇区读写示例总结

Windows物理磁盘扇区读写、FSCTL_LOCK_VOLUME、FSCTL_UNLOCK_VOLUME、FSCTL_DISMOUNT_VOLUME
  • good123day
  • good123day
  • 2017-12-17 22:51:35
  • 71

4KB大扇区硬盘的性能分析

 前段时间,我发了一篇文章《大扇区硬盘的出现与分析》来说明09年出现的大扇区的硬盘,并且也受到一些读者的来信询问,这里,转载一篇老外的文章,再次说明一下4K大扇区硬盘,希望可以让你对4K大扇区硬盘有进...
  • fjb2080
  • fjb2080
  • 2010-05-27 09:31:00
  • 12614

linux基础教程:文件系统和磁盘

(1)文件系统磁盘设备的使用有三个步骤,第一创建新分区,第二指定文件系统,第三进行挂载。没有文件系统的设备被称为裸设备创建文件系统的过程叫做格式化常见的文件系统fat32,ntfs,是windows所...
  • define_us
  • define_us
  • 2016-08-25 20:06:31
  • 279

volatile变量规则:对一个变量的写操作先行发生于后面对这个变量的读操作

如果单看一句话的,基本上是无法理解的。这句话的意思,并不是“对于一个变量a,a=5这样的写操作,在b=a这样的读操作之前执行”的意思,这句话实际上就是体现happens-before原则具备传递性。想...
  • hotdust
  • hotdust
  • 2017-02-16 22:49:54
  • 642

linux 下得到磁盘物理扇区大小

现在2T以上的磁盘其物理扇区基本上是4K大小的,在linux下要得到磁盘的物理扇区大小有如下几种方式: 1.使用hdparm 工具,hdparm -I 直接查看磁盘原始提供的物理和逻辑扇区大小。 ...
  • lkkey80
  • lkkey80
  • 2013-02-04 22:09:42
  • 2340
收藏助手
不良信息举报
您举报文章:用VB对磁盘的物理扇区数据读/写操作
举报原因:
原因补充:

(最多只允许输入30个字)