option Explicit

Class APIHelper
    ' 定义数据库读取类实例
    Dim sqHelper

    ' ***************************************************************************
    ' 从计算机 API 中,获取计算机的序列号
    ' ***************************************************************************
    Function ReadComputerSerialNumber()
        Dim objWMIService, colItems, objItem
        Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
        Set colItems = objWMIService.ExecQuery ("Select * from Win32_BIOS")
        ' 返回计算机序列号
        For Each objItem In colItems
            ReadComputerSerialNumber = Trim(objItem.SerialNumber)
        Next
    End Function
    
    ' ***************************************************************************
    ' 从计算机 API 中,获取计算机的 MAC 地址
    ' ***************************************************************************
    Function ReadMacAddress()
        ReadMacAddress = ""
        Dim objWMIService,objNetworkAdapters,objAdapter
        Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
        Set objNetworkAdapters = objWMIService.ExecQuery("select * from Win32_NetworkAdapterConfiguration where IPEnabled = 1")
        For Each objAdapter In objNetworkAdapters
            ShowMessage objAdapter.MacAddress,0
            ' Get the MAC address
            ReadMacAddress = objAdapter.MacAddress
        Next 
    End Function
   
    ' ***************************************************************************
    ' 从计算机系统 API 中,读取计算机的磁盘数量。
    ' ***************************************************************************
    Function ReadComputerDiskPartitions()
        Dim objWMIService, wmiDiskDrives, wmiDiskDrive, objPartition
        Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
        Set wmiDiskDrives = objWMIService.ExecQuery ("Select * from Win32_DiskDrive")
   
        Dim diskPartitions, dicPartition, diskArray, diskPartitionsByQuery
        Set diskArray = CreateObject("Scripting.Dictionary")
        ' 逐个磁盘驱动器读取
        For Each wmiDiskDrive In wmiDiskDrives
            Select Case wmiDiskDrive.MediaType
                ' 如果是固态硬盘媒体
                Case "Fixed hard disk media"
                    SET diskPartitions = CreateObject("Scripting.Dictionary")
                    diskPartitions.CompareMode = TextCompare
                    ' 输出磁盘编号 0 及磁盘大小
                    diskPartitions("DiskID") = wmiDiskDrive.Index
                    diskPartitions("DiskSize") = wmiDiskDrive.Size ' 单位 MB
                    Set diskPartitions("DiskPartitions") = CreateObject("Scripting.Dictionary")
                   
                    ' 输出指定磁盘的分区列表
                    Set diskPartitionsByQuery = objWMIService.ExecQuery("Select * from Win32_DiskPartition WHERE DiskIndex = " & wmiDiskDrive.Index)
                    For each objPartition in diskPartitionsByQuery
                        SET dicPartition = CreateObject("Scripting.Dictionary")
                        dicPartition.CompareMode = TextCompare
                        dicPartition("PartitionID") = objPartition.Index
                        dicPartition("PartitionSize") = objPartition.Size
                       
                        diskPartitions("DiskPartitions").Add dicPartition("PartitionID"),dicPartition
                    Next
                   
                    diskArray.Add diskPartitions("DiskID"),diskPartitions
            End Select
        Next
       
        Set ReadComputerDiskPartitions = diskArray
    End Function
End Class