根据计算机名或IP,得到局域网内任一电脑网卡MAC(200分)

  • 主题发起人 主题发起人 勇者
  • 开始时间 开始时间

勇者

Unregistered / Unconfirmed
GUEST, unregistred user!
根据计算机名或IP,得到局域网内任一电脑网卡MAC
 
需要使用网络控件吧 。
 
//这是计算机名查MAC的.
function TForm1.GetMacAddress(const AServerName : string) : string;
type
TNetTransportEnum = function(pszServer : PWideChar;
Level : DWORD;
var pbBuffer : pointer;
PrefMaxLen : LongInt;
var EntriesRead : DWORD;
var TotalEntries : DWORD;
var ResumeHandle : 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;

var
E,ResumeHandle,
EntriesRead,
TotalEntries : DWORD;
FLibHandle : THandle;
sMachineName,
sMacAddr,
Retvar : string;
pBuffer : pointer;
pInfo : PTransportInfo;
FNetTransportEnum : TNetTransportEnum;
FNetApiBufferFree : TNetApiBufferFree;
pszServer : array[0..128] of WideChar;
i,ii,iIdx : integer;
begin
sMachineName := trim(AServerName);
Retvar := '00-00-00-00-00-00';

// Add leading // if missing
if (sMachineName <> '') and (length(sMachineName) >= 2) then
begin
if copy(sMachineName,1,2) <> '//' then
sMachineName := '//' + sMachineName
end;

// Setup and load from DLL
pBuffer := nil;
ResumeHandle := 0;
FLibHandle := LoadLibrary('NETAPI32.DLL');

// Execute the external function
if FLibHandle <> 0 then
begin
@FNetTransportEnum := GetProcAddress(FLibHandle,'NetWkstaTransportEnum');
@FNetApiBufferFree := GetProcAddress(FLibHandle,'NetApiBufferFree');
E := FNetTransportEnum(StringToWideChar(sMachineName,pszServer,129),0,
pBuffer,-1,EntriesRead,TotalEntries,Resumehandle);

if E = 0 then
begin
pInfo := pBuffer;

// Enumerate all protocols - look for TCPIP
for i := 1 to EntriesRead do
begin
if pos('TCPIP',UpperCase(pInfo^.transport_name)) <> 0 then
begin
// Got It - now format result 'xx-xx-xx-xx-xx-xx'
iIdx := 1;
sMacAddr := pInfo^.transport_address;

for ii := 1 to 12 do
begin
Retvar[iIdx] := sMacAddr[ii];
inc(iIdx);
if iIdx in [3,6,9,12,15] then inc(iIdx);
end;
end;

inc(pInfo);
end;
if pBuffer <> nil then FNetApiBufferFree(pBuffer);
end;

try
FreeLibrary(FLibHandle);
except
// 错误处理
end;
end;
result:=Retvar;
end;
 
上面的只能2k/xp下使用,98不幸,要用NB空间可以得到所有OS的Mac
 
to:菜鸟黄,
本机的没有问题,其它的显示'00-00-00-00-00-00';
 
先ping
再arp
搞定
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3905480
 
网络上应该有很多答案了
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部