Here is the VB.net code which displays the information of ipconfig /all
which includes the MAC address of the machine. This will work on Windows 98
and higher and windows 2000 and higher. It makes use of Ip helper api's
GetNetworkParams and GetAdapterInfo() api
Here is the code
----------------------------------------------------------------------------
----------------------------------------------------------------------------
-----------------------------------------
'=====================================================================
' File: IPConfig.vb
' Summary: Demonstrates how to programatically retrieve IP configuration
' information by invoking native win32 API's using unmanaged code
' in C#. The output is very similar to IPCONFIG.EXE utility.
'---------------------------------------------------------------------
' This file is part of the Microsoft .NET Framework SDK Code Samples.
' Copyright (C) 2000 Microsoft Corporation. All rights reserved.
'This source code is intended only as a supplement to Microsoft
'Development Tools and/or on-line documentation. See these other
'materials for detailed information regarding Microsoft code samples.
'THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
'KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
'IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'PARTICULAR PURPOSE.
'=====================================================================
'Add the following namespaces
Imports System
Imports System.Runtime.InteropServices
Imports System.Text
Imports System.Threading
Imports Microsoft.VisualBasic.Constants
Imports Microsoft.VisualBasic.Conversion
'Constants used in the program as defined in IPExport.h and WinError.h
Public Class IPConfigConst
Public Const MAX_ADAPTER_NAME As Integer = 128
Public Const MAX_HOSTNAME_LEN As Integer = 128
Public Const MAX_DOMAIN_NAME_LEN As Integer = 128
Public Const MAX_SCOPE_ID_LEN As Integer = 256
Public Const MAX_ADAPTER_DESCRIPTION_LENGTH As Integer = 128
Public Const MAX_ADAPTER_NAME_LENGTH As Integer = 256
Public Const MAX_ADAPTER_ADDRESS_LENGTH As Integer = 8
Public Const DEFAULT_MINIMUM_ENTITIES As Integer = 32
Public Const ERROR_BUFFER_OVERFLOW As Integer = 111
Public Const ERROR_SUCCESS As Integer = 0
End Class
'Different Adapter types as defined in IPifcons.h
Public Class IPAdapterTypes
Public Const MIB_IF_TYPE_OTHER As Integer = 1
Public Const MIB_IF_TYPE_ETHERNET As Integer = 6
Public Const MIB_IF_TYPE_TOKENRING As Integer = 9
Public Const MIB_IF_TYPE_FDDI As Integer = 15
Public Const MIB_IF_TYPE_PPP As Integer = 23
Public Const MIB_IF_TYPE_LOOPBACK As Integer = 24
Public Const MIB_IF_TYPE_SLIP As Integer = 28
End Class
'declared as structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure IPAdapterInfo
Public NextPointer As IntPtr
Public ComboIndex As Integer
<MarshalAs(UnmanagedType.ByValTStr, _
SizeConst:=IPConfigConst.MAX_ADAPTER_NAME_LENGTH + 4)> _
Public AdapterName As String
<MarshalAs(UnmanagedType.ByValTStr, _
SizeConst:=IPConfigConst.MAX_ADAPTER_DESCRIPTION_LENGTH + 4)> _
Public Description As String
Public AddressLength As Integer
<MarshalAs(UnmanagedType.ByValArray, _
SizeConst:=IPConfigConst.MAX_ADAPTER_ADDRESS_LENGTH)> _
Public Address() As Byte
Public Index As Integer
Public Type As Integer
Public DhcpEnabled As Integer
Public CurrentIPAddress As IntPtr
Public IPAddressList As IPAddrString
Public GatewayList As IPAddrString
Public DhcpServer As IPAddrString
Public HaveWins As Boolean
Public PrimaryWinsServer As IPAddrString
Public SecondaryWinsServer As IPAddrString
Public LeaseObtained As Integer
Public LeaseExpires As Integer
End Structure
'declared as structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
Public Structure IPAddrString
Public NextPointer As IntPtr
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=4 * 4)> _
Public IPAddressString As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=4 * 4)> _
Public IPMaskString As String
Public Context As Integer
End Structure
'typedef struct _IP_ADAPTER_INDEX_MAP
'{
' ULONG Index // adapter index
' WCHAR Name [MAX_ADAPTER_NAME]; // name of the adapter
'}
'declared as structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure IPAdapterIndexMap
Public Index As Integer
<MarshalAs(UnmanagedType.ByValTStr, _
SizeConst:=IPConfigConst.MAX_ADAPTER_NAME)> _
Public Name As String
End Structure
'typedef struct _IP_INTERFACE_INFO
'{
' LONG NumAdapters; // number of adapters in array
' IP_ADAPTER_INDEX_MAP Adapter[1]; // adapter indices and names
'}
'declared as structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure IPInterfaceInfo
Public NumAdapters As Integer
Public Adapter As IPAdapterIndexMap
End Structure