请教一个简单的问题:如何在VB中读写硬盘的任意物理扇区?

jessezappy 2002-07-15 10:30:19
请教一个简单的问题:如何在VB中读写硬盘的任意物理扇区?
用任何方法都可以(API,DLL,OCX等)!只要能配合VB程序,但是不能用交换文件。
...全文
164 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
超级大笨狼 2003-04-24
  • 打赏
  • 举报
回复
VB6编程中如何获取硬盘分区信息

  也许你并不了解硬盘分区信息应该包括些什么,但如果你曾经对硬盘分过区,你或许对此有所了解,在此为各位介绍一个用VB编写的获取硬盘分区信息的程序。在这个程序中,它将详细地告诉你:你的硬盘总容量、分过几个区、每个区的总容量、及现在剩余的可用容量、硬盘分区表为几位(即是FAT32还是FAT16),每个分区是几个字节……怎么样?够完整详细了吧!好的,就让我们一起来看一下吧:
  首先做准备工作:在FORM1上新建二个LABEL(LABEL1和LABEL2)一个COMMAND1命令按钮。然后输入以下代码:
  Private Declare Function GetDriveType Lib
  kernel32“Alias "GetDriveTypeA(ByVal nDrive As String) As Long
  Private Declare Function GetDiskFreeSpace Lib“kernel32" Alias“GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTtoalNumberOfClusters As Long) As Long
  Private Const DRIVE_FIXED = 3
  Private Sub Form_Load() '作初始化设置
  COMMAND1.Caption = “测试硬盘"
  Form1.Caption = “测试硬盘程序"
  Label1.WordWrap = True
  Label1.Caption = “"
  Label2.WordWrap = True
  Label2.Caption = “"
  End Sub
  Private Sub COMMAND1_Click()
  Dim DriveNum As Integer
  Dim TempDrive As String
  Dim X As Long
  For DriveNum = 97 To 122 Step 1 '检测从A-Z(盘符)
  TempDrive = GetDriveType(Chr(DriveNum) & “:\")
  Select Case TempDrive '如是3则表示是硬盘,测试你有几个盘
  Case 3: X = GetDiskSpace(Chr(DriveNum)) '调用子程序
  End Select
  Next DriveNum
  End Sub
  Public Function GetDiskSpace(DrivePath As String)
  Dim Drive As String
  Dim SectorsPerCluster As Long
  Dim BytesPerSector As Long
  Dim NumberOfFreeClusters As Long
  Dim TotalClusters As Long
  Dim Check As Integer
  Dim DiskSpace
  Dim diskTotal
  Static AllDiskTotal As Long
  Static NUM As Integer
  NUM = NUM + 1 '分几个区的计算
  Drive = Left(Trim(DrivePath), 1) & “:\"
  Check = GetDiskFreeSpace(Drive, SectorsPerCluster, BytesPerSector, NumberOfFreeClusters, TotalClusters)
  If Check <> 0 Then
  DiskSpace = SectorsPerCluster * BytesPerSector * NumberOfFreeClusters
  '这是一个分区磁盘剩余空间的计算公式
  DiskSpace = Format$(DiskSpace, “###,###") '以规定格式显示,如732,324,231
  diskTotal = SectorsPerCluster * BytesPerSector * TotalClusters
  '这是一个分区磁盘总容量的计算公式
  diskTotal = Format$(diskTotal, “###,###")
  AllDiskTotal = AllDiskTotal + diskTotal '整个硬盘的总容量
  Label1.Caption =“你的硬盘总容量为:” & Format$(AllDiskTotal,“###,###") &个字节,即:” & Left(AllDiskTotal, 1) & . & Mid(AllDiskTotal, 2, 1) &“G,一共分了”& NUM &“个区,其中:"
   Label2.Caption = Label2.Caption & UCase(DrivePath) & “盘的整个容量为:" & diskTotal &“个字节" & ",其剩余磁盘空间为:“& DiskSpace & " 个字节,磁盘已FAT“& SectorsPerCluster & ",每个分区为:“& BytesPerSector & "个字节。“& vbCrLf & vbCrLf”
  End If
  End Function
  OK!现在你运行一下,你是否满意它?
  注:以上程序在中文WINDOWS98,中文VB6.0企业版中调试通过
chenyu5188 2003-04-24
  • 打赏
  • 举报
回复
接上面:
Sub Main()

Dim verinfo As OSVERSIONINFO
Dim Ret As Long
verinfo.dwOSVersionInfoSize = Len(verinfo)
Ret = GetVersionEx(verinfo)
Dim OutStr As String
Select Case verinfo.dwPlatformId
Case VER_PLATFORM_WIN32S
MsgBox "Win32s is not supported by this programm."
End
Case VER_PLATFORM_WIN32_WINDOWS
OutStr = hdid9x
MsgBox OutStr
End
Case VER_PLATFORM_WIN32_NT
OutStr = hdidnt
MsgBox OutStr
End
End Select

End Sub


Private Function DetectIDE(bIDEDeviceMap As Byte) As String
If (bIDEDeviceMap And 1) Then
If (bIDEDeviceMap And 16) Then
DetectIDE = DetectIDE & "ATAPI device is attached to primary controller, drive 0."
Else
DetectIDE = DetectIDE & "IDE device is attached to primary controller, drive 0."
End If
End If
If (bIDEDeviceMap And 2) Then
If (bIDEDeviceMap And 32) Then
DetectIDE = DetectIDE & "ATAPI device is attached to primary controller, drive 1."
Else
DetectIDE = DetectIDE & "IDE device is attached to primary controller, drive 1."
End If
End If
If (bIDEDeviceMap And 4) Then
If (bIDEDeviceMap And 64) Then
DetectIDE = DetectIDE & "ATAPI device is attached to secondary controller, drive 0."
Else
DetectIDE = DetectIDE & "IDE device is attached to secondary controller, drive 0."
End If
End If
If (bIDEDeviceMap And 8) Then
If (bIDEDeviceMap And 128) Then
DetectIDE = DetectIDE & "ATAPI device is attached to secondary controller, drive 1."
Else
DetectIDE = DetectIDE & "IDE device is attached to secondary controller, drive 1."
End If
End If
End Function


Private Function ByteArrToString(inByte() As Byte, ByVal strlen As Integer) As String
Dim i As Integer
For i = 0 To strlen
If inByte(i) = 0 Then
Exit For
End If
ByteArrToString = ByteArrToString & Chr(inByte(i))
Next i
End Function

Private Function ByteArrToLong(inByte() As Byte) As Double
Dim i As Integer
For i = 0 To 3
ByteArrToLong = ByteArrToLong + CDbl(inByte(i)) * (256 ^ i)
Next i

End Function
chenyu5188 2003-04-24
  • 打赏
  • 举报
回复
接上面:
Private Function hdid9x() As String

'We start in 95/98/Me
h = CreateFile("\\.\Smartvsd", 0, 0, 0, CREATE_NEW, 0, 0)
If h = 0 Then
hdid9x = "open smartvsd.vxd failed"
Exit Function
End If

Dim olp As OVERLAPPED
Dim lRet As Long
lRet = DeviceIoControl(h, DFP_GET_VERSION, ByVal 0&, 0, vers, Len(vers), ByVal i, olp)
If lRet = 0 Then
hdid9x = "DeviceIoControl failed:DFP_GET_VERSION"
CloseHandle (h)
Exit Function
End If

'If IDE identify command not supported, fails
If (vers.fCapabilities And 1) <> 1 Then
hdid9x = "Error: IDE identify command not supported."
CloseHandle (h)
Exit Function
End If

'Display IDE drive number detected
Dim sPreOutStr As String
sPreOutStr = DetectIDE(vers.bIDEDeviceMap)
hdid9x = sPreOutStr

'Identify the IDE drives
For j = 0 To 3
Dim phdinfo As TIDSECTOR
Dim s(40) As Byte

If (j And 1) = 1 Then
in_data.irDriveRegs.bDriveHeadReg = &HB0
Else
in_data.irDriveRegs.bDriveHeadReg = &HA0
End If
If (vers.fCapabilities And (16 \ (2 ^ j))) = (16 \ (2 ^ j)) Then
'We don't detect a ATAPI device.
hdid9x = "Drive " & CStr(j + 1) & " is a ATAPI device, we don't detect it"
Else
in_data.irDriveRegs.bCommandReg = &HEC
in_data.bDriveNumber = j
in_data.irDriveRegs.bSectorCountReg = 1
in_data.irDriveRegs.bSectorNumberReg = 1
in_data.cBufferSize = 512

lRet = DeviceIoControl(h, DFP_RECEIVE_DRIVE_DATA, in_data, Len(in_data), out_data, Len(out_data), ByVal i, olp)

If lRet = 0 Then
hdid9x = "DeviceIoControl failed:DFP_RECEIVE_DRIVE_DATA"
CloseHandle (h)
Exit Function
End If

Dim StrOut As String

CopyMemory phdinfo, out_data.bBuffer(0), Len(phdinfo)

CopyMemory s(0), phdinfo.sModelNumber(0), 40
s(40) = 0
ChangeByteOrder s, 40

StrOut = ByteArrToString(s, 40)

hdid9x = hdid9x & vbCrLf & "Module Number:" & StrOut
CopyMemory s(0), phdinfo.sFirmwareRev(0), 8
s(8) = 0
ChangeByteOrder s, 8

StrOut = ByteArrToString(s, 8)

hdid9x = hdid9x & vbCrLf & "Firmware rev:" & StrOut
CopyMemory s(0), phdinfo.sSerialNumber(0), 20
s(20) = 0
ChangeByteOrder s, 20

StrOut = ByteArrToString(s, 20)

hdid9x = hdid9x & vbCrLf & "Serial Number:" & StrOut

CopyMemory s(0), phdinfo.ulTotalAddressableSectors(0), 4

s(5) = 0
Dim dblStrOut As Double
dblStrOut = ByteArrToLong(s)
hdid9x = hdid9x & vbCrLf & "Capacity:" & dblStrOut / 2 / 1024 & "M"
End If
Next j

'Close handle before quit
CloseHandle (h)
CopyRight

End Function

Private Function hdidnt() As String
Dim hd As String * 80
Dim phdinfo As TIDSECTOR
Dim s(40) As Byte
Dim StrOut As String

hdidnt = ""
'We start in NT/Win2000

For j = 0 To 3 '这里取四个硬盘的信息,正常PC不超过四个硬盘
hd = "\\.\PhysicalDrive" & CStr(j)
hdidnt = hdidnt & vbCrLf & hd
h = CreateFile(hd, GENERIC_READ Or GENERIC_WRITE, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)

Dim olpv As OVERLAPPED

Dim lRet As Long
lRet = DeviceIoControl(h, DFP_GET_VERSION, ByVal 0&, 0, vers, Len(vers), ByVal i, olpv)

If lRet = 0 Then
CloseHandle (h)
Else
'If IDE identify command not supported, fails
If (vers.fCapabilities And 1) <> 1 Then
hdidnt = "Error: IDE identify command not supported."
CloseHandle (h)
Exit Function
End If
'Identify the IDE drives
If (j And 1) = 1 Then
in_data.irDriveRegs.bDriveHeadReg = &HB0
Else
in_data.irDriveRegs.bDriveHeadReg = &HA0
End If
If (vers.fCapabilities And (16 \ (2 ^ j))) <> 0 Then
'We don't detect a ATAPI device.
hdidnt = hdidnt & vbCrLf & "Drive " & CStr(j + 1) & " is a ATAPI device, we don't detect it"
Else

in_data.irDriveRegs.bCommandReg = &HEC
in_data.bDriveNumber = j
in_data.irDriveRegs.bSectorCountReg = 1
in_data.irDriveRegs.bSectorNumberReg = 1
in_data.cBufferSize = 512

Dim olpr As OVERLAPPED

lRet = DeviceIoControl(h, DFP_RECEIVE_DRIVE_DATA, in_data, Len(in_data), out_data, Len(out_data), ByVal i, olpr)
If lRet <= 0 Then
hdidnt = hdidnt & vbCrLf & "DeviceIoControl failed:DFP_RECEIVE_DRIVE_DATA"
CloseHandle (h)

Else

CopyMemory phdinfo, out_data.bBuffer(0), Len(phdinfo)

CopyMemory s(0), phdinfo.sModelNumber(0), 40
s(40) = 0
ChangeByteOrder s, 40

StrOut = ByteArrToString(s, 40)

hdidnt = hdidnt & vbCrLf & "Module Number:" & StrOut
CopyMemory s(0), phdinfo.sFirmwareRev(0), 8
s(8) = 0
ChangeByteOrder s, 8

StrOut = ByteArrToString(s, 8)

hdidnt = hdidnt & vbCrLf & "Firmware rev:" & StrOut
CopyMemory s(0), phdinfo.sSerialNumber(0), 20
s(20) = 0
ChangeByteOrder s, 20

StrOut = ByteArrToString(s, 20)

hdidnt = hdidnt & vbCrLf & "Serial Number:" & StrOut

CopyMemory s(0), phdinfo.ulTotalAddressableSectors(0), 4
s(5) = 0
Dim dblStrOut As Double
dblStrOut = ByteArrToLong(s)

hdidnt = hdidnt & vbCrLf & "Capacity:" & dblStrOut / 2 / 1024 & "M"
CloseHandle (h)
End If
End If
End If
Next j
CopyRight

End Function

chenyu5188 2003-04-24
  • 打赏
  • 举报
回复
'*************************************************************************
'通过MS的S.M.A.R.T.接口,'我们可以直接从RING3调用API DeviceIoControl()来获取硬盘信息.
'*************************************************************************

Option Explicit
Option Base 0

Private Const DFP_GET_VERSION = &H74080
Private Const DFP_SEND_DRIVE_COMMAND = &H7C084
Private Const DFP_RECEIVE_DRIVE_DATA = &H7C088

'#pragma pack(1)
Private Type TGETVERSIONOUTPARAMS '{
bVersion As Byte 'Binary driver version.
bRevision As Byte 'Binary driver revision.
bReserved As Byte 'Not used.
bIDEDeviceMap As Byte 'Bit map of IDE devices.
fCapabilities As Long 'Bit mask of driver capabilities.
dwReserved(4) As Long 'For future use.
End Type

Private Type TIDEREGS
bFeaturesReg As Byte 'Used for specifying SMART "commands".
bSectorCountReg As Byte 'IDE sector count register
bSectorNumberReg As Byte 'IDE sector number register
bCylLowReg As Byte 'IDE low order cylinder value
bCylHighReg As Byte 'IDE high order cylinder value
bDriveHeadReg As Byte 'IDE drive/head register
bCommandReg As Byte 'Actual IDE command.
bReserved As Byte 'reserved for future use. Must be zero.
End Type

Private Type TSENDCMDINPARAMS
cBufferSize As Long 'Buffer size in bytes
irDriveRegs As TIDEREGS 'Structure with drive register values.
bDriveNumber As Byte 'Physical drive number to send 'command to (0,1,2,3).
bReserved(2) As Byte 'Reserved for future expansion.
dwReserved(3) As Long 'For future use.
''BYTE bBuffer(1) 'Input buffer.
End Type

Private Type TDRIVERSTATUS
bDriverError As Byte 'Error code from driver, 'or 0 if no error.
bIDEStatus As Byte 'Contents of IDE Error register.
'Only valid when bDriverError 'is SMART_IDE_ERROR.
bReserved(1) As Byte 'Reserved for future expansion.
dwReserved(1) As Long 'Reserved for future expansion.
End Type

Private Type TSENDCMDOUTPARAMS
cBufferSize As Long 'Size of bBuffer in bytes
DRIVERSTATUS As TDRIVERSTATUS 'Driver status structure.
bBuffer(511) As Byte 'Buffer of arbitrary length
'in which to store the data read from the drive.
End Type

Private Type TIDSECTOR
wGenConfig As Integer
wNumCyls As Integer
wReserved As Integer
wNumHeads As Integer
wBytesPerTrack As Integer
wBytesPerSector As Integer
wSectorsPerTrack As Integer
wVendorUnique(2) As Integer
sSerialNumber(19) As Byte
wBufferType As Integer
wBufferSize As Integer
wECCSize As Integer
sFirmwareRev(7) As Byte
sModelNumber(39) As Byte
wMoreVendorUnique As Integer
wDoubleWordIO As Integer
wCapabilities As Integer
wReserved1 As Integer
wPIOTiming As Integer
wDMATiming As Integer
wBS As Integer
wNumCurrentCyls As Integer
wNumCurrentHeads As Integer
wNumCurrentSectorsPerTrack As Integer
ulCurrentSectorCapacity(3) As Byte
wMultSectorStuff As Integer
ulTotalAddressableSectors(3) As Byte
wSingleWordDMA As Integer
wMultiWordDMA As Integer
bReserved(127) As Byte
End Type

'/*+++
'Global vars
'---*/
Private vers As TGETVERSIONOUTPARAMS
Private in_data As TSENDCMDINPARAMS
Private out_data As TSENDCMDOUTPARAMS
Private h As Long
Private i As Long
Private j As Byte

Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type

Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _
(LpVersionInformation As OSVERSIONINFO) As Long

Private Const VER_PLATFORM_WIN32S = 0
Private Const VER_PLATFORM_WIN32_WINDOWS = 1
Private Const VER_PLATFORM_WIN32_NT = 2

Private Declare Function CreateFile Lib "kernel32" _
Alias "CreateFileA" (ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, _
ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) _
As Long

Private Const CREATE_NEW = 1
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_SHARE_READ = &H1
Private Const FILE_SHARE_WRITE = &H2

Private Type OVERLAPPED
Internal As Long
InternalHigh As Long
offset As Long
OffsetHigh As Long
hEvent As Long
End Type

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, lpOverlapped As OVERLAPPED) As Long

Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)


Private Sub CopyRight()

Dim StrMsg As String
StrMsg = StrMsg & "直接从RING3调用API DeviceIoControl()来获取硬盘信息的VB程序 "
StrMsg = StrMsg & vbCrLf & "***********************************************************"
StrMsg = StrMsg & vbCrLf & "HDD identifier v1.0 for WIN95/98/Me/NT/2000. written by Lu Lin"
StrMsg = StrMsg & vbCrLf & "For more information, please visit Inside StrMsg = StrMsg & vbCrLf
StrMsg = StrMsg & vbCrLf & "***********************************************************"
MsgBox StrMsg
End Sub

Sub ChangeByteOrder(szString() As Byte, uscStrSize As Long)
Dim i As Long
Dim temp As String
For i = 0 To uscStrSize - 1 Step 2
temp = szString(i)
szString(i) = szString(i + 1)
szString(i + 1) = temp
Next i
End Sub

用户 昵称 2003-04-24
  • 打赏
  • 举报
回复
CreateFile( "\\.\C:", ... );
jessezappy 2003-04-24
  • 打赏
  • 举报
回复
高手们帮帮忙吧。。。。这个做加密很好用的。。。。大家想想办法吧。。!
jessezappy 2003-03-31
  • 打赏
  • 举报
回复
有没有VB能解决的方法。。?
crystal_heart 2002-10-02
  • 打赏
  • 举报
回复
就是代码写起来比较麻烦,我曾经把那段C++代码改成VB代码,需要找很多资料。
crystal_heart 2002-10-02
  • 打赏
  • 举报
回复
这个问题。。。。。。2000下面比较好办,把硬盘设备按流方式打开就可以操纵了。
jessezappy 2002-10-01
  • 打赏
  • 举报
回复
没有人知道?
jessezappy 2002-07-15
  • 打赏
  • 举报
回复
用汇编倒是很简单,但是可能要用到交换文件,我是用来做加密验证,用交换文件的话就不安全了。。。。
或者其他难以发现的加密方法告诉我也可以。。。
jessezappy 2002-07-15
  • 打赏
  • 举报
回复
应该有API可用,不过我的资料里面暂时查不到,最好用API..

7,785

社区成员

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

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