Ergänzung zum FAQ-Beitrag:
Wie lese ich die MAC-Adresse aus?
Mit folgendem Code kann man von mehrere LanAs die Mac Addresse herausfinden. Da ich vor kurzem ein Programm schreiben musste, das alle Macs von allen im System verbauten Lanas herausfindet, habe ich gedacht, dass ich Anderen damit viel Zeitsparen kann und veröffentliche hier einfach die entsprechendendie Quellcodezeilen. Kommentare sind noch in English, müsste ja jeder versethen, des weitern muss man die UNIT NB30 einbinden. Das Programm funktioniert bisher sehr zuverlässig, selbst auf meinem PC, der zurzeit über 3 Netzwerkkarten und 2 ISDN Karten verfügt.
SMI
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121:
| FUNCTION GetMacAddress(AdapterID : Integer) : String; TYPE TAdapterStatusA = record Adapt : TAdapterStatus; NameBuff : array[0..30] of TNameBuffer; end; VAR NCB :TNCB; ReturnCode :Char; AdapterStatus :TAdapterStatus; BEGIN FillChar( NCB, SizeOf(NCB), 0 );
NCB.ncb_command := Char(NCBRESET); NCB.ncb_lana_num := Char(AdapterID);
ReturnCode := NetBios(@NCB);
IF ReturnCode <> #0 THEN BEGIN Result:=Char(Ord(ReturnCode)*-1); Exit; END;
FillChar( NCB, SizeOf(NCB), 0 );
NCB.ncb_command := Char(NCBASTAT); NCB.ncb_lana_num := Char(AdapterID); NCB.ncb_buffer := @AdapterStatus; NCB.ncb_length := SizeOf(AdapterStatus); StrCopy(NCB.ncb_callname, '* ' );
ReturnCode := Netbios( @NCB );
IF ReturnCode <> #0 THEN BEGIN Result:=Char(Ord(ReturnCode)*-1); Exit; END;
result:=Format('%2.2x-%2.2x-%2.2x-%2.2x-%2.2x-%2.2x', [ORD(AdapterStatus.Adapter_address[0]), ORD(AdapterStatus.Adapter_address[1]), ORD(AdapterStatus.Adapter_address[2]), ORD(AdapterStatus.Adapter_address[3]), ORD(AdapterStatus.Adapter_address[4]), ORD(AdapterStatus.Adapter_address[5])]) end;
FUNCTION GetAdapters: TLanaEnum; VAR NCB :TNCB; LanaEnum :TLanaEnum; ReturnCode :Char; BEGIN FillChar(NCB, SizeOf(NCB), 0); FillChar(LanaEnum, SizeOf(TLanaEnum), 0);
NCB.ncb_Command := CHAR(NCBENUM); NCB.ncb_buffer := @LanaEnum; NCB.ncb_length := Sizeof(LanaEnum);
ReturnCode := NetBios(@NCB);
IF ReturnCode <> #0 THEN BEGIN LanaEnum.Length := CHAR(0); LanaEnum.Lana[0] := ReturnCode; END;
Result := LanaEnum; END;
PROCEDURE TForm1.Button1Click(Sender: TObject); VAR L_Enum :TLanaEnum; i :INTEGER; BEGIN L_Enum := GetAdapters; IF L_Enum.Length = '0' THEN BEGIN Exit; END;
FOR i:= 0 TO (BYTE(L_Enum.Length) - 1) DO BEGIN ListBox1.Items.Add(getMACaddress(BYTE(L_Enum.Lana[i]))); END; END; |
Hinweis:
Hier ist eine Lösung, die die MAC-Adresse eines beliebigen PCs im LAN ermitteln kann, auch die eigene (=localhost).
Moderiert von Narses: Beitrag überarbeitet.