[structlayout(layoutkind.auto)]
public struct ASTAT
{
public ADAPTER_STATUS adapt;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=(int)NCBCONST.NUM_NAMEBUF)]
public NAME_BUFFER NameBuff;
}
public class Win32API
{
[DllImport("NETAPI32.DLL")]
public static extern char Netbios(ref NCB ncb);
}
public string GetMacAddress()
{
string addr="";
try
{
int cb;
ASTAT adapter;
NCB Ncb=new NCB();
char uRetCode;
LANA_ENUM lenum;
ncb.ncb_command = (byte)NCBCONST.NCBENUM;
cb = Marshal.SizeOf(typeof(LANA_ENUM));
Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);
Ncb.ncb_length = (ushort)cb;
uRetCode = Win32API.Netbios(ref Ncb);
lenum = (LANA_ENUM)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(LANA_ENUM));
Marshal.FreeHGlobal(Ncb.ncb_buffer);
if(uRetCode != (short)NCBCONST.NRC_GOODRET)
return "";
for(int i=0; i < lenum.length ;i++)
{
Ncb.ncb_command = (byte)NCBCONST.NCBRESET;
Ncb.ncb_lana_num = lenum.lana[i];
uRetCode = Win32API.Netbios(ref Ncb);
if(uRetCode != (short)NCBCONST.NRC_GOODRET)
return "";
ncb.ncb_command = (byte)NCBCONST.NCBASTAT;
Ncb.ncb_lana_num = lenum.lana[i];
Ncb.ncb_callname[0]=(byte)''*'';
cb = Marshal.SizeOf(typeof(ADAPTER_STATUS)) + Marshal.SizeOf(typeof(NAME_BUFFER))*(int)NCBCONST.NUM_NAMEBUF;
Ncb.ncb_buffer = Marshal.AllocHGlobal(cb);
Ncb.ncb_length = (ushort)cb;
uRetCode = Win32API.Netbios(ref Ncb);
adapter.adapt = (ADAPTER_STATUS)Marshal.PtrToStructure(Ncb.ncb_buffer, typeof(ADAPTER_STATUS));
Marshal.FreeHGlobal(Ncb.ncb_buffer);
if (uRetCode == (short)NCBCONST.NRC_GOODRET)
{
if(i>0)
addr += ":";
addr = string.Format("{0,2:X}{1,2:X}{2,2:X}{3,2:X}{4,2:X}{5,2:X}",
adapter.adapt.adapter_address[0],
adapter.adapt.adapter_address,
adapter.adapt.adapter_address,
adapter.adapt.adapter_address,
adapter.adapt.adapter_address,
adapter.adapt.adapter_address);
}
}
}
catch
{}
return addr.Replace('' '', ''0'');
}
}
}