program GetMAC; uses Dialogs, SysUtils, nb30; function GetMACAddress(PCName: string) : string; type TASTAT = packed record adapt: nb30.TADAPTERSTATUS; NameBuff: array [0..30] of TNAMEBUFFER; end; var NCB: TNCB; Tmp: String; pASTAT: Pointer; AST: TASTAT; begin // The IBM NetBIOS 3.0 specifications defines four basic // NetBIOS environments under the NCBRESET command. Win32 // follows the OS/2 Dynamic Link Routine (DLR) environment. // This means that the first NCB issued by an application // must be a NCBRESET, with the exception of NCBENUM. // The Windows NT implementation differs from the IBM // NetBIOS 3.0 specifications in the NCB_CALLNAME field. FillChar(NCB, SizeOf(NCB), 0); NCB.ncb_command := Chr(NCBRESET); NetBios(@NCB); // To get the Media Access Control (MAC) address for an // ethernet adapter programmatically, use the Netbios() // NCBASTAT command and provide a "*" as the name in the // NCB.ncb_CallName field (in a 16-chr string). // NCB.ncb_callname = "* " FillChar(NCB, SizeOf(NCB), 0); FillChar(NCB.ncb_callname[0], 16, ' '); Move(PCName[1], NCB.ncb_callname[0], Length(PCName)); NCB.ncb_command := Chr(NCBASTAT); // For machines with multiple network adapters you need to // enumerate the LANA numbers and perform the NCBASTAT // command on each. Even when you have a single network // adapter, it is a good idea to enumerate valid LANA numbers // first and perform the NCBASTAT on one of the valid LANA // numbers. It is considered bad programming to hardcode the // LANA number to 0 (see the comments section below). NCB.ncb_lana_num := #0; NCB.ncb_length := SizeOf(AST); GetMem(pASTAT, NCB.ncb_length); if pASTAT=nil then begin result := 'memory allocation failed!'; exit; end; NCB.ncb_buffer := pASTAT; NetBios(@NCB); Move(NCB.ncb_buffer, AST, SizeOf(AST)); with AST.adapt do Tmp := Format('%.2x-%.2x-%.2x-%.2x-%.2x-%.2x', [ord(adapter_address[0]), ord(adapter_address[1]), ord(adapter_address[2]), ord(adapter_address[3]), ord(adapter_address[4]), ord(adapter_address[5])]); FreeMem(pASTAT); Result := Tmp; end; begin ShowMessage(GetMACAddress('*')); end. 试验证明,以下代码才是可靠的 unit Unit2; interface function GetMACAddress: string; implementation uses SysUtils,NB30; function GetAdapterInfo(Lana: Char): String; var Adapter:TAdapterStatus; NCB: TNCB; begin FillChar(NCB, SizeOf(NCB), 0); NCB.ncb_command := Char(NCBRESET); NCB.ncb_lana_num := Lana; if Netbios(@NCB) <> Char(NRC_GOODRET) then begin Result := 'mac not found'; Exit; end; FillChar(NCB, SizeOf(NCB), 0); NCB.ncb_command := Char(NCBASTAT); NCB.ncb_lana_num := Lana; NCB.ncb_callname := '*'; FillChar(Adapter, SizeOf(Adapter), 0); NCB.ncb_buffer := @Adapter; NCB.ncb_length := SizeOf(Adapter); if Netbios(@NCB) <> Char(NRC_GOODRET) then begin Result := 'mac not found'; Exit; end; Result := IntToHex(Byte(Adapter.adapter_address[0]), 2) + '-' + IntToHex(Byte(Adapter.adapter_address[1]), 2) + '-' + IntToHex(Byte(Adapter.adapter_address[2]), 2) + '-' + IntToHex(Byte(Adapter.adapter_address[3]), 2) + '-' + IntToHex(Byte(Adapter.adapter_address[4]), 2) + '-' + IntToHex(Byte(Adapter.adapter_address[5]), 2); end; function GetMACAddress: string; var AdapterList: TLanaEnum; NCB: TNCB; begin FillChar(NCB, SizeOf(NCB), 0); NCB.ncb_command := Char(NCBENUM); NCB.ncb_buffer := @AdapterList; NCB.ncb_length := SizeOf(AdapterList); Netbios(@NCB); if Byte(AdapterList.length) > 0 then Result := GetAdapterInfo(AdapterList.lana[0]) else Result := 'mac not found'; end; end.