试一试这个,不知道有用否。 '调用API函数 GetVolumeInformation?'API声明:Private Declare Function GetVolumeInformation Lib "kernel32" _ Alias "GetVolumeInformationA" _ (ByVal lpRootPathName As String, _ ByVal lpVolumeNameBuffer As String, _ ByVal nVolumeNameSize As Long, _ lpVolumeSerialNumber As Long, _ lpMaximumComponentLength As Long, _ lpFileSystemFlags As Long, _ ByVal lpFileSystemNameBuffer As String, _ ByVal nFileSystemNameSize As Long) As Long'程序:Private Sub Form_Load() Dim sDriveLetter As String Dim RetVal As Long Dim lpName As String Dim nSize As Long Dim nSerial As Long Dim lpMaxComp As Long Dim nFileFlags As Long Dim lpFileName As String lpName = Space(255) lpFileName = Space(255) nSize = 255 sDriveLetter = "G:\" '这里你可以指定任何盘号 RetVal = GetVolumeInformation(sDriveLetter, lpName, _ nSize, nSerial, lpMaxComp, nFileFlags, lpFileName, nSize) '你可以看出,不仅能得到序列号,还可以得到其他很多信息 Debug.Print lpName Debug.Print nSerial Debug.Print lpMaxComp Debug.Print nFileFlags Debug.Print lpFileName Debug.Print nSize If RetVal = 0 Then '这里是你的错误处理代码 Exit Sub 'Quit the subroutine Else lblDriveSerial.Caption = CStr(nSerial) '假定你已经建立了这个标签,将在标签上显示序列号. End If End Sub
'调用API函数 GetVolumeInformation?'API声明:Private Declare Function GetVolumeInformation Lib "kernel32" _
Alias "GetVolumeInformationA" _
(ByVal lpRootPathName As String, _
ByVal lpVolumeNameBuffer As String, _
ByVal nVolumeNameSize As Long, _
lpVolumeSerialNumber As Long, _
lpMaximumComponentLength As Long, _
lpFileSystemFlags As Long, _
ByVal lpFileSystemNameBuffer As String, _
ByVal nFileSystemNameSize As Long) As Long'程序:Private Sub Form_Load()
Dim sDriveLetter As String
Dim RetVal As Long
Dim lpName As String
Dim nSize As Long
Dim nSerial As Long
Dim lpMaxComp As Long
Dim nFileFlags As Long
Dim lpFileName As String
lpName = Space(255)
lpFileName = Space(255)
nSize = 255 sDriveLetter = "G:\" '这里你可以指定任何盘号 RetVal = GetVolumeInformation(sDriveLetter, lpName, _
nSize, nSerial, lpMaxComp, nFileFlags, lpFileName, nSize)
'你可以看出,不仅能得到序列号,还可以得到其他很多信息
Debug.Print lpName
Debug.Print nSerial
Debug.Print lpMaxComp
Debug.Print nFileFlags
Debug.Print lpFileName
Debug.Print nSize If RetVal = 0 Then
'这里是你的错误处理代码
Exit Sub 'Quit the subroutine
Else
lblDriveSerial.Caption = CStr(nSerial)
'假定你已经建立了这个标签,将在标签上显示序列号.
End If
End Sub
要用 API. 我一般是直接用 VC 写动态库来做这种事情。首先,用 SetupDiGetClassDevs API 得到 HDEVINFO 数据再用 SetupDiEnumDeviceInterfaces API 传入 HDEVINFO 枚举所有符合要求的设备,返回一个 SP_INTERFACE_DEVICE_DATA 结构数组。然后调用 SetupDiGetInterfaceDeviceDetail API 两次,第一次得到长度,第二次得到实际数据。从得到的 PSP_INTERFACE_DEVICE_DETAIL_DATA 结构数组中提取 ->DevicePath 信息就可以了。如果得到多个设备,遍历数组即可。