怎样正确取得网卡的mac地址 ( 积分: 100 )

  • 主题发起人 主题发起人 781028
  • 开始时间 开始时间
7

781028

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样正确取得网卡的mac地址
 
开始-运行-cmd-ipconfig/all
 
winexec(pchar('command.com /C ipconfig /all >'+temp),sw_hide)
 
cmd下ipconfig/all
 
function GetAdapterInfo: TClientDataSet;
var
Work : PIPAdapterInfo;
Size,Res,i : Integer;
AHandle : THandle;
DllRun : function(AI:PIPAdapterInfo; var Size:integer):integer;stdcall;

Function MACToStr(ByteArr : PByte; Len : Integer) : String;
Begin
Result := '';
While (Len > 0) do Begin
Result := Result+IntToHex(ByteArr^,2);//+'-';
ByteArr := Pointer(Integer(ByteArr)+SizeOf(Byte));
Dec(Len);
End;
//SetLength(Result,Length(Result)-1); { remove last dash }
End;

Function GetIpString(Addr : PIPAddrString) : String;
Begin
Result := '';
{ While (Addr <> nil) do Begin
Result := Result+'A: '+Addr^.IPAddress+' M: '+Addr^.IPMask+#13;
Addr := Addr^.Next;
End; } //目前只取当前网卡的第一个IP
Result:=Addr^.IPAddress;
End;

Function GetMaskString(Addr : PIPAddrString) : String;
Begin
Result := '';
{While (Addr <> nil) do Begin
Result := Result+'A: '+Addr^.IPAddress+' M: '+Addr^.IPMask+#13;
Addr := Addr^.Next;
End; } //目前只取当前网卡的第一个IP的Submask
Result:=Addr^.IPMask;
End;
begin
Result:=Nil;
Size := 5120;


AHandle:=LoadLibrary('iphlpapi.dll');
if AHandle<32 then
begin
MessageBox(Application.Handle,PChar('Error in Loading iphlpapi.dll'),PChar('GetAdapterInfo Tip'),0);
Exit;
end;
DllRun := Nil;
DllRun := GetProcAddress(AHandle, 'GetAdaptersInfo');
if not Assigned(DllRun) then
begin
MessageBox(Application.Handle,PChar('Error locate GetAdaptersInfo!'),PChar('GetAdapterInfo Tip'),0);
FreeLibrary(AHandle);
Exit;
end;

GetMem(Work,Size);
Res := DllRun(Work,Size);
FreeLibrary(AHandle);

{if (Res <> ERROR_SUCCESS) Then
Begin
SetLastError(Res);
RaiseLastWin32Error;
end; }

Result:=TClientDataSet.Create(Application);
Result.FieldDefs.Add('ComboIndex',ftInteger);
Result.FieldDefs.Add('Description',ftString,25);
Result.FieldDefs.Add('MAC',ftString,15);
Result.FieldDefs.Add('IP_Addr',ftString,15);
Result.FieldDefs.Add('Mask',ftString,15);
Result.FieldDefs.Add('Gateway',ftString,15);

if not Result.Active then Result.CreateDataSet;

with Result do
begin
if (Res <> ERROR_SUCCESS) Then
Begin
//SetLastError(Res);
//RaiseLastWin32Error;
Append;
FieldByName('ComboIndex').AsInteger:=1;
FieldByName('Description').AsString:='Null NetCard';
FieldByName('MAC').AsString:='000000000000';
FieldByName('IP_Addr').AsString:='127.0.0.1';
FieldByName('Mask').AsString:='255.0.0.0';
FieldByName('Gateway').AsString:='127.0.0.1';
End
else
begin

i:=1;
Repeat
Append;
FieldByName('ComboIndex').AsInteger:=Work^.ComboIndex;
FieldByName('Description').AsString:=Work^.Description;
FieldByName('MAC').AsString:=MACToStr(@Work^.Address,Work^.AddressLength);
FieldByName('IP_Addr').AsString:=GetIpString(@Work^.IPAddressList);
FieldByName('Mask').AsString:=GetMaskString(@Work^.IPAddressList);
FieldByName('Gateway').AsString:=GetIpString(@Work^.GatewayList);
Inc(I);
Work := Work^.Next;
Until (Work = nil);

end; //else
Result.Post; //提交最后一条入DataSet中
end;
FreeMem(Work);
end;

function GetIpMAC(IpDataSet:TClientDataSet;sIp:string):string;
begin
Result:='000000000000';
if not (Assigned(IpDataSet) and Assigned(IpDataSet.FindField('MAC')) ) then Exit;
if IpDataSet.RecordCount=1 then
Result:=IpDataSet.FieldByName('MAC').AsString
else
if IpDataSet.RecordCount>1 then
if IpDataSet.Locate('MAC',sIP,[]) then
Result:=IpDataSet.FieldByName('MAC').AsString;
end;
 
接受答案了.
 
后退
顶部