NEWBIE: WMI within VB.NET Error Trapping

G

Guest

Hello,

I am porting a legacy program I created in Macromedia Director over to
VB.net and have been learning quite a lot over the last week and half of
porting the application.

One of the things that the Director application did was retrieve a bunch of
system level information (total hard drives, ram, mac address, etc..) I've
found that tapping into WMI through VB.net really helps locate a lot of this
information. One problem I've noticed is that sometimes the code I've pieced
together fails with the following error when trying to derive information
from WMI:

Object reference not set to an instance of an object

My gut feeling is telling me that this information is not available from
within WMI, even though it has an entry on the MSDN website as something
that can be retrieved from within the WMI framework.

The following code fails with the 'Object reference not set to an instance
of an object' on my Windows 2000 machine with the latest patches, .NET
framework, etc...

=============================================================
Imports System.Management

Public Class Form1
Inherits System.Windows.Forms.Form


Public Sub getDNSDomain()
Dim mc As New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim moc As ManagementObjectCollection = mc.GetInstances()
Dim mo As ManagementObject

For Each mo In moc
If mo("Caption").ToString() <> "" Then
TextBox1.Text = mo("DNSDomain").ToString()
End If
Next
End Sub

mo.Dispose()
mo = Nothing
moc.Dispose()
moc = Nothing
mc.Dispose()
mc = Nothing

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
getDNSDomain()
End Sub

End Class

============================================================================
====

I run the application in Debug mode and the error dialog appears at this
line:

TextBox1.Text = mo("DNSDomain").ToString()

I have a feeling that the mo object has not been populated with information
and thus the error is displayed.

Is there a way within VB.net to tell if an object is actually an object? In
Director I would use ObjectP to test to see if the object is actually an
object.

I guess what I would like to do is make sure that I can trap for errors
whenever I request a piece of information from WMI and it's not available on
the system instead of having the error dialog box appear each time.

Also, is there a more non-neophyte way to march through WMI information
instead of writing 100 functions to grab all the information I am trying to
retrieve?

Any suggestions would be greatly appreciated.

Cheers,

devin
 
K

Ken Tucker [MVP]

Hi,

Enclose TextBox1.Text = mo("DNSDomain").ToString() in a try catch block
to prevent the error from coming up.
If mo("Caption").ToString() <> "" Then

Try

TextBox1.Text = mo("DNSDomain").ToString()

Catch

End Try

End If

Ken

------------------------------------
 
G

Guest

Howdy Ken,

Thank you the help. That worked perfect!

One more question. Is it possible to grab the WMI string attribute name
instead of always having to hard code it?

So instead of doing this:

If mo("Caption").ToString() <> "" Then
Try
TextBox1.Text = mo("DNSDomain").ToString()
Catch
End Try
End If

Is it possible to grab the WMI string attribute kind of like this:

Dim myAttributeName As String

For Each mo In moc
myAttributeName = mo.name.ToString()
If mo(myAttributeName) <> "" Then
Try
TextBox1.Text = mo(myAttributeName).ToString()
Catch
End Try
End If
Next

I'm not sure of the exact syntax, which I am sure is wrong in my example but
I think it clearly shows I am trying to grab the name of WMI attribute and
then grab it's value if it exists.

Any suggestions?

Best Regards,

devin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top