如何获得硬盘序列号

jinzhao 2003-02-07 11:00:13
请问如何获得硬盘序列号,用于制作 共享软件.

或者如何获得永久性的本机信息.
...全文
32 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
skile 2003-02-07
  • 打赏
  • 举报
回复
在WIN98下,把在SYSTEM目录下smartvsd.vxd这个文件拷贝到SYSTEM\IOSUBSYS 然后重新启动,然后再运行

'注意:以下代码得到的是硬盘厂商固定的序列号,而不是硬盘的逻辑序列号
'以下代码支持Windows 95 OSR2, Windows 98, Windwos 98 SE, Windows ME 第一个硬盘必须为IDE接口


Option Explicit

Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
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 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 Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private Type IDERegs
bFeaturesReg As Byte
bSectorCountReg As Byte
bSectorNumberReg As Byte
bCylLowReg As Byte
bCylHighReg As Byte
bDriveHeadReg As Byte
bCommandReg As Byte
bReserved As Byte
End Type

Private Type InParams
cBufferSize As Long
irDriveRegs As IDERegs
bDriveNumber As Byte
bReserved(0 To 19) As Byte
End Type


Dim inbuff As InParams
Dim outbuff(0 To 528) As Byte
Dim SerialNumber As String


Function ChangeByteOrder(s As Variant, nLen As Long)
Dim i As Long
Dim pi As Long
pi = 0

For i = 0 To nLen / 2 - 1
c = s(pi)
s(pi) = s(pi + 1)
s(pi + 1) = c
pi = pi + 2
Next

End Function



Private Sub Form_Load()

Dim nBytes As Long
Dim nRet As Long
Dim hVxD As Long

Dim BSerialNumber(0 To 19) As Byte

inbuff.cBufferSize = 512
inbuff.bDriveNumber = 0
inbuff.irDriveRegs.bSectorCountReg = 1
inbuff.irDriveRegs.bSectorNumberReg = 1
inbuff.irDriveRegs.bCylHighReg = 0
inbuff.irDriveRegs.bCylLowReg = 0
inbuff.irDriveRegs.bDriveHeadReg = &HA0
inbuff.irDriveRegs.bCommandReg = &HEC
hVxD = CreateFile("\\.\smartvsd", 0, 0, 0, 1, 0, 0)
nRet = DeviceIoControl(hVxD, &H7C088, inbuff, Len(inbuff) - 1, outbuff(0), 528, nBytes, 0)
If nRet > 0 Then
CopyMemory BSerialNumber(0), outbuff(36), 20
SerialNumber = StrConv(BSerialNumber, vbUnicode)
SerialNumber = Trim(SerialNumber)
End If

Call CloseHandle(hVxD)
MsgBox SerialNumber

End Sub
yefm 2003-02-07
  • 打赏
  • 举报
回复
得到磁盘序列号
磁盘序列号在每次软盘或硬盘格式化后都重新生成,并且不回重复。许多程序员用此加密。其实也可以修改该函数,可以得到磁盘卷标和文件系统类型信息。

声明:
Private Declare Function GetVolumeInformation Lib "kernel32.dll" Alias
"GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer
As String, ByVal nVolumeNameSize As Integer, lpVolumeSerialNumber As Long,
lpMaximumComponentLength As Long, lpFileSystemFlags As Long, ByVal
lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As Long

代码:

Function GetSerialNumber(sRoot As String) As Long
Dim lSerialNum As Long
Dim R As Long
Dim sTemp1 As String, sTemp2 As String
strLabel = String$(255, Chr$(0))
' 磁盘卷标
strType = String$(255, Chr$(0))
' 文件系统类型 一般为 FAT
R = GetVolumeInformation(sRoot, strLabel, Len(strLabel), lSerialNum, 0, 0,
strType, Len(strType))
GetSerialNumber = lSerialNum
'在 strLabel 中为 磁盘卷标
'在 strType 中为 文件系统类型
End Function

用法:

当驱动器不存在时,函数返回 0。如果是个非根目录,也将返回 0:

lSerial = GetSerialNumber("c:\")

gang75 2003-02-07
  • 打赏
  • 举报
回复
gz

7,764

社区成员

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

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