代码是网上载的,再自己稍微改了下function GetMacAddress: String;
var
i: integer;
s: String;function NBGetAdapterAddress(a: integer):String;
var
NC:TNCB;// Netbios control block file://NetBios控制块
ADAPTE : TADAPTERSTATUS;//取得网卡状态
LANAENU : TLANAENUM;// Netbios lana
intId : Integer;// Temporary work value//临时变量
cR : Char;// Netbios return code//NetBios返回值
strTem : String;// Temporary string//临时变量begin
Result := '';// Initialize
Try
//Zero control block
ZeroMemory(@NC, SizeOf(NC));
//Issue enum command
NC.ncb_command:=Chr(NCBENUM);
cR := NetBios(@NC);
//Reissue enum command
NC.ncb_buffer := @LANAENU;
NC.ncb_length := SizeOf(LANAENU);
cR := NetBios(@NC);
If Ord(cR)<>0 Then Exit;
//Reset adapter
ZeroMemory(@NC, SizeOf(NC));
NC.ncb_command := Chr(NCBRESET);
NC.ncb_lana_num := LANAENU.lana[a];
cR := NetBios(@NC);
If Ord(cR)<>0 Then Exit;
//Get adapter address
ZeroMemory(@NC, SizeOf(NC));
NC.ncb_command := Chr(NCBASTAT);
NC.ncb_lana_num := LANAENU.lana[a];
StrPCopy(NC.ncb_callname, '*');
NC.ncb_buffer := @ADAPTE;
NC.ncb_length := SizeOf(ADAPTE);
cR := NetBios(@NC);
//Convert it to string
strTem := '';
For intId := 0 To 5 Do
strTem := strTem+ InttoHex(Integer(ADAPTE.adapter_address[intId]),2);
Result := strTem;
finally
end;
end;begin
Result:= '';
for i:=1 to 255 do//这里的数字设多少为宜
begin
s:=NBGetAdapterAddress(i);
Form1.Memo1.Lines.Add(IntToStr(i)+'-->'+s);
end;
end;ipconfig/all结果,这里能正确显示2块网卡和1块拨号的虚拟卡
Windows IP Configuration Host Name . . . . . . . . . . . . : cocowe Primary Dns Suffix . . . . . . . : Node Type . . . . . . . . . . . . : Unknown IP Routing Enabled. . . . . . . . : Yes WINS Proxy Enabled. . . . . . . . : NoEthernet adapter 本地连接 3: Connection-specific DNS Suffix . : Description . . . . . . . . . . . : Realtek RTL8139 Family PCI Fast Ethernet NIC #2 Physical Address. . . . . . . . . : 00-E0-5C-01-43-03 DHCP Enabled. . . . . . . . . . . : No IP Address. . . . . . . . . . . . : 192.168.0.1 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : Ethernet adapter 本地连接: Connection-specific DNS Suffix . : Description . . . . . . . . . . . : Realtek RTL8139 Family PCI Fast Ethernet NIC Physical Address. . . . . . . . . : 00-E0-4C-39-00-31 DHCP Enabled. . . . . . . . . . . : Yes Autoconfiguration Enabled . . . . : Yes IP Address. . . . . . . . . . . . : 139.254.21.173 Subnet Mask . . . . . . . . . . . : 255.255.254.0 Default Gateway . . . . . . . . . : DHCP Server . . . . . . . . . . . : 169.254.20.1 DNS Servers . . . . . . . . . . . : 202.101.172.35 218.108.248.249 Lease Obtained. . . . . . . . . . : 2005年1月11日 0:19:30 Lease Expires . . . . . . . . . . : 2005年1月12日 0:19:30PPP adapter hzcnc://这块是拨号用的 Connection-specific DNS Suffix . : Description . . . . . . . . . . . : WAN (PPP/SLIP) Interface Physical Address. . . . . . . . . : 00-53-45-00-00-00 DHCP Enabled. . . . . . . . . . . : No IP Address. . . . . . . . . . . . : 218.108.138.193 Subnet Mask . . . . . . . . . . . : 255.255.255.255 Default Gateway . . . . . . . . . : 218.108.168.93 DNS Servers . . . . . . . . . . . : 218.108.245.157 218.108.248.219 NetBIOS over Tcpip. . . . . . . . : Disabled
var
i: integer;
s: String;function NBGetAdapterAddress(a: integer):String;
var
NC:TNCB;// Netbios control block file://NetBios控制块
ADAPTE : TADAPTERSTATUS;//取得网卡状态
LANAENU : TLANAENUM;// Netbios lana
intId : Integer;// Temporary work value//临时变量
cR : Char;// Netbios return code//NetBios返回值
strTem : String;// Temporary string//临时变量begin
Result := '';// Initialize
Try
//Zero control block
ZeroMemory(@NC, SizeOf(NC));
//Issue enum command
NC.ncb_command:=Chr(NCBENUM);
cR := NetBios(@NC);
//Reissue enum command
NC.ncb_buffer := @LANAENU;
NC.ncb_length := SizeOf(LANAENU);
cR := NetBios(@NC);
If Ord(cR)<>0 Then Exit;
//Reset adapter
ZeroMemory(@NC, SizeOf(NC));
NC.ncb_command := Chr(NCBRESET);
NC.ncb_lana_num := LANAENU.lana[a];
cR := NetBios(@NC);
If Ord(cR)<>0 Then Exit;
//Get adapter address
ZeroMemory(@NC, SizeOf(NC));
NC.ncb_command := Chr(NCBASTAT);
NC.ncb_lana_num := LANAENU.lana[a];
StrPCopy(NC.ncb_callname, '*');
NC.ncb_buffer := @ADAPTE;
NC.ncb_length := SizeOf(ADAPTE);
cR := NetBios(@NC);
//Convert it to string
strTem := '';
For intId := 0 To 5 Do
strTem := strTem+ InttoHex(Integer(ADAPTE.adapter_address[intId]),2);
Result := strTem;
finally
end;
end;begin
Result:= '';
for i:=1 to 255 do//这里的数字设多少为宜
begin
s:=NBGetAdapterAddress(i);
Form1.Memo1.Lines.Add(IntToStr(i)+'-->'+s);
end;
end;ipconfig/all结果,这里能正确显示2块网卡和1块拨号的虚拟卡
Windows IP Configuration Host Name . . . . . . . . . . . . : cocowe Primary Dns Suffix . . . . . . . : Node Type . . . . . . . . . . . . : Unknown IP Routing Enabled. . . . . . . . : Yes WINS Proxy Enabled. . . . . . . . : NoEthernet adapter 本地连接 3: Connection-specific DNS Suffix . : Description . . . . . . . . . . . : Realtek RTL8139 Family PCI Fast Ethernet NIC #2 Physical Address. . . . . . . . . : 00-E0-5C-01-43-03 DHCP Enabled. . . . . . . . . . . : No IP Address. . . . . . . . . . . . : 192.168.0.1 Subnet Mask . . . . . . . . . . . : 255.255.255.0 Default Gateway . . . . . . . . . : Ethernet adapter 本地连接: Connection-specific DNS Suffix . : Description . . . . . . . . . . . : Realtek RTL8139 Family PCI Fast Ethernet NIC Physical Address. . . . . . . . . : 00-E0-4C-39-00-31 DHCP Enabled. . . . . . . . . . . : Yes Autoconfiguration Enabled . . . . : Yes IP Address. . . . . . . . . . . . : 139.254.21.173 Subnet Mask . . . . . . . . . . . : 255.255.254.0 Default Gateway . . . . . . . . . : DHCP Server . . . . . . . . . . . : 169.254.20.1 DNS Servers . . . . . . . . . . . : 202.101.172.35 218.108.248.249 Lease Obtained. . . . . . . . . . : 2005年1月11日 0:19:30 Lease Expires . . . . . . . . . . : 2005年1月12日 0:19:30PPP adapter hzcnc://这块是拨号用的 Connection-specific DNS Suffix . : Description . . . . . . . . . . . : WAN (PPP/SLIP) Interface Physical Address. . . . . . . . . : 00-53-45-00-00-00 DHCP Enabled. . . . . . . . . . . : No IP Address. . . . . . . . . . . . : 218.108.138.193 Subnet Mask . . . . . . . . . . . : 255.255.255.255 Default Gateway . . . . . . . . . : 218.108.168.93 DNS Servers . . . . . . . . . . . : 218.108.245.157 218.108.248.219 NetBIOS over Tcpip. . . . . . . . : Disabled
==============
//---------------------------------------------------------------------------
// EnumNetCards 枚举出网卡
//---------------------------------------------------------------------------
void __fastcall EnumNetCards(TList *NetDeviceList)
{
AnsiString DevValue;
PNetCardStruct NetCard;
DWORD Status, Problem;
LPTSTR Buffer = NULL;
DWORD BufSize = 0;
HDEVINFO hDevInfo = 0;
if (INVALID_HANDLE_VALUE == (hDevInfo =
SetupDiGetClassDevs(NULL,NULL,0,DIGCF_PRESENT|DIGCF_ALLCLASSES)))
return;
SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
HKEY hKeyClass;
char DeviceName[200];
for (DWORD DeviceId=0;
SetupDiEnumDeviceInfo(hDevInfo,DeviceId,&DeviceInfoData); DeviceId++)
{
if (CM_Get_DevNode_Status(&Status, &Problem,
DeviceInfoData.DevInst,0) != CR_SUCCESS)
continue; DevValue.SetLength(0);
if (GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_CLASS , &Buffer, (PULONG)&BufSize))
DevValue = Buffer;
if (DevValue == "Net")
{
DevValue.SetLength(0);
if (GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_ENUMERATOR_NAME , &Buffer, (PULONG)&BufSize))
DevValue = Buffer;
if (DevValue != "ROOT")
{
NetCard = new TNetCardStruct;
NetCard->Id = DeviceId;
NetCard->Name = "<Unknown Device>";
if (GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DRIVER , &Buffer, (PULONG)&BufSize))
if (GetRegistryProperty(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC , &Buffer, (PULONG)&BufSize))
NetCard->Name = Buffer;
NetCard->Disabled = (Status & DN_HAS_PROBLEM) && (CM_PROB_DISABLED == Problem);
NetCard->Changed = false;
NetDeviceList->Add(NetCard);
}
}
}
}