Re: How to get mac addresses for physical net adapters on XP
- From: jmagaram <jmagaram@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 14 Dec 2007 13:14:06 -0800
i think this code works. i'd appreciate it if someone else would try it.
public static class NetworkAdapterInpector {
private const int ERROR_SUCCESS = 0;
private const int ERROR_BUFFER_OVERFLOW = 111;
private const int MAX_ADAPTER_DESCRIPTION_LENGTH = 128;
private const int MAX_ADAPTER_NAME_LENGTH = 256;
private const int MAX_ADAPTER_ADDRESS_LENGTH = 8;
public static List<NetworkAdapter> GetNetworkAdapters() {
IntPtr pArray = IntPtr.Zero;
try {
List<NetworkAdapter> results = new List<NetworkAdapter>();
long adapterInfoSize =
Marshal.SizeOf(typeof(IP_ADAPTER_INFO));
pArray = Marshal.AllocHGlobal(new IntPtr(adapterInfoSize));
int apiResult = GetAdaptersInfo(pArray, ref adapterInfoSize);
if (apiResult == ERROR_BUFFER_OVERFLOW) {
pArray = Marshal.ReAllocHGlobal(pArray, new
IntPtr(adapterInfoSize));
apiResult = GetAdaptersInfo(pArray, ref adapterInfoSize);
}
if (apiResult == ERROR_SUCCESS) {
IntPtr pEntry = pArray;
do {
NetworkAdapter netAdapter = new NetworkAdapter();
IP_ADAPTER_INFO adapter =
(IP_ADAPTER_INFO)Marshal.PtrToStructure(pEntry, typeof(IP_ADAPTER_INFO));
netAdapter.Description = adapter.AdapterDescription;
netAdapter.Type = adapter.Type;
if (adapter.AddressLength > 0) {
List<byte> macAddress = new List<byte>();
for (int i = 0; i < adapter.AddressLength; i++) {
macAddress.Add(adapter.Address[i]);
}
netAdapter.MacAddress = macAddress.ToArray();
}
results.Add(netAdapter);
pEntry = adapter.Next;
}
while (pEntry != IntPtr.Zero);
}
return results;
}
finally {
Marshal.FreeHGlobal(pArray);
}
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
private struct IP_ADAPTER_INFO {
public IntPtr Next;
public Int32 ComboIndex;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
MAX_ADAPTER_NAME_LENGTH + 4)]
public string AdapterName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst =
MAX_ADAPTER_DESCRIPTION_LENGTH + 4)]
public string AdapterDescription;
public UInt32 AddressLength;
[MarshalAs(UnmanagedType.ByValArray, SizeConst =
MAX_ADAPTER_ADDRESS_LENGTH)]
public byte[] Address;
public Int32 Index;
public UInt32 Type;
public UInt32 DhcpEnabled;
public IntPtr CurrentIpAddress;
public IP_ADDR_STRING IpAddressList;
public IP_ADDR_STRING GatewayList;
public IP_ADDR_STRING DhcpServer;
public bool HaveWins;
public IP_ADDR_STRING PrimaryWinsServer;
public IP_ADDR_STRING SecondaryWinsServer;
public Int32 LeaseObtained;
public Int32 LeaseExpires;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
private struct IP_ADDR_STRING {
public IntPtr Next;
public IP_ADDRESS_STRING IpAddress;
public IP_ADDRESS_STRING IpMask;
public Int32 Context;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
private struct IP_ADDRESS_STRING {
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string Address;
}
[DllImport("iphlpapi.dll", CharSet = CharSet.Ansi)]
private static extern int GetAdaptersInfo(IntPtr pAdapterInfo, ref
Int64 pBufOutLen);
}
"Volodymyr Shcherbyna" wrote:
Try GetAdaptersInfo. It gives you mac address of adapter in.
'IP_ADAPTER_INFO::Address' member.
--
Volodymyr
"jmagaram" <jmagaram@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:DEA2BF45-7FD6-4533-92C1-8CDCAFC0EC08@xxxxxxxxxxxxxxxx
I am trying to determine a unique, fairly unchangeable identifier for a
computer. One component of this hardware fingerprint will be the mac
addresses of physical network adapter. How do I get it, ideally from C#. I
want to get the same list of network adapters you see in the
DeviceManager.
I've run into a bunch of dead ends.
System.Net.NetworkInformation.NetworkInterface returns info about logical
adapters, which I don't want. I can't figure out how to distinguish
logical
from physical adapters here.
Win32_NetworkAdapter works fine on Vista as long as I filter for
PhysicalAdapter=TRUE, but this value is missing on XP.
Win32_NetworkAdapterConfiguration with IPEnabled=TRUE doesn't contain
enough
data. My laptop has both a wireless and wired connection but only one is
being used at a time.
GetAdaptersInfo seems promising but I haven't tried it yet.
I really only need 1 mac address, ideally of the non-wireless "primary"
network card if there is such a thing.
- References:
- Re: How to get mac addresses for physical net adapters on XP
- From: Volodymyr Shcherbyna
- Re: How to get mac addresses for physical net adapters on XP
- Prev by Date: Re: Does Windows XP Embedded have the 10 TCP/IP limit that XP has?
- Next by Date: Ack packets not standard
- Previous by thread: Re: How to get mac addresses for physical net adapters on XP
- Index(es):
Relevant Pages
|
|