DELPHI获取网卡MAC地址.docx
- 文档编号:3306746
- 上传时间:2022-11-21
- 格式:DOCX
- 页数:8
- 大小:17.69KB
DELPHI获取网卡MAC地址.docx
《DELPHI获取网卡MAC地址.docx》由会员分享,可在线阅读,更多相关《DELPHI获取网卡MAC地址.docx(8页珍藏版)》请在冰豆网上搜索。
DELPHI获取网卡MAC地址
DELPHI获取网卡MAC地址
1、通过IP取MAC地址
uses
WinSock;
Functionsendarp(ipaddr:
ulong;
temp:
dword;
ulmacaddr:
pointer;
ulmacaddrleng:
pointer):
DWord;StdCall;External'Iphlpapi.dll'Name'SendARP';
procedureTForm1.Button1Click(Sender:
TObject);
var
myip:
ulong;
mymac:
array[0..5]ofbyte;
mymaclength:
ulong;
r:
integer;
begin
myip:
=inet_addr(PChar('192.168.6.180'));
mymaclength:
=length(mymac);
r:
=sendarp(myip,0,@mymac,@mymaclength);
label1.caption:
='errorcode:
'+inttostr(r);
label2.caption:
=format('%2.2x:
%2.2x:
%2.2x:
%2.2x:
%2.2x:
%2.2x',[mymac[0],mymac[1],mymac[2],mymac[3],mymac[4],mymac[5]]);
end;
2、取MAC地址(含多网卡),最好的方法,支持Vista,Win7
unitUnit1;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,
WinSock,StdCtrls;
Const
MAX_HOSTNAME_LEN=128;{fromIPTYPES.H}
MAX_DOMAIN_NAME_LEN=128;
MAX_SCOPE_ID_LEN=256;
MAX_ADAPTER_NAME_LENGTH=256;
MAX_ADAPTER_DESCRIPTION_LENGTH=128;
MAX_ADAPTER_ADDRESS_LENGTH=8;
Type
TIPAddressString=Array[0..4*4-1]ofChar;
PIPAddrString=^TIPAddrString;
TIPAddrString=Record
Next:
PIPAddrString;
IPAddress:
TIPAddressString;
IPMask:
TIPAddressString;
Context:
Integer;
End;
PFixedInfo=^TFixedInfo;
TFixedInfo=Record{FIXED_INFO}
HostName:
Array[0..MAX_HOSTNAME_LEN+3]ofChar;
DomainName:
Array[0..MAX_DOMAIN_NAME_LEN+3]ofChar;
CurrentDNSServer:
PIPAddrString;
DNSServerList:
TIPAddrString;
NodeType:
Integer;
ScopeId:
Array[0..MAX_SCOPE_ID_LEN+3]ofChar;
EnableRouting:
Integer;
EnableProxy:
Integer;
EnableDNS:
Integer;
End;
PIPAdapterInfo=^TIPAdapterInfo;
TIPAdapterInfo=Record{IP_ADAPTER_INFO}
Next:
PIPAdapterInfo;
ComboIndex:
Integer;
AdapterName:
Array[0..MAX_ADAPTER_NAME_LENGTH+3]ofChar;
Description:
Array[0..MAX_ADAPTER_DESCRIPTION_LENGTH+3]ofChar;
AddressLength:
Integer;
Address:
Array[1..MAX_ADAPTER_ADDRESS_LENGTH]ofByte;
Index:
Integer;
_Type:
Integer;
DHCPEnabled:
Integer;
CurrentIPAddress:
PIPAddrString;
IPAddressList:
TIPAddrString;
GatewayList:
TIPAddrString;
DHCPServer:
TIPAddrString;
HaveWINS:
Bool;
PrimaryWINSServer:
TIPAddrString;
SecondaryWINSServer:
TIPAddrString;
LeaseObtained:
Integer;
LeaseExpires:
Integer;
End;
type
TForm1=class(TForm)
Button1:
TButton;
Memo1:
TMemo;
procedureButton1Click(Sender:
TObject);
private
{Privatedeclarations}
procedureGetAdapterInformation;
public
{Publicdeclarations}
end;
var
Form1:
TForm1;
Functionsendarp(ipaddr:
ulong;
temp:
dword;
ulmacaddr:
pointer;
ulmacaddrleng:
pointer):
DWord;StdCall;
implementation
{$R*.dfm}
Functionsendarp;External'Iphlpapi.dll'Name'SendARP';
FunctionGetAdaptersInfo(AI:
PIPAdapterInfo;VarBufLen:
Integer):
Integer;
StdCall;External'iphlpapi.dll'Name'GetAdaptersInfo';
procedureTForm1.GetAdapterInformation;
Var
AI,Work:
PIPAdapterInfo;
Size:
Integer;
Res:
Integer;
I:
Integer;
FunctionMACToStr(ByteArr:
PByte;Len:
Integer):
String;
Begin
Result:
='';
While(Len>0)doBegin
Result:
=Result+IntToHex(ByteArr^,2)+'-';
ByteArr:
=Pointer(Integer(ByteArr)+SizeOf(Byte));
Dec(Len);
End;
SetLength(Result,Length(Result)-1);{removelastdash}
End;
FunctionGetAddrString(Addr:
PIPAddrString):
String;
Begin
Result:
='';
While(Addr<>nil)doBegin
Result:
=Result+'A:
'+Addr^.IPAddress+'M:
'+Addr^.IPMask+#13;
Addr:
=Addr^.Next;
End;
End;
FunctionTimeTToDateTimeStr(TimeT:
Integer):
String;
ConstUnixDateDelta=25569;{daysbetween12/31/1899and1/1/1970}
Var
DT:
TDateTime;
TZ:
TTimeZoneInformation;
Res:
DWord;
Begin
If(TimeT=0)ThenResult:
=''
ElseBegin
{UnixTIME_Tissecssince1/1/1970}
DT:
=UnixDateDelta+(TimeT/(24*60*60));{inUTC}
{calculatebias}
Res:
=GetTimeZoneInformation(TZ);
If(Res=TIME_ZONE_ID_INVALID)ThenRaiseLastWin32Error;
If(Res=TIME_ZONE_ID_STANDARD)ThenBegin
DT:
=DT-((TZ.Bias+TZ.StandardBias)/(24*60));
Result:
=DateTimeToStr(DT)+''+WideCharToString(TZ.StandardName);
End
ElseBegin{daylightsavingtime}
DT:
=DT-((TZ.Bias+TZ.DaylightBias)/(24*60));
Result:
=DateTimeToStr(DT)+''+WideCharToString(TZ.DaylightName);
End;
End;
End;
begin
Memo1.Lines.Clear;
Size:
=5120;
GetMem(AI,Size);
Res:
=GetAdaptersInfo(AI,Size);
If(Res<>ERROR_SUCCESS)ThenBegin
SetLastError(Res);
RaiseLastWin32Error;
End;
WithMemo1,LinesdoBegin
Work:
=AI;
I:
=1;
Repeat
Add('');
Add('Adapter'+IntToStr(I));
Add('ComboIndex:
'+IntToStr(Work^.ComboIndex));
Add('Adaptername:
'+Work^.AdapterName);
Add('Description:
'+Work^.Description);
Add('Adapteraddress:
'+MACToStr(@Work^.Address,Work^.AddressLength));
Add('Index:
'+IntToStr(Work^.Index));
Add('Type:
'+IntToStr(Work^._Type));
Add('DHCP:
'+IntToStr(Work^.DHCPEnabled));
Add('CurrentIP:
'+GetAddrString(Work^.CurrentIPAddress));
Add('IPaddresses:
'+GetAddrString(@Work^.IPAddressList));
Add('Gateways:
'+GetAddrString(@Work^.GatewayList));
Add('DHCPservers:
'+GetAddrString(@Work^.DHCPServer));
Add('HasWINS:
'+IntToStr(Integer(Work^.HaveWINS)));
Add('PrimaryWINS:
'+GetAddrString(@Work^.PrimaryWINSServer));
Add('SecondaryWINS:
'+GetAddrString(@Work^.SecondaryWINSServer));
Add('Leaseobtained:
'+TimeTToDateTimeStr(Work^.LeaseObtained));
Add('Leaseexpires:
'+TimeTToDateTimeStr(Work^.LeaseExpires));
Inc(I);
Work:
=Work^.Next;
Until(Work=nil);
End;
FreeMem(AI);
end;
procedureTForm1.Button1Click(Sender:
TObject);
begin
GetAdapterInformation;
end;
end.
方法3我没试成功
usesnb30;
functionNBGetAdapterAddress(a:
Integer):
string;
var
NCB:
TNCB;//Netbioscontrolblock//NetBios控制块
ADAPTER:
TADAPTERSTATUS;//Netbiosadapterstatus//取网卡状态
LANAENUM:
TLANAENUM;//Netbioslana
intIdx:
Integer;//Temporaryworkvalue//临时变量
cRC:
Char;//Netbiosreturncode//NetBios返回值
strTemp:
string;//Temporarystring//临时变量
begin
Result:
='';
try
ZeroMemory(@NCB,SizeOf(NCB));//Zerocontrolblocl
NCB.ncb_command:
=Chr(NCBENUM);//Issueenumcommand
cRC:
=NetBios(@NCB);
NCB.ncb_buffer:
=@LANAENUM;//Reissueenumcommand
NCB.ncb_length:
=SizeOf(LANAENUM);
cRC:
=NetBios(@NCB);
ifOrd(cRC)<>0then
exit;
ZeroMemory(@NCB,SizeOf(NCB));//Resetadapter
NCB.ncb_command:
=Chr(NCBRESET);
NCB.ncb_lana_num:
=LANAENUM.lana[a];
cRC:
=NetBios(@NCB);
ifOrd(cRC)<>0then
exit;
ZeroMemory(@NCB,SizeOf(NCB));//Getadapteraddress
NCB.ncb_command:
=Chr(NCBASTAT);
NCB.ncb_lana_num:
=LANAENUM.lana[a];
StrPCopy(NCB.ncb_callname,'*');
NCB.ncb_buffer:
=@ADAPTER;
NCB.ncb_length:
=SizeOf(ADAPTER);
cRC:
=NetBios(@NCB);
strTemp:
='';//Convertittostring
forintIdx:
=0to5do
strTemp:
=strTemp+InttoHex(Integer(ADAPTER.adapter_address[intIdx]),2);
Result:
=strTemp;
finally
end;
end;
方法4:
当前活动的网卡地址(Vista下可能取不到)
//======================================================================
//返回值是主机AServerName的MAC地址
//AServerName参数的格式为\\或者ServerName
//参数ServerName为空时返回本机的MAC地址
//MAC地址以XX-XX-XX-XX-XX-XX的格式返回
//======================================================================
functionGetMacAddress2(constAServerName:
string):
string;
type
TNetTransportEnum=function(pszServer:
PWideChar;
Level:
DWORD;
varpbBuffer:
pointer;
PrefMaxLen:
LongInt;
varEntriesRead:
DWORD;
varTotalEntries:
DWORD;
varResumeHandle:
DWORD):
DWORD;stdcall;
TNetApiBufferFree=function(Buffer:
pointer):
DWORD;stdcall;
PTransportInfo=^TTransportInfo;
TTransportInfo=record
quality_of_service:
DWORD;
number_of_vcs:
DWORD;
transport_name:
PWChar;
transport_address:
PWChar;
wan_ish:
boolean;
end;
varE,ResumeHandle,
EntriesRead,
TotalEntries:
DWORD;
FLibHandle:
THandle;
sMachineName,
sMacAddr,
Retvar:
string;
pBuffer:
pointer;
pInfo:
PTransportInfo;
FNetTransportEnum:
TNetTransportEnum;
FNetApiBufferFree:
TNetApiBufferFree;
pszServer:
array[0..128]ofWideChar;
i,ii,iIdx:
integer;
begin
sMachineName:
=trim(AServerName);
Retvar:
='00-00-00-00-00-00';
//Addleading\\ifmissing
if(sMachineName<>'')and(length(sMachineName)>=2)thenbegin
ifcopy(sMachineName,1,2)<>'\\'then
sMachineName:
='\\'+sMachineName
end;
//SetupandloadfromDLL
pBuffer:
=nil;
ResumeHandle:
=0;
FLibHandle:
=LoadLibrary('NETAPI32.DLL');
//Executetheexternalfunction
ifFLibHandle<>0thenbegin
@FNetTransportEnum:
=GetProcAddress(FLibHandle,'NetWkstaTransportEnum');
@FNetApiBufferFree:
=GetProcAddress(FLibHandle,'NetApiBufferFree');
E:
=FNetTransportEnum(StringToWideChar(sMachineName,pszServer,129),0,
pBuffer,-1,EntriesRead,TotalEntries,Resumehandle);
ifE=0thenbegin
pInfo:
=pBuffer;
//Enumerateallprotocols–lookforTCPIP
fori:
=1toEntriesReaddobegin
ifpos('TCPIP',UpperCase(pInfo^.transport_name))<>0thenbegin
//GotIt–nowformatresultxx-xx-xx-xx-xx-xx
iIdx:
=1;
sMacAddr:
=pInfo^.transport_address;
forii:
=1to12dobegin
Retvar[iIdx]:
=sMacAddr[ii];
inc(iIdx);
ifiIdxin[3,6,9,12,15]theninc(iIdx);
end;
end;
inc(pInfo);
end;
ifpBuffer<>nilthenFNetApiBufferFree(pBuffer);
end;
try
FreeLibrary(FLibHandle);
ex
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- DELPHI 获取 网卡 MAC 地址