public string GetMacAddress() { string str = ""; try { HardwareInfo.NCB ncb = new HardwareInfo.NCB(); ncb.ncb_command = (byte)55; int cb1 = Marshal.SizeOf(typeof(HardwareInfo.LANA_ENUM)); ncb.ncb_buffer = Marshal.AllocHGlobal(cb1); ncb.ncb_length = (ushort)cb1; char ch1 = HardwareInfo.Win32API.Netbios(ref ncb); HardwareInfo.LANA_ENUM structure = (HardwareInfo.LANA_ENUM)Marshal.PtrToStructure(ncb.ncb_buffer, typeof(HardwareInfo.LANA_ENUM)); Marshal.FreeHGlobal(ncb.ncb_buffer); if (ch1 != char.MinValue) { return(""); } for (int index = 0; index < (int)structure.length; ++index) { ncb.ncb_command = (byte)50; ncb.ncb_lana_num = structure.lana[index]; if (HardwareInfo.Win32API.Netbios(ref ncb) != char.MinValue) { return(""); } ncb.ncb_command = (byte)51; ncb.ncb_lana_num = structure.lana[index]; ncb.ncb_callname[0] = (byte)42; int cb2 = Marshal.SizeOf(typeof(HardwareInfo.ADAPTER_STATUS)) + Marshal.SizeOf(typeof(HardwareInfo.NAME_BUFFER)) * 30; ncb.ncb_buffer = Marshal.AllocHGlobal(cb2); ncb.ncb_length = (ushort)cb2; char ch2 = HardwareInfo.Win32API.Netbios(ref ncb); HardwareInfo.ASTAT astat; astat.adapt = (HardwareInfo.ADAPTER_STATUS)Marshal.PtrToStructure(ncb.ncb_buffer, typeof(HardwareInfo.ADAPTER_STATUS)); Marshal.FreeHGlobal(ncb.ncb_buffer); if (ch2 == char.MinValue) { if (index > 0) { str += ":"; } str = string.Format("{0,2:X}{1,2:X}{2,2:X}{3,2:X}{4,2:X}{5,2:X}", (object)astat.adapt.adapter_address[0], (object)astat.adapt.adapter_address[1], (object)astat.adapt.adapter_address[2], (object)astat.adapt.adapter_address[3], (object)astat.adapt.adapter_address[4], (object)astat.adapt.adapter_address[5]); } } } catch { } return(str.Replace(' ', '0')); }
public static extern char Netbios(ref HardwareInfo.NCB ncb);