Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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:
VB
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:
VB
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:
VB
'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.
Posted

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, ...
 
Share this answer
 
Comments
Kschuler 30-Jul-12 14:39pm    
I only have about 400 users and have not had issues with two machines having the same "unique" code. I'm not worried about uniqueness at this time. These machines used to work with the code I have, but now a group of them are encountering these errors and are, as a result, returning different "unique" values. My question is what kinds of things can cause errors with accessing the WMI. The computers that are having the trouble belong to a company that is known for very tight security, so I'm guessing they must have changed some permissions and/or settings to an anti-virus or something that has stopped my code from working. Also, because of their tight security it isn't easy to get a utility through. They block all .exe (even if zipped or changed the file extension) in their email. That's why I was trying to figure this out solo before resorting to that. Do you have any other suggestions?
Dave Kreskowiak 30-Jul-12 15:25pm    
Nope. You have to know what your code is thinking/seeing on their equipment. If you don't know that, you're simply guessing at what the problem is. You may as well be firng a arrows at a target with your eyes closed.

There is no virus scanning software that will be changing the values your code is looking for in WMI. It MAY be possible that your customer has changed security on the WMI objects you're looking at, but I highly doubt it.

More likely is that you're not getting any values back from those object at all. That's why I say you have to be looking at more than just those 3 values.

Another possibility is WMI is screwed on those machines. If they are Windows XP machines, that would be the first thing I look at. Yes, it's very possible. After working with SCCM client issues for the last year, WMI gets corrupts on XP machines FAR more often than it does on Win7.
Kschuler 30-Jul-12 15:33pm    
I hashed an empty string the same way I hash those values and confirmed that you are correct. I am NOT getting any values for those in this particular case. But my point is that I used to get values for them on these exact same machines. I am confident that the reason I'm not getting values right now is because of these errors. I will look into how to fixed a machine with a corrupt WMI and see what I find with that.
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.
 
Share this answer
 
Comments
Kschuler 5-Sep-12 14:17pm    
It turned out that there was quite a bit messed up on the user's particular machine. We finally got their IT people to take a look and they had to repair more than just the WMI. Thanks for your help with the issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900