Re: How to get mac addresses for physical net adapters on XP



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.



.



Relevant Pages

  • [Full-disclosure] Lets make a spy-proof communications infrastructure
    ... cell phone network which will run like a peer to peer network, ... If we devised some private communicator, ... While truly global routing might require some relays to bridge areas ...
    (Full-Disclosure)
  • [Full-disclosure] Lets design a spy-proof communications infrastructure
    ... cell phone network which will run like a peer to peer network, ... If we devised some private communicator, ... While truly global routing might require some relays to bridge areas ...
    (Full-Disclosure)
  • Re: [Full-disclosure] Lets make a spy-proof communications infrastructure
    ... cell phone network which will run like a peer to peer network, ... If we devised some private communicator, ... While truly global routing might require some relays to bridge areas ... of the government and the intelligence services. ...
    (Full-Disclosure)
  • Re: About uses NetBios() to get MAC,HELP!!!
    ... there is more than one network card seen by the machine ... Private Const NCBASTAT As Long = &H33 ... ncb_callname As String * NCBNAMSZ ... 'enumerate the LANA numbers and perform the NCBASTAT ...
    (microsoft.public.vb.winapi)
  • Re: Connection denied
    ... private IP addresses (also known as "non-routable" IP ... Because private addresses have no global meaning, routing information ... At the beginning the network rule was:>> Source networks: Internal ... >> I could ping any external destination. ...
    (microsoft.public.isaserver)