2

我正在使用一种方法将所有 USB 外部插入到 PC 中。我使用在 vb6 中编写的函数并创建了一个 dll。现在我想在 c++ 程序中使用 dll,但我有一个关于 getObject() 函数的错误。我可以在用 vb6 编写的 dll 中使用 getObject() 吗?这是函数的代码

Public Function GetUsbDeviceList() As String
    Dim ComputerName As String
    Dim wmiServices As Object
    Dim wmiDiskPartitions As Object
    Dim wmiDiskPartition As Object
    Dim wmiDiskDrives As Object
    Dim wmiDiskDrive As Object
    Dim wmiLogicalDisks As Object
    Dim wmiLogicalDisk As Object
    Dim Vol As Object
    Dim strFound As String
    Dim Query As String

    ComputerName = "."
    Set wmiServices = GetObject("winmgmts:{impersonationLevel=Impersonate}!//" & ComputerName)

    strFound = ""
    ' Get physical disk drive
    Set wmiDiskDrives = wmiServices.ExecQuery("SELECT * FROM Win32_DiskDrive where InterfaceType = 'USB'")
    For Each wmiDiskDrive In wmiDiskDrives
        'MsgBox ("Disk drive Caption: " & wmiDiskDrive.Caption & vbNewLine & "DeviceID: " & " (" & wmiDiskDrive.DeviceID & ")")
        'Use the disk drive device id to find associated partition
        Query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
        Set wmiDiskPartitions = wmiServices.ExecQuery(Query)

        For Each wmiDiskPartition In wmiDiskPartitions
           'Use partition device id to find logical disk
           Set wmiLogicalDisks = wmiServices.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" & wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
            For Each wmiLogicalDisk In wmiLogicalDisks
                'MsgBox ("Drive letter associated with disk drive = " & wmiDiskDrive.Caption & wmiDiskDrive.DeviceID & vbNewLine & " Partition = " & wmiDiskPartition.DeviceID & vbNewLine & " is " & wmiLogicalDisk.DeviceID)
                 Dim DatiUnita As Scripting.FileSystemObject
                 Set DatiUnita = GetObject(, "Scripting.FileSystemObject")
                Set Vol = DatiUnita.GetDrive(DatiUnita.GetDriveName(wmiLogicalDisk.DeviceID))
                strFound = strFound + wmiLogicalDisk.DeviceID + "," + Vol.volumename + "," + wmiDiskDrive.PNPDeviceID + ";"
            Next
        Next
    Next

    GetUsbDeviceList = strFound
End Function
4

1 回答 1

2

改变

Set DatiUnita = GetObject(, "Scripting.FileSystemObject")

Set DatiUnita = CreateObject("Scripting.FileSystemObject")

(或者,您可以使用

Set DatiUnita = GetObject("", "Scripting.FileSystemObject")

即一个空字符串,不要与空字符串混淆Variant。)

于 2013-02-25T14:25:50.537 回答