-1

私はvb6でhddシリアル番号を取得する方法を見つけました。ただし、オプションを選択する必要があります

  • プライマリマスター
  • プライマリスレーブ
  • セカンダリマスター
  • 二次スレーブ

しかし、私はオプションを自動選択したいと思います。自動選択ロジックは、

上記の4種類のHDDを4台持っているとします。そして、ロジックは、現在のシステムがロードされているhddタイプを選択します。

現在のシステムがどのHDDタイプにあるかを検出する方法が本当にわかりません。助けてください。

これが私がHDDシリアル番号を選択するために使用するクラスです。HDSNクラス

4

2 に答える 2

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
于 2012-08-09T01:07:33.470 に答える
0

上記のMarkBertenshawのソリューションを使用して、モジュールに次のように記述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

MarkBertenshawに感謝します。

于 2012-08-10T20:16:57.410 に答える