使用WMI ManagementObjectSearcher时出错 [英] Error when using WMI ManagementObjectSearcher

查看:57
本文介绍了使用WMI ManagementObjectSearcher时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,该应用程序使用WMI拉取值以生成唯一"(我知道这不是真正的唯一可能性),用于为使用该应用程序的每台计算机标识编号,用于注册/许可.我的一些客户现在在此代码中遇到错误,并被要求再次进行注册.我无法在机器上重现该错误,因此我的大部分结论大部分都是猜测,但我认为问题出在以下这段代码上:

 私有 功能 GetManagementObject( ByVal  strProperty  As  字符串 ByVal  strCollection  As  字符串) As  字符串
     Dim  sb  As   New  System.Text. StringBuilder
    尝试
        ' 使用ManagementObjectSearcher对象从集合中选择属性
         Dim  objSearcher  As  新建 ManagementObjectSearcher(" & strProperty& "  FROM"& strCollection)
         Dim  objCollection  As  ManagementObjectCollection = objSearcher.获取
        对于 每个 objM  As  ManagementObject  objCollection中
            对于 每个 objP  As  PropertyData  objM.Properties中
                如果 objP.Value  IsNot  DBNull.Value 然后
                    sb.Append(objP.Value)' 如果有多个集合或属性,则将它们合并在一起
                结束 如果
            下一步
        下一步
        ' 清理对象
        objSearcher.Dispose()
        objCollection.Dispose()
    捕获,例如 As 异常
        MsgBox(例如消息)
    结束 尝试

    返回 sb.ToString
结束 功能



我用三种不同的参数集来调用此方法:

 GetManagementObject("   Win32_BaseBoard")
GetManagementObject(" "  Win32_BIOS")
GetManagementObject(" "  Win32_ComputerSystemProduct")


我将返回值合并在一起,然后对其进行哈希处理以创建我的唯一"数字.现在的问题是某些用户遇到此错误:Initialization failure.他们得到了3次,所以我确信原因是这种方法,因为在收到错误时会在代码中调用3次.那么,有人知道在使用此代码之前需要检查的任何权限问题吗?还是解决该错误的方法?甚至只是为什么有些人得到它而其他人却没有得到它?
我在寻求帮助时发现了很多东西,其中包括尝试远程访问机器.我不是要远程连接,该程序只想从这台计算机上获取信息.而且我怀疑还有另一个问题(或相同问题,但症状不同),因为在我应用程序的另一个位置,我有以下代码:

 '  sbException是StringBuilder对象
sbException.Append(" & My.Computer.Info.OSFullName.ToUpper& vbNewLine)


而且我相信这条线也会引起错误.我不能肯定地说那是确切的行,而且我也不知道错误消息.这段代码位于MyApplicationEvents UnhandledException中,在那里我有处理未处理异常的代码,并且如果该代码遇到异常,我也没有放入代码来提供详细的错误消息,我只是放置了一个通用消息框,因为我认为它永远不会发生(伙计们,请从我的错误中学习).会不会同时存在某些类型的权限/授权问题?任何建议将不胜感激.

解决方案

您可能希望向他们发送一个小实用程序,以转储在这些WMI属性中找到的值.您的问题是,没有什么说明这些值必须由制造商填充,并且在更换主板后,替换主板的技术人员可能没有重新填充这些值.

坦白说,仅查看3个可能存在或可能不存在的值不足以唯一地标识一台机器.我可能会寻找大约10个不同的值,包括硬盘驱动器序列号,CDROM/DVD序列号,硬盘驱动器大小,内存大小,CPU序列号(通常不可用!),管理员帐户SID,...

我们还不时遇到WMI问题.当您用Google搜索"WMI修复"时,您会发现很多结果,这似乎很常见.
组策略也可以用于更改WMI访问权限.


I have an app that uses WMI to pull values to generate a "unique" (I know there is a slight chance it''s not really unique) identifying number for each computer that uses the app, for registration/licensing purposes. Some of my customers are now experiencing errors in this code and are being asked to go through registration again. I can''t reproduce the error on my machine, so most of my conclusions are mostly guess work, but I think the issue is with this bit of code:

Private Function GetManagementObject(ByVal strProperty As String, ByVal strCollection As String) As String
    Dim sb As New System.Text.StringBuilder
    Try
        'Use a ManagementObjectSearcher object to select a property from a collection
        Dim objSearcher As New ManagementObjectSearcher("SELECT " & strProperty & " FROM " & strCollection)
        Dim objCollection As ManagementObjectCollection = objSearcher.Get
        For Each objM As ManagementObject In objCollection
            For Each objP As PropertyData In objM.Properties
                If objP.Value IsNot DBNull.Value Then
                    sb.Append(objP.Value) 'Concats together if there are multiple collections or properties
                End If
            Next
        Next
        'Clean up objects
        objSearcher.Dispose()
        objCollection.Dispose()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

    Return sb.ToString
End Function



I call this method with three different parm sets:

GetManagementObject("SerialNumber", "Win32_BaseBoard")
GetManagementObject("SerialNumber", "Win32_BIOS")
GetManagementObject("UUID", "Win32_ComputerSystemProduct")


I concat the return values together and then hash it to create my "unique" number. The issue now is that some users are getting this error: Initialization failure. They get it three times, so I''m confident that the cause is this method because it is called three times in the code that fires when they get the error. So does anyone know of any authorities and/or permissions issues that I need to check before using this code? Or a way around this error? Or even just why some people get it and others don''t?

A lot of things I''ve found while searching for help involve trying to access the machine remotely. I''m not trying to connect remotely, the program just wants info from THIS computer. And I''m suspicious that there is another problem (or same problem but different symptom) because at another location in my app I have this bit of code:

'sbException is a StringBuilder object
sbException.Append("Operating System: " & My.Computer.Info.OSFullName.ToUpper & vbNewLine)


And I believe that this line is also causing an error. I can''t say for certain it''s that exact line, and I don''t know the error message. This code is located in MyApplicationEvents UnhandledException where I have code to take care of unhandled exceptions, and I didn''t put code in to give a detailed error message if THAT code encounters an exception, I just put a generic message box because I thought it would never happen (Learn from my mistakes, folks). Could there be some kind of permissions/authority issue that would cause BOTH of these issues? Any suggestions would be appreciated.

解决方案

You might want to send them a little utility to dump the values found in those WMI properties. Your problem is that there is NOTHING that says those values must be populated by the manufacturer and after a motherboard replacement the values may not have been repopulated by the tech who replaced the motherboard.

Frankly, just looking at 3 values that may or may not be there is insufficient to uniquely identify a machine. I''d probably look for about 10 different values including hard drive serial numbers, CDROM/DVD serial numbers, hard drive sizes, memory size, CPU serial number (normally NOT available!), administrator account SID, ...


Also we experience problems with WMI from time to time. When you google for "WMI repair", you''ll find lots of results, seems to be quite common.
Also group policies may be used to change permissions on WMI access.


这篇关于使用WMI ManagementObjectSearcher时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆