如何获得当前的硬盘类型?

时间:2012-08-08 09:14:49

标签: vb6 wmi master-slave hdd

我找到了一种在vb6中获取hdd serial no的方法。但它需要从

中选择一个选项
  • 小学大师
  • 主要奴隶
  • 辅助主人
  • 辅助奴隶

但我想自动选择一个选项。自动选择逻辑是,

  

假设我有4个以上4种硬盘。逻辑将选择加载当前系统的hdd类型。

我真的不知道如何检测当前系统是哪种硬盘类型。请帮忙。

这是我用来选择HDD序列号的类。 HDSN CLASS

2 个答案:

答案 0 :(得分:1)

以下代码应该有所帮助:

Option Explicit

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

Private Const IOCTL_VOLUME_BASE     As Long = 86 ' Asc("V")
Private Const METHOD_BUFFERED       As Long = 0
Private Const FILE_READ_ACCESS      As Long = 1
Private Const FILE_ANY_ACCESS       As Long = 0

'DEFINE IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS CTL_CODE(IOCTL_VOLUME_BASE, 0, METHOD_BUFFERED, FILE_ANY_ACCESS)
Private Const IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS = (((IOCTL_VOLUME_BASE) * (2& ^ 16&)) Or ((FILE_ANY_ACCESS) * (2& ^ 14&)) Or ((0&) * (2& ^ 2&)) Or (METHOD_BUFFERED))

Private Type DISK_EXTENT
    DiskNumber          As Long
    StartingOffset      As Currency
    ExtentLength        As Currency
End Type

Private Type VOLUME_DISK_EXTENTS
    NumberOfDiskExtents As Currency
    Extents(1 To 4)     As DISK_EXTENT
End Type

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 Declare Function CloseHandle _
    Lib "kernel32" _
    (ByVal hObject As Long) As Long

Private Declare Function GetWindowsDirectory Lib "Kernel32.dll" Alias "GetWindowsDirectoryW" ( _
    ByVal lpBuffer As Long, _
    ByVal uSize As Long _
) As Long

Private Declare Function DeviceIoControlNoInput _
    Lib "kernel32" Alias "DeviceIoControl" _
    (ByVal hDevice As Long, _
    ByVal dwIoControlCode As Long, _
    ByVal lpInBuffer As Long, _
    ByVal nInBufferSize As Long, _
    ByRef lpOutBuffer As Any, _
    ByVal nOutBufferSize As Long, _
    ByRef lpBytesReturned As Long, _
    ByVal lpOverlapped As Long) As Long

' Return the index of the physical drive from which we've booted into Windows. 
Public Function GetBootPhysicalDrive() As Long

    Dim sWindowsPath        As String
    Dim nRet                As Long
    Dim sDevicePath         As String
    Dim hLogicalBootDrive   As Long
    Dim sVolumeDevice       As String
    Dim uVolumeDiskExtents  As VOLUME_DISK_EXTENTS
    Dim nBytesReturned      As Long

    ' Allocate space and retrieve the windows directory.
    sWindowsPath = Space$(64)
    nRet = GetWindowsDirectory(StrPtr(sWindowsPath), 64)

    ' This gives us the volume that Windows is on. Open it.
    sVolumeDevice = "\\.\" & Left$(sWindowsPath, 2)
    hLogicalBootDrive = CreateFile(sVolumeDevice, GENERIC_READ + GENERIC_WRITE, FILE_SHARE_READ + FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0&, 0&)

    ' Find out information about this volume.
    nRet = DeviceIoControlNoInput(hLogicalBootDrive, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, 0&, 0&, uVolumeDiskExtents, LenB(uVolumeDiskExtents), nBytesReturned, 0&)

    If nRet = 0 Then
    ' Something went wrong. Return error value.
        GetBootPhysicalDrive = -1
    Else
    ' This is the physical disk number.
        GetBootPhysicalDrive = uVolumeDiskExtents.Extents(1).DiskNumber
    End If

    ' Close volume.
    CloseHandle hLogicalBootDrive

End Function

答案 1 :(得分:0)

使用Mark Bertenshaw的Above解决方案我在模块中编写了以下内容,并调用GetDiskSerialNumber函数来获取当前系统从其引导的HDD的序列。这是我的模块代码:

Option Explicit

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

Private Const IOCTL_VOLUME_BASE     As Long = 86 ' Asc("V")
Private Const METHOD_BUFFERED       As Long = 0
Private Const FILE_READ_ACCESS      As Long = 1
Private Const FILE_ANY_ACCESS       As Long = 0

'DEFINE IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS CTL_CODE(IOCTL_VOLUME_BASE, 0, METHOD_BUFFERED, FILE_ANY_ACCESS)
Private Const IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS = (((IOCTL_VOLUME_BASE) * (2& ^ 16&)) Or ((FILE_ANY_ACCESS) * (2& ^ 14&)) Or ((0&) * (2& ^ 2&)) Or (METHOD_BUFFERED))

Private Type DISK_EXTENT
    DiskNumber          As Long
    StartingOffset      As Currency
    ExtentLength        As Currency
End Type

Private Type VOLUME_DISK_EXTENTS
    NumberOfDiskExtents As Currency
    Extents(1 To 4)     As DISK_EXTENT
End Type

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 Declare Function CloseHandle _
    Lib "kernel32" _
    (ByVal hObject As Long) As Long

Private Declare Function GetWindowsDirectory Lib "Kernel32.dll" Alias "GetWindowsDirectoryW" ( _
    ByVal lpBuffer As Long, _
    ByVal uSize As Long _
) As Long

Private Declare Function DeviceIoControlNoInput _
    Lib "kernel32" Alias "DeviceIoControl" _
    (ByVal hDevice As Long, _
    ByVal dwIoControlCode As Long, _
    ByVal lpInBuffer As Long, _
    ByVal nInBufferSize As Long, _
    ByRef lpOutBuffer As Any, _
    ByVal nOutBufferSize As Long, _
    ByRef lpBytesReturned As Long, _
    ByVal lpOverlapped As Long) As Long

' Return the index of the physical drive from which we've booted into Windows. 
Public Function GetBootPhysicalDrive() As Long

    Dim sWindowsPath        As String
    Dim nRet                As Long
    Dim sDevicePath         As String
    Dim hLogicalBootDrive   As Long
    Dim sVolumeDevice       As String
    Dim uVolumeDiskExtents  As VOLUME_DISK_EXTENTS
    Dim nBytesReturned      As Long

    ' Allocate space and retrieve the windows directory.
    sWindowsPath = Space$(64)
    nRet = GetWindowsDirectory(StrPtr(sWindowsPath), 64)

    ' This gives us the volume that Windows is on. Open it.
    sVolumeDevice = "\\.\" & Left$(sWindowsPath, 2)
    hLogicalBootDrive = CreateFile(sVolumeDevice, GENERIC_READ + GENERIC_WRITE, FILE_SHARE_READ + FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0&, 0&)

    ' Find out information about this volume.
    nRet = DeviceIoControlNoInput(hLogicalBootDrive, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, 0&, 0&, uVolumeDiskExtents, LenB(uVolumeDiskExtents), nBytesReturned, 0&)

    If nRet = 0 Then
    ' Something went wrong. Return error value.
        GetBootPhysicalDrive = -1
    Else
    ' This is the physical disk number.
        GetBootPhysicalDrive = uVolumeDiskExtents.Extents(1).DiskNumber
    End If

    ' Close volume.
    CloseHandle hLogicalBootDrive

End Function

Public Function GetDiskSerialNumber() As String
    Dim wmiObject As Object
    Dim obj As Object

    Set wmiObject = GetObject("WinMgmts:")
    For Each obj In wmiObject.InstancesOf("Win32_PhysicalMedia")
        If obj.Tag = "\\.\PHYSICALDRIVE" + CStr(GetBootPhysicalDrive) Then GetDiskSerialNumber = obj.Tag + " : " + obj.SerialNumber
    Next obj
End Function

非常感谢Mark Bertenshaw