Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WMI in my c# app to get mac & IP address

15 views
Skip to first unread message

Jason

unread,
Nov 30, 2006, 6:55:21 PM11/30/06
to
Hello

I'm writting a c# app, and I've got a computer that has two built in nic's.
One is a Intel Pro/100 and the other is a Intel Pro/1000.

I want to get the mac and ip address assigned to the Intel Pro 100.

Anyone have idea, examples of how I can get that information to display to a
user?


no potted meat@hotmail.com David Browne

unread,
Nov 30, 2006, 8:46:06 PM11/30/06
to

"Jason" <som...@microsoft.com> wrote in message
news:ul4ZAsNF...@TK2MSFTNGP03.phx.gbl...

Open Server Explorer, expand your computer, expand Management Classes,
right-click on the desired WMI class and generate a managed wrapper for it.

David

Jason

unread,
Dec 1, 2006, 8:53:45 AM12/1/06
to
Ok, I got that much, but it looks like I'm going to need a bit more help.
Can I use that as a class to get the ionfo I need for my particular network
adapter?

"David Browne" <davidbaxterbrowne no potted me...@hotmail.com> wrote in
message news:%2366m5pO...@TK2MSFTNGP03.phx.gbl...

no potted meat@hotmail.com David Browne

unread,
Dec 1, 2006, 10:06:40 AM12/1/06
to

"Jason" <som...@microsoft.com> wrote in message

news:uq3qfAVF...@TK2MSFTNGP03.phx.gbl...


> Ok, I got that much, but it looks like I'm going to need a bit more help.
> Can I use that as a class to get the ionfo I need for my particular
> network adapter?
>

Then do a seach on "wmi ip mac" and discover that that information is under
the Win32_NetworkAdapter class. And find the following snippet of code

no potted meat@hotmail.com David Browne

unread,
Dec 1, 2006, 10:17:42 AM12/1/06
to

"David Browne" <davidbaxterbrowne no potted me...@hotmail.com> wrote in

message news:eQ9jOpVF...@TK2MSFTNGP04.phx.gbl...

oops

strComputer = "."
Set objWMIService = GetObject(_
"winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapter " _
& "Where NetConnectionID = " & _
"'Local Area Connection 2'")

For Each objItem in colItems
strMACAddress = objItem.MACAddress
Next

Set colItems = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration")

For Each objItem in colItems
If objItem.MACAddress = strMACAddress Then
For Each strIPAddress in objItem.IPAddress
Wscript.Echo "IP Address: " & strIPAddress
Next
End If
Next


Which tells you the classes you will need to use. Aparently you need to use
Win32_NetworkAdapter to enumerate the nic's and then match each nic with its
Win32_NetworkAdapterConfiguration by matching the MAC address. In the
server explorer you need so fumble around and add an additional class (right
click on Management Classes, add class). And find
Win32_NetworkAdapterConfiguration under root\CIMV2\Network Adapter Settings.
Add managed wrappers for both classes, and then write:

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using csTest.ROOT.CIMV2;

namespace csTest
{

class Program
{


public static void Main(string[] args)
{
Dictionary<string,NetworkAdapterConfiguration> configs =
new Dictionary<string,NetworkAdapterConfiguration>();

foreach (NetworkAdapterConfiguration config in
NetworkAdapterConfiguration.GetInstances())
{
if (config.MACAddress != null)
{
configs.Add(config.MACAddress, config);
}
}

foreach (NetworkAdapter nic in NetworkAdapter.GetInstances())
{
Console.WriteLine(string.Format("NIC {0} MAC
{1}",nic.Name,nic.MACAddress));

if (nic.MACAddress != null)
{
NetworkAdapterConfiguration config = configs[nic.MACAddress];

string[] addresses = config.IPAddress;
if (addresses == null)
{
addresses = new string[0];
}
foreach (string ip in addresses)
{
Console.WriteLine(string.Format(" Address {0}",
config.IPAddress));
}
}

}
}
}

}


Piece of cake, huh :). But you can get to everything, I mean everything,
using WMI.

David

0 new messages