'/////////////////////////////////////////
'//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
'//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 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
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
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
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
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.