在dll中使用带有vb6.0的GetObject()

时间:2013-02-25 13:14:12

标签: dll vb6

我正在使用一种方法将所有usb extern插入到PC中。我在vb6中使用函数write并创建了一个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

1 个答案:

答案 0 :(得分:2)

更改

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

Set DatiUnita = CreateObject("Scripting.FileSystemObject")

(或者,您可以使用

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

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