function GetAdaptersInfo(pAdapterInfo: PIP_ADAPTER_INFO;
pOutBufLen: PDWORD): DWORD; stdcall;
external 'IPHLPAPI.DLL' name 'GetAdaptersInfo';
implementation
procedure ShowMACS();
var
pbuf: PIP_ADAPTER_INFO;
buflen: DWORD;
i: integer;
mac: string;
begin
buflen := 0;
if GetAdaptersInfo(pbuf, @bufLen) = ERROR_BUFFER_OVERFLOW then
begin
pbuf := AllocMem(buflen);
if GetAdaptersInfo(pbuf, @bufLen) = ERROR_SUCCESS then
while pbuf <> nil do
begin
mac := '';
for i := 0 to 5 do
mac := mac + IntToHex(pbuf.Address[i], 2);
ShowMessage(mac);
pbuf := pbuf.Next;
end;
FreeMem(pbuf);
end;
end;
type
TMACAddress = packed array[0..5] of Byte;
ENetBiosError = class(Exception);
TAStat = record Adapt: TAdapterStatus;
NameBuff: array[0..30] of TNameBuffer;
end;
function GetMacAddress(AdapterNum: Integer): TMACAddress;
var
Ncb: TNCB;
uRetCode: Char;
J: Integer;
Adapter: TAStat;
begin
FillChar(NCB, SizeOf(NCB), 0);
with NCB do
begin
ncb_command := Char(NCBRESET);
ncb_lana_num := Char(AdapterNum);
end;
uRetCode := Netbios(@Ncb);
if uRetCode <> #0 then raise Exception.CreateFmt(sNetBIOSError, [Ord(uRetCode)]);
FillChar(NCB, SizeOf(NCB), 0);
with NCB do
begin
ncb_command := Char(NCBASTAT);
ncb_lana_num := Char(AdapterNum);
StrCopy(ncb_callname, '*');
ncb_buffer := @Adapter;
ncb_length := sizeof(Adapter);
end;
uRetCode := Netbios(@Ncb);
if uRetCode <> #0 then raise Exception.CreateFmt(sNetBIOSError, [Ord(uRetCode)]);
for J := 0 to 5 do
Result[J] := Ord(Adapter.Adapt.Adapter_address[J]);
end;
方法2:
uses nb30;
function NBGetAdapterAddress(a: Integer): string;
var
NCB: TNCB; // Netbios control block //NetBios控制块
ADAPTER: TADAPTERSTATUS; // Netbios adapter status//取网卡状态
LANAENUM: TLANAENUM; // Netbios lana
intIdx: Integer; // Temporary work value//临时变量
cRC: Char; // Netbios return code//NetBios返回值
strTemp: string; // Temporary string//临时变量
begin
Result := '';
try
ZeroMemory(@NCB, SizeOf(NCB)); // Zero control blocl
strTemp := ''; // Convert it to string
for intIdx := 0 to 5 do
strTemp := strTemp + InttoHex(Integer(ADAPTER.adapter_address[intIdx]), 2);
Result := strTemp;
finally
end;
end;