3

我在 Windows XP 上遇到了关于我的应用程序的问题。我在 Windows XP 上收到以下错误,而在 7/8 上它工作正常,我不知道下一步该做什么。我调查了这个问题,发现它与 WMI 有关。我尝试在 Windows XP 系统上修复 WMI,但仍然无法正常工作。

这是我收到的错误:

************** 异常文本 **************

System.Management.ManagementException:在 System.Management.PropertyDataCollection.get_Item(String propertyName) 在 System.Management.PropertyData.RefreshPropertyInfo() 在 System.Management.ManagementBaseObject.GetPropertyValue 中找不到 System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode) (String propertyName) at System.Management.ManagementBaseObject.get_Item(String propertyName)
at ?.?.(Object , EventArgs) at System.Windows.Forms.Timer.OnTick(EventArgs e) at System.Windows.Forms.Timer.TimerNativeWindow .WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

winmgmt 服务正在上述系统上运行。有谁知道下一步该怎么做?我目前正在安装 Windows XP 以查明错误,但我可以从该站点更有经验的成员那里获得一些帮助。

这是模拟错误的代码。它在 hdd.Get 循环上停止

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        TextBox1.Text = TextBox1.Text + vbNewLine + "Starting process"
        Try
            Dim HDD_Serial As String = Nothing
            TextBox1.Text = TextBox1.Text + vbNewLine + "Step 1"
            Dim hdd As New ManagementObjectSearcher("select * from Win32_DiskDrive")
            TextBox1.Text = TextBox1.Text + vbNewLine + "Step 2"
            For Each hd In hdd.Get
                TextBox1.Text = TextBox1.Text + vbNewLine + "Step 2.x"
                HDD_Serial = hd("SerialNumber")
            Next
            TextBox1.Text = TextBox1.Text + vbNewLine + "Step 3"
            HDD_Serial = HDD_Serial.Replace(" ", "")
            TextBox1.Text = TextBox1.Text + vbNewLine + "Step 4"
            Dim regkey As String = "0000-0000-0000-0000"
            TextBox1.Text = TextBox1.Text + vbNewLine + "Step 5"
            Dim sstring As String = regkey + "|" + HDD_Serial
            TextBox1.Text = TextBox1.Text + vbNewLine + "Step 6"
            sstring = AESEncrypt(sstring, "4545664456", "1251545478")
            TextBox1.Text = TextBox1.Text + vbNewLine + "Step 7"
            sstring = StrToHex(sstring)
            TextBox1.Text = TextBox1.Text + vbNewLine + "Step 8"
            Dim rk2 As RegistryKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\Test Key")
            TextBox1.Text = TextBox1.Text + vbNewLine + "Step 9"
            rk2.SetValue("regentry", sstring, RegistryValueKind.String)
            TextBox1.Text = TextBox1.Text + vbNewLine + "Step 10"
        Catch ex As Exception
            MessageBox.Show("Error")
            TextBox1.Text = TextBox1.Text + vbNewLine + vbNewLine + "-----------------------------------------------------" + vbNewLine + ex.ToString
        End Try
        TextBox1.Text = TextBox1.Text + vbNewLine + "End of log"
    End Sub

错误图片:

在此处输入图像描述

4

2 回答 2

1

如果您希望序列号的格式与从 WMI“VolumeSerialNumber”中获得的格式相同,请使用以下命令

Return Convert.ToString(serNum, 16).ToString.ToUpper 

这将为您提供十六进制字符串....

于 2014-03-18T23:16:55.470 回答
1

我设法为此找到了解决方法。我首先检测到正在运行的 Windows 版本,然后在 Windows XP 的情况下,我使用 API 调用而不是 WMI 来获取 HDD ID。如果版本高于 XP,我使用 WMI。我的应用程序使用的是 NET Framework 4.0,因此我不需要检查早期版本的 Windows。

<DllImport("kernel32.dll")> _
    Private Shared Function GetVolumeInformation(ByVal PathName As String, ByVal VolumeNameBuffer As StringBuilder, ByVal VolumeNameSize As Int32, ByRef VolumeSerialNumber As Int32, ByRef MaximumComponentLength As Int32, ByRef FileSystemFlags As Int32, ByVal FileSystemNameBuffer As StringBuilder, ByVal FileSystemNameSize As Int32) As Long
    End Function

    Friend Function GetVolumeSerial(ByVal strDriveLetter As String) As String

        Dim serNum As System.Int32 = 0
        Dim maxCompLen As System.Int32 = 0
        Dim VolLabel As StringBuilder = New StringBuilder(256)
        Dim VolFlags As Int32 = New Int32
        Dim FSName As StringBuilder = New StringBuilder(256)
        strDriveLetter += ":\"
        Dim Ret As Long = GetVolumeInformation(strDriveLetter, VolLabel, CType(VolLabel.Capacity, Int32), serNum, maxCompLen, VolFlags, FSName, CType(FSName.Capacity, Int32))
        Return Convert.ToString(serNum)
    End Function
于 2013-08-30T20:05:02.360 回答