// Fetches the MAC address and prints it
static void GetMACaddress(void)
{
IP_ADAPTER_INFO AdapterInfo[16]; // Allocate information
// for up to 16 NICs
DWORD dwBufLen = sizeof(AdapterInfo); // Save memory size of buffer
DWORD dwStatus = GetAdaptersInfo( // Call GetAdapterInfo
AdapterInfo, // [out] buffer to receive data
&dwBufLen); // [in] size of receive data buffer
assert(dwStatus == ERROR_SUCCESS); // Verify return value is
// valid, no buffer overflow
PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo; // Contains pointer to
// current adapter info
do {
PrintMACaddress(pAdapterInfo->Address); // Print MAC address
pAdapterInfo = pAdapterInfo->Next; // Progress through
// linked list
}
while(pAdapterInfo); // Terminate if last adapter
}
这个论坛随便搜索一下就有了。这个算法太常见了。下面就是抄来的 。
type
ASTAT =record
adapt:TADAPTERSTATUS ;
NameBuff:array[0..30] of TNameBuffer;
end;
New_byte=array[0..5] of byte;
var
NewByte:New_Byte;
procedure TfrmMain.GetNetCardAddress;
var
ncb:^TNCB;
AdapterList:^TLanaEnum;
ret:Char;
i,K:integer;
Adapter:^ASTAT;
textt:string;
// pAdd:string;
// m_byteAddressMySelf:string;
// m_ctrlThisNetCard:TStrings;
begin
// m_ctrlThisNetCard:=TStringList.Create;
getMem(ncb,sizeof(TNCB));
getMem(AdapterList,sizeof(TLanaEnum));
ncb^.ncb_command:=char(NCBENUM);
Ncb^.ncb_buffer := pchar(AdapterList);
Ncb^.ncb_length := sizeof(AdapterList);
// ret :=Netbios(PNCB(ncb));
Netbios(PNCB(ncb));
if (Ncb^.ncb_retcode<>char(NRC_GOODRET)) then exit;
For I:=0 to ord(AdapterList^.Length) do
begin
getmem(ncb,sizeof(TNCB));
Ncb^.ncb_command := char(NCBRESET);
Ncb^.ncb_lana_num := AdapterList.lana[i];
if (Netbios(PNCB(Ncb)) <> char(NRC_GOODRET)) then exit;
if (ret<> #0) then exit;
for K:=0 to 5 do
if(Adapter^.adapt.adapter_address[K]<>#0)then break;
if(K>=6)then continue;
textt:=format('%.02X %.02X %.02X %.02X %.02X %.02X',
[ord(Adapter^.adapt.adapter_address[0]),
ord(Adapter^.adapt.adapter_address[1]),
ord(Adapter^.adapt.adapter_address[2]),
ord(Adapter^.adapt.adapter_address[3]),
ord(Adapter^.adapt.adapter_address[4]),
ord(Adapter^.adapt.adapter_address[5])]);
if(m_ctrlThisNetCard.items.IndexOf(textt)=LB_ERR)then
m_ctrlThisNetCard.items.Add(textt);
end;
end;
function TfrmMain.HextoDec(const Hex: string): integer;
var
I:integer;
C:char;
Value:integer;
begin
value:=0;
For I:=1 to Length(Hex) do
begin
C:=Hex[I];
if(C>='A') and (C<='F')then
value:=Value*10+(ord(C)-ord('A')+10)
else
Value:=Value*10+(ord(C)-ord('0'));
end;
result:=Value;
end;
function TfrmMain.SetStringtobyte(var NewByte: New_byte;
const SourceStr: string): boolean;
var
str,tmp:string;
start,I:integer;
begin
try
For I:=0 to 5 do
NewByte[I]:=0;
tmp:=Uppercase(SourceStr+' ');
for I:=0 to 5 do
begin
start:=pos(' ',tmp);
inc(start,-1);
str:=copy(tmp,0,start);
NewByte[I]:=HextoDec(str);
tmp:=copy(tmp,start+1,length(tmp)-start);
tmp:=trimLeft(tmp);
end;
result:=True;
except
result:=False;
end;
end;