[VB2005] UInt32 reading/setting value problem

Nazgulled

Centurion
Joined
Jun 1, 2004
Messages
119
I'm trying to read an UInt32 value from WMI (Win32_NetworkAdapterConfiguration) but I'm getting some exception...

Here's the VB code:
Code:
Imports System.Management

Public Class Form1

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim mc As New ManagementClass("Win32_NetworkAdapterConfiguration")
		Dim moc As ManagementObjectCollection = mc.GetInstances

		For Each mo As ManagementObject In moc
			If mo("Caption").Equals("[00000010] Marvell Yukon 88E8036 PCI-E Fast Ethernet Controller") Then
				TextBox1.Text = mo("InterfaceIndex")
			End If
		Next
	End Sub
End Class

Here's the exception:
System.Management.ManagementException was unhandled
Message="Not found "
Source="System.Management"
StackTrace:
at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
at System.Management.PropertyData.RefreshPropertyInfo()
at System.Management.PropertyDataCollection.get_Item(String propertyName)
at System.Management.ManagementBaseObject.GetPropertyValue(String propertyName)
at System.Management.ManagementBaseObject.get_Item(String propertyName)
at WindowsApplication1.Form1.Form1_Load(Object sender, EventArgs e) in C:\Documents and Settings\Nazgulled\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb:line 11
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

The exception happens at this line: TextBox1.Text = mo("InterfaceIndex").
It also happens if I try something like:

Code:
Dim val As UInt32
val = mo("InterfaceIndex")

Replacing TextBox1.Text = mo("InterfaceIndex") with val = mo("InterfaceIndex").

How can I access this? I need to save the interface index to a variable so I can compare network interfaces between Win32_NetworkAdapterConfiguration and Win32_NetworkAdapter.

Here's some interesting links:
MSDN: Win32_NetworkAdapterConfiguration
MSDN: Win32_NetworkAdapter
 
Last edited:
Nevermind... after a lot of search and a bit of luck I found something about association between those 2 WMI classes and came up with something like this:

Code:
Dim mos As ManagementObjectSearcher

        mos = New ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

        For Each moConfig As ManagementObject In mos.Get
            mos = New ManagementObjectSearcher("ASSOCIATORS OF " + "{Win32_NetworkAdapterConfiguration.Index='" + _
            moConfig("Index").ToString + "'}" + " WHERE AssocClass=Win32_NetworkAdapterSetting")

            For Each moAdapter As ManagementObject In mos.Get()
                If moAdapter("Caption").Equals("[00000010] Marvell Yukon 88E8036 PCI-E Fast Ethernet Controller") Then
                    TextBox1.Text = moAdapter("NetConnectionID")
                    TextBox1.Text = TextBox1.Text + ControlChars.CrLf + moConfig("MACAddress")
                End If
            Next
        Next

But if you guys know a better way to do this, just let me hear it...
 
I am facing the same problem! Any solutions for this?
My problem is that the code is running on all but one machine. What can be the problem in this code:

Code:
string macstr=null;
				ManagementClass myManagementClass = new ManagementClass("Win32_NetworkAdapterConfiguration");

				ManagementObjectCollection moc = myManagementClass.GetInstances();


				foreach(ManagementObject mo in moc)
				{
					try
					{
						macstr=mo["MacAddress"].ToString();
					}
					catch(Exception e)
					{
						break;
					}
					break;
				}
 
Back
Top