|
-
Jan 7th, 2007, 03:47 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [2005] WMI L2CacheSpeed
Ok i can't figure this out when i run my app it halts at,
VB Code:
lblCPU.Text += "L2CacheSpeed: " & info("L2CacheSpeed").ToString() & CRLF
The error is, System.NullReferenceException{"Object reference not set to an instance of an object."},
Here is all my code for the app,
VB Code:
'Set were the form is showen on the desktop
Me.Location = New Point(1010, 30)
' This is to show how to use the SelectQuery object in the place of a SELECT
' statement.
Dim query As New SelectQuery("Win32_processor")
'ManagementObjectSearcher retrieves a collection of WMI objects based on
' the query.
Dim search As New ManagementObjectSearcher(query)
' Display each entry for Win32_processor
Dim info As ManagementObject
For Each info In search.Get()
lblCPU.Text = "CPU Name: " & info("Name").ToString() & CRLF
lblCPU.Text += "Caption: " & info("Caption").ToString() & CRLF
lblCPU.Text += "CurrentClockedSpeed: " & info("CurrentClockSpeed").ToString() & CRLF
lblCPU.Text += "MaxClockSpeed: " & info("MaxClockSpeed").ToString() & CRLF
lblCPU.Text += "ExtClock: " & info("ExtClock").ToString() & CRLF
lblCPU.Text += "CurrentVoltage: " & info("CurrentVoltage").ToString() & CRLF
lblCPU.Text += "L2CacheSize: " & info("L2CacheSize").ToString() & CRLF
lblCPU.Text += "L2CacheSpeed: " & info("L2CacheSpeed").ToString() & CRLF
lblCPU.Text += "Status: " & info("Status").ToString() & CRLF
Next
Im Using vb.net 2005 Express.
-
Jan 7th, 2007, 05:20 PM
#2
Re: [2005] WMI L2CacheSpeed
Are you sure your processor has that property and it is not null? You can check it by using the .GetText method to see what properties it returns.
VB Code:
For Each info As System.Management.ManagementObject In search.Get()
MessageBox.Show(info.GetText(Management.TextFormat.Mof))
Next
Check to see if the property you are trying to display is in that list. If it isn't, you will get an error when trying to pull a property name that it cannot return, ie a null value. You cannot assume all hardware is going to pull a value for any particular property...
-
Jan 7th, 2007, 06:07 PM
#3
Thread Starter
Hyperactive Member
Re: [2005] WMI L2CacheSpeed
Ok, thanks for the help. I dont have L2CacheSpeed. But i was wondering how i can still have the L2CacheSpeed in my code but have it work only when the users computer has L2CacheSpeed on there CPU. An how can i make it so if the user dosent have L2CacheSpeed on there CPU it will not make the program frag?
-
Jan 7th, 2007, 09:13 PM
#4
Re: [2005] WMI L2CacheSpeed
I would assume that info("L2CacheSpeed") would return Nothing in that case, so simply assign the result to a variable first, then test it for Nothing.
Also, you should avoid all that string concatenation as it's ugly and inefficient. You should use String.Format or a StringBuilder, which has Append, AppendLine and AppendFormat methods. Much cleaner and much more efficient.
-
Jan 8th, 2007, 01:09 AM
#5
Re: [2005] WMI L2CacheSpeed
You can also check out my WMI codebank example, which mentions a mgmtclassgen.exe program you can use to create a wrapper class for whatever WMI class you are trying to use. This wrapper class should handle null values without a problem (not erroring out when you try to access a property that is null), as well as return enumerated values for properties that have them (instead of some number that you have to look up the meaning of). The WMI zip file is a library with the class already in it (as well has hundreds of others), if you wish to reference it, however its a rather large file when uncompressed, which you might feel is a little overkill if you wish to only use one class.
-
Jan 9th, 2007, 05:52 PM
#6
Thread Starter
Hyperactive Member
Re: [2005] WMI L2CacheSpeed
Thank you gigemboy and jmcilhinney for your post, ill look into the String.Format and StringBuilder.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|