Autor Beitrag
SMI
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 106

Win95-2003 / Debian / Suse
D1/D3/D6/D7
BeitragVerfasst: Mo 26.08.02 02:28 
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

ausblenden volle Höhe Delphi-Quelltext
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:
// Pleace notice, I called the Lan Adapters within a function Lana
// Outside a function I named them Adapter

FUNCTION GetMacAddress(AdapterID : Integer) : String;
TYPE
 TAdapterStatusA = record
   Adapt : TAdapterStatus;
   NameBuff : array[0..30of TNameBuffer;
 end;
VAR
  NCB           :TNCB;                   //Network Control Block
  ReturnCode    :Char;                   //Retun Code
  AdapterStatus :TAdapterStatus;
BEGIN
 //Clear NCB Structure
 FillChar( NCB, SizeOf(NCB), 0 );

 //Programm NBC to fulfill a reset. It doublecheck if the Lana is present.
 //Can be removed, when used with GetAdapters.
 NCB.ncb_command  := Char(NCBRESET);
 NCB.ncb_lana_num := Char(AdapterID);

 //Process NCB to NetBios
 ReturnCode := NetBios(@NCB);

 //Check if query was successfull, if returnCode = #0
 IF ReturnCode <> #0 THEN
 BEGIN
  // Return the negative returncode
  // Error check would be "if ReturnCode[0]='-' Then..."
  Result:=Char(Ord(ReturnCode)*-1);
  Exit;
 END;

 //Clear NCB Structure
 FillChar( NCB, SizeOf(NCB), 0 );

 //Programm NBC to ask for the MAC associated with the LanaID
 NCB.ncb_command  := Char(NCBASTAT);
 NCB.ncb_lana_num := Char(AdapterID);
 NCB.ncb_buffer   := @AdapterStatus;
 NCB.ncb_length   := SizeOf(AdapterStatus);
 StrCopy(NCB.ncb_callname, '* ' );

 //Process NCB to NetBios
 ReturnCode := Netbios( @NCB );

 //Check if query was successfull, if returnCode = #0
 IF ReturnCode <> #0 THEN
 BEGIN
  // Return the negative returncode
  Result:=Char(Ord(ReturnCode)*-1);
  Exit;
 END;

 //Return formated MAC
 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 GetAdapter
//=================
//
// This function will return the the total nummber of all lana's istalled
// and a unique the id of each lana.
// E.g. on a system containing lana's 0, 2 and 3, a structure with
// length =3, lana[0]=0, lana[1]=2 and lana[2]=3 will be returned.

FUNCTION GetAdapters: TLanaEnum;
VAR
  NCB        :TNCB;                   //Network Control Block
  LanaEnum   :TLanaEnum;              //Enumeration of Lan Adapters
  ReturnCode :Char;                   //Retun Code

BEGIN
 //Clear NCB Structure
 FillChar(NCB, SizeOf(NCB), 0);
 //Clear LanaEnum Stucture
 FillChar(LanaEnum, SizeOf(TLanaEnum), 0);

 //Programm NBC to ask for the number of Lana's
 NCB.ncb_Command := CHAR(NCBENUM);
 NCB.ncb_buffer  := @LanaEnum;
 NCB.ncb_length  := Sizeof(LanaEnum);

 //Ask NetBios
 ReturnCode := NetBios(@NCB);

 //Check if query was successfull, if returnCode = #0
 IF ReturnCode <> #0 THEN
 BEGIN
  LanaEnum.Length  := CHAR(0);
  LanaEnum.Lana[0] := ReturnCode;
 END;

 // Return valid LanaEnum Structure
 Result := LanaEnum;
END;


PROCEDURE TForm1.Button1Click(Sender: TObject);
VAR
 L_Enum :TLanaEnum;
 i      :INTEGER;
BEGIN
 L_Enum := GetAdapters;                        { enumerate lanas for WIN NT }
 IF L_Enum.Length = '0' THEN
 BEGIN
  Exit;
 END;

 FOR i:= 0 TO (BYTE(L_Enum.Length) - 1DO
 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 user profile iconNarses: Beitrag überarbeitet.


NB30.pas  (17.41 KB) Download (Rev 0)
 (659x, 659x gesamt)
Beschreibung: