Using VB to read/write the physical sector data of disk

Source: Internet
Author: User
Tags array file system readfile
Data Option Explicit

'/////////////////////////////////////////
'//To disk physical sector data read/write operations
'//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 Dwflagsandatt Ributes as Long, ByVal hTemplateFile 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 Lo Ng, 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, Lpdistancetomo Vehigh as Long, ByVal Dwmovemethod 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 Lpov Erlapped 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
'//Open Disk
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
'//Turn off disk
Closedisk = CloseHandle (Hdisk)
End Function

Public Function getdiskgeometry () as Boolean
'//Get disk parameters
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)
'//Return the parameters of the disk
mediatype = Lpgeometry.mediatype
Cylinders = LpGeometry.Cylinders.lowpart
Trackspercylinder = Lpgeometry.trackspercylinder
Sectorspertrack = Lpgeometry.sectorspertrack
BytesPerSector = Lpgeometry.bytespersector

End Sub

Public Property Get BufferSize () as Long
'//returns the buffer size for each read/write
BufferSize = Lbuffersize
End Property


Public Function Lockvolume () as Boolean
'//Lock the volume
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
'//Unlock the volume
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
'//Remove the volume so that the system will recognize the disk, which is equivalent to reseating the disc
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
'//Read disk data by cylinder and track
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
'//write disk data by cylinder and track
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 would 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




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.