检查电脑是台式机还是笔记本电脑等

时间:2013-03-11 05:59:16

标签: vb6 wmi runtime-error

我正在使用以下vb6代码来检查计算机是否是台式机,笔记本电脑等。但代码无法正常运行并出现运行时错误。我在VB.net中做了相同的代码,它工作正常。当我在vb6中做到这一点我得到错误。我知道我错过了导致错误的东西。任何人都帮我解决错误?以下是vb6中的完整代码

Option Explicit
 Private Sub Command1_Click()
  Dim oWMI As Object
  Dim oSystem As Object
  Dim SQL As String
  Dim objChassis As Object
  Dim strChassisType As Object
  Dim objWMIService As Object
  Dim colChassis As Object

  Dim strComputer As String
   strComputer = "."
  SQL = "Select * from Win32_SystemEnclosure"


  Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colChassis = objWMIService.ExecQuery(SQL)
For Each objChassis In colChassis
        For Each strChassisType In objChassis.ChassisTypes
            Select Case strChassisType
                Case 1
                    MsgBox ("Other")
                Case 2
                    MsgBox ("Unknown")
                Case 3
                    MsgBox ("Desktop")
                Case 4
                    MsgBox ("Low Profile Desktop")
                Case 5
                    MsgBox ("Pizza Box")
                Case 6
                    MsgBox ("Mini Tower")
                Case 7
                    MsgBox ("Tower")
                Case 8
                    MsgBox ("Portable")
                Case 9
                    MsgBox ("Laptop")
                Case 10
                    MsgBox ("Notebook")
                Case 11
                    MsgBox ("Handheld")
                Case 12
                    MsgBox ("Docking Station")
                Case 13
                    MsgBox ("All-in-One")
                Case 14
                    MsgBox ("Sub-Notebook")
                Case 15
                    MsgBox ("Space Saving")
                Case 16
                    MsgBox ("Lunch Box")
                Case 17
                    MsgBox ("Main System Chassis")
                Case 18
                    MsgBox ("Expansion Chassis")
                Case 19
                    MsgBox ("Sub-Chassis")
                Case 20
                    MsgBox ("Bus Expansion Chassis")
                Case 21
                    MsgBox ("Peripheral Chassis")
                Case 22
                    MsgBox ("Storage Chassis")
                Case 23
                    MsgBox ("Rack Mount Chassis")
                Case 24
                    MsgBox ("Sealed-Case PC")
                Case Else
                    MsgBox ("Unknown")
            End Select
        Next
    Next
End Sub

1 个答案:

答案 0 :(得分:1)

所以你已经使用working VBScript code,并通过将所有变量声明为Object将其移植到VB6。现在您的移植代码失败并出现错误。可能出现什么问题?

了解所涉及语言使用的数据类型是值得的。幸运的是,VBScript中的所有变量都是Variant。根据{{​​3}}:

  

VBScript只有一种名为Variant的数据类型。变体是一个   可以包含不同种类的特殊数据类型   信息,取决于它的使用方式。因为Variant是唯一的   VBScript中的数据类型,它也是所有人返回的数据类型   VBScript中的函数。

     

大多数情况下,您可以将所需的数据放在Variant中,而Variant的行为方式最适合其包含的数据。

幸运的是,VB6支持非常相同的Variant数据类型。这就是为什么将变量声明为Variant可以解决问题:

  Dim oWMI
  Dim oSystem
  Dim SQL
  Dim objChassis
  Dim strChassisType
  Dim objWMIService
  Dim colChassis
  Dim strComputer
相关问题