vb读扇区问题

张3李4 2010-06-25 06:40:41

当扇区超过20万是就不可以读取了~~!

Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Long) As Long

count=200000 * 512
Call ReadFile(hDevice, buffer(0), count, hadRead, ByVal 0&)

麻烦各位高手指点~~!
谢谢了~~!
...全文
808 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
请叫我黎小白 2013-02-20
  • 打赏
  • 举报
回复
解决了么,能给下代码么。
请叫我黎小白 2013-02-20
  • 打赏
  • 举报
回复
能这么读么, 读不了啊。能共享一下代码么?361856299@qq.com
zhanghao810810 2010-08-30
  • 打赏
  • 举报
回复
帮顶一下。。。
张3李4 2010-07-06
  • 打赏
  • 举报
回复
Private Sub Command2_Click()
Lts.Caption = "正在数据恢复..."
Dim hDevice As Long
Dim hadRead As Long

Dim ii As Double
Dim i As Double

Dim buffer() As Byte


Dim mfile As String, mfile2 As String
Dim buff() As Byte


mfile = App.Path + "\Image.bin"

ii = FileLen(mfile)
ReDim buffer(ii)

Tsq.Text = ii / L4.Caption

ReDim buff(ii)

Open mfile For Binary As #1
Get #1, , buff
Close #1

For i = 1 To ii
buffer(i) = buff(i - 1)
Next


hDevice = CreateFile("\\.\" & tdisk1.Text, GENERIC_READ Or GENERIC_WRITE, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0&, 0&)

If hDevice <> -1 Then



Call WriteFile(hDevice, buffer(1), ii, hadRead, ByVal 0&)



End If


If Check1.Value = 1 Then
Call DismountVolume
End If
Call CloseHandle(hDevice)

Lts.Caption = "数据恢复完成!!."

End Sub
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 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 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

Private Sub lvButtons_H1_Click()
tDisk.Clear
Dim strDriveBuffer As String
Dim strDrives As String
Dim i As Long
Dim udtInfo As DEVICE_INFORMATION
Dim ii As Integer

strDriveBuffer = Space(240)
strDriveBuffer = Left$(strDriveBuffer, GetLogicalDriveStrings(Len(strDriveBuffer), strDriveBuffer))

For ii = 1 To 24
strDrives = "PHYSICALDRIVE" & ii

udtInfo = GetDevInfo(strDrives)

If udtInfo.Valid Then
Select Case udtInfo.BusType
Case BusTypeUsb: tDisk.AddItem "" & strDrives & "\" & "USB\" & Trim$(udtInfo.VendorID) & Trim$(udtInfo.ProductID)
Case BusType1394: tDisk.AddItem "" & strDrives & "\" & "1394\" & Trim$(udtInfo.VendorID) & Trim$(udtInfo.ProductID)
Case BusTypeAta: tDisk.AddItem "" & strDrives & "\" & "ATA\" & Trim$(udtInfo.VendorID) & Trim$(udtInfo.ProductID)
Case BusTypeAtapi: tDisk.AddItem "" & strDrives & "\" & "ATAPI\" & Trim$(udtInfo.VendorID) & Trim$(udtInfo.ProductID)
Case BusTypeFibre: tDisk.AddItem "" & strDrives & "\" & "FIBRE\" & Trim$(udtInfo.VendorID) & Trim$(udtInfo.ProductID)
Case BusTypeRAID: tDisk.AddItem "" & strDrives & "\" & "RAID\" & Trim$(udtInfo.VendorID) & Trim$(udtInfo.ProductID)
Case BusTypeScsi: tDisk.AddItem "" & strDrives & "\" & "SCSI\" & Trim$(udtInfo.VendorID) & Trim$(udtInfo.ProductID)
Case BusTypeSsa: tDisk.AddItem "" & strDrives & "\" & "SSA\" & Trim$(udtInfo.VendorID) & Trim$(udtInfo.ProductID)
Case BusTypeUnknown: tDisk.AddItem "" & strDrives & "\" & "Unknown\" & Trim$(udtInfo.VendorID) & Trim$(udtInfo.ProductID)

End Select


End If
Next

tDisk.Text = ""
End Sub

Private Sub tDisk_Change()
vt = InStr(tDisk.Text, "\")

If tDisk.Text <> "" Then
tdisk1.Text = Mid(tDisk.Text, 1, vt - 1)
Call diskinfo
End If
End Sub

张3李4 2010-07-06
  • 打赏
  • 举报
回复
Private Sub Form_Load()
Dim strDriveBuffer As String
Dim strDrives As String
Dim i As Long
Dim udtInfo As DEVICE_INFORMATION
Dim ii As Integer

strDriveBuffer = Space(240)
strDriveBuffer = Left$(strDriveBuffer, GetLogicalDriveStrings(Len(strDriveBuffer), strDriveBuffer))

For ii = 1 To 24
strDrives = "PHYSICALDRIVE" & ii

udtInfo = GetDevInfo(strDrives)

If udtInfo.Valid Then
Select Case udtInfo.BusType
Case BusTypeUsb: tDisk.AddItem "" & strDrives & "\" & "USB\" & Trim$(udtInfo.VendorID) & Trim$(udtInfo.ProductID)
Case BusType1394: tDisk.AddItem "" & strDrives & "\" & "1394\" & Trim$(udtInfo.VendorID) & Trim$(udtInfo.ProductID)
Case BusTypeAta: tDisk.AddItem "" & strDrives & "\" & "ATA\" & Trim$(udtInfo.VendorID) & Trim$(udtInfo.ProductID)
Case BusTypeAtapi: tDisk.AddItem "" & strDrives & "\" & "ATAPI\" & Trim$(udtInfo.VendorID) & Trim$(udtInfo.ProductID)
Case BusTypeFibre: tDisk.AddItem "" & strDrives & "\" & "FIBRE\" & Trim$(udtInfo.VendorID) & Trim$(udtInfo.ProductID)
Case BusTypeRAID: tDisk.AddItem "" & strDrives & "\" & "RAID\" & Trim$(udtInfo.VendorID) & Trim$(udtInfo.ProductID)
Case BusTypeScsi: tDisk.AddItem "" & strDrives & "\" & "SCSI\" & Trim$(udtInfo.VendorID) & Trim$(udtInfo.ProductID)
Case BusTypeSsa: tDisk.AddItem "" & strDrives & "\" & "SSA\" & Trim$(udtInfo.VendorID) & Trim$(udtInfo.ProductID)
Case BusTypeUnknown: tDisk.AddItem "" & strDrives & "\" & "Unknown\" & Trim$(udtInfo.VendorID) & Trim$(udtInfo.ProductID)

End Select


End If
Next

tDisk.Text = ""
End Sub


Private Sub Command1_Click()

Lts.Caption = "正在备份数据..."

Dim hDevice As Long
Dim hadRead As Long

hDevice = CreateFile("\\.\" & tdisk1.Text, GENERIC_READ Or GENERIC_WRITE, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0&, 0&)


Call diskinfo

Dim mfile As String, mfile2 As String

Dim buffer() As Byte

Dim count As Long
Dim count1 As Long
count = Tsq.Text
count1 = L4.Caption

count = count * count1

ReDim buffer(count)

' Dim buffer(1 To 512000) As Byte




Open "Image.bin" For Binary As #1
' Open "Image.bin" For Output As #1

If hDevice <> -1 Then



Call ReadFile(hDevice, buffer(1), count, hadRead, ByVal 0&) '//(****)

' Dim ii As Long
' For ii = 1 To count

Put #1, , buffer()
' Next


Close #1

Call CloseHandle(hDevice)
Lts.Caption = "数据备份完成!"

End If


End Sub

Private Sub diskinfo()
Dim a As Variant
Dim b As Long
Dim C As Variant
Dim d As Variant

'\\.\PHYSICALDRIVE0 为第一个硬盘
'\\.\c: 为C盘 '\\.\D:为D盘
'其他的盘和分区改数字就好了

OpenDisk ("\\.\" & tdisk1.Text)

Dim MediaType, Cylinders, TracksPerCylinder, SectorsPerTrack, BytesPerSector As Long
a = GetDiskGeometry()

MediaType = lpGeometry.MediaType
Cylinders = lpGeometry.Cylinders.lowpart
TracksPerCylinder = lpGeometry.TracksPerCylinder
SectorsPerTrack = lpGeometry.SectorsPerTrack
BytesPerSector = lpGeometry.BytesPerSector


L6.Caption = lpGeometry.MediaType
L1.Caption = lpGeometry.Cylinders.lowpart
L2.Caption = lpGeometry.TracksPerCylinder
L3.Caption = lpGeometry.SectorsPerTrack
L4.Caption = lpGeometry.BytesPerSector


b = lpGeometry.Cylinders.lowpart * lpGeometry.TracksPerCylinder
C = lpGeometry.SectorsPerTrack * lpGeometry.BytesPerSector
d = Format((C * b) / 1000000000, 0#)
L5.Caption = d & "GB"
CloseDisk
End Sub
张3李4 2010-07-06
  • 打赏
  • 举报
回复
Private Declare Function GetDriveTypeStr Lib "kernel32.dll" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Private Declare Function GetDriveType Lib "kernel32.dll" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
'-----------------------------------------------------------------------------------------------
'-----------------------------------------------------------------------------------------------
Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000
Const FILE_SHARE_READ = &H1
Const FILE_SHARE_WRITE = &H2
Const OPEN_EXISTING = 3

'//为了简单起见,注意下面的声明语句部分地方已经改动过
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 ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Long) As Long

Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, _
ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, lpOverlapped As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long




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 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
lb_bn 2010-07-06
  • 打赏
  • 举报
回复
帮顶一下。。。
张3李4 2010-07-06
  • 打赏
  • 举报
回复
我根据在网络上找的代码写了,只可读出100M左右的扇区数据,超过了就不可以了,就是不知道怎么读写硬盘末尾那些扇区~~!那位大哥能指点指点~~!小弟拜谢了~~!
舉杯邀明月 2010-06-26
  • 打赏
  • 举报
回复
老马,失误了吧…………

嗷嗷叫的老马 2010-06-25
  • 打赏
  • 举报
回复
count=200000 * 512

//

count=clng(200000) * clng(512)
舉杯邀明月 2010-06-25
  • 打赏
  • 举报
回复
‘读扇区’???

不知道你的 hFile 是如何得到的。
20W*512,也就是 10K 的扇区数据,数据不算多,应该没有问题吧。

就是不知道你的‘缓冲区’是否‘正确开辟’了那么大的存贮空间。
嗷嗷叫的老马 2010-06-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 chen8013 的回复:]
引用 2 楼 myjian 的回复:
count=200000 * 512

//

count=clng(200000) * clng(512)



count=200000 * 512 与 count=clng(200000) * clng(512) 有区别吗?
[/Quote]
VB默认是Integer,不做那种强制转换会溢出
舉杯邀明月 2010-06-25
  • 打赏
  • 举报
回复
1F 应该是 ‘100K 的扇区数据’。

舉杯邀明月 2010-06-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 myjian 的回复:]
count=200000 * 512

//

count=clng(200000) * clng(512)
[/Quote]


count=200000 * 512 与 count=clng(200000) * clng(512) 有区别吗?
a1875566250 2010-06-25
  • 打赏
  • 举报
回复
首先不明白LZ想要干什么

1,488

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧