简单:
1、win98下通过GUID
function TForm1.SysGetNicAddress:string;
var
Tmp:TGUID;
tmpstr,newstr:string;
cnt:Integer;
begin
try
CoCreateGuid(Tmp);
tmpstr:=GUIDToString(Tmp);
tmpstr:=Copy(tmpstr,Length(tmpstr)-12,12);
for cnt:=1 to 5 do
newstr :=newstr+Copy(tmpstr,cnt*2 -1 ,2)+'-' ;
newstr :=newstr+Copy(tmpstr,11,2);
except
newstr:='';
end;
Result :=newstr;
end;
2、win2000、XP下通过UDP协议
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, winsock,
StdCtrls;
const
WM_SOCK = WM_USER + 1; //自定义windows消息
UDPPORT = 6767; //设定UDP端口号
NBTPORT = 137;
...
procedure TForm1.FormCreate(Sender: TObject);
var
TempWSAData: TWSAData;
//optval: integer;
begin
// 初始化SOCKET
if WSAStartup($101, TempWSAData)=1 then
showmessage('StartUp Error!');
s := Socket(AF_INET, SOCK_DGRAM, 0);
if (s = INVALID_SOCKET) then //Socket创建失败
begin
showmessage(inttostr(WSAGetLastError())+' Socket创建失败');
CloseSocket(s);
end;
//本机SockAddr绑定
addr.sin_family := AF_INET;
addr.sin_addr.S_addr := INADDR_ANY;
addr.sin_port := htons(UDPPORT);
if Bind(s, addr, sizeof(addr)) <> 0 then
begin
showmessage('bind fail');
end;
WSAAsyncSelect(s, Form1.Handle , WM_SOCK, FD_READ);
//对方SockAddrIn设定
FSockAddrIn.SIn_Family := AF_INET;
FSockAddrIn.SIn_Port := htons(NBTPORT);
end;
procedure TForm1.GetInfo(buffer: Array of byte;len:integer);
var
str:string;
i,j,pos,name_num: integer;
begin
name_num:=0;
for i:=1 to len do
begin
if((buffer=$21)and(buffer[i+1]=$00)and(buffer[i+2]=$01))
then
begin
name_num:=buffer[i+9];
break;
end;
end;
if name_num=0 then exit;
pos:=i+10;
str:='';
{
for i:=pos to (pos+18*name_num-1) do
begin
if (((i-pos)mod 18) =0) then
begin
for j:=0 to 14 do
begin
if trim(char(buffer[i+j]))='' then buffer[i+j]:=ord(' ');
str:=str+char(buffer[i+j]);
end;
if (buffer[i+16] and $80)=$80 then
begin
str:=str+format('<%x>',[buffer[i+15]]);
str:=str+'<GROUP>';
ListBox1.Items.Add(str);
end
else
begin
str:=str+format('<%x>',[buffer[i+15]]);
str:=str+'<UNIQUE>';
ListBox1.Items.Add(str);
end;
str:='';
end;
end;
}
for i:=0 to 5 do
begin
str:=str+format('%.2x.',[buffer[i+pos+18*name_num]]);
end;
delete(str,length(str),1);
str:='MAC:'+str;
ListBox1.Items.Add(str);
ListBox1.Items.Add('------------------------------------------------------');
ListBox1.TopIndex :=ListBox1.Items.count-1;
end;
procedure TForm1.ReadData(var Message: TMessage);
var
buffer: Array [1..500] of byte;
len{,i}: integer;
flen: integer;
Event: word;
value: string;
begin
value:='';
flen:=sizeof(FSockAddrIn);
FSockAddrIn.SIn_Port := htons(NBTPORT);
Event := WSAGetSelectEvent(Message.LParam);
if Event = FD_READ then
begin
len := recvfrom(s, buffer, sizeof(buffer), 0, FSockAddrIn, flen);
{for i:=1 to len do value:=value+format('%x',[buffer]);
ListBox1.items.add(value);
value:='';
for i:=1 to len do if char(buffer)<>#0 then value:=value+char(buffer);
ListBox1.items.add(value);}
if len<> 0 then GetInfo(buffer,len);
end;
end;
procedure TForm1.SendData(b:array of byte);
var
len: integer;
begin
FSockAddrIn.SIn_Addr.S_addr := inet_addr(pchar(edit1.text));
len := sendto(s, b[0],50, 0, FSockAddrIn, sizeof(FSockAddrIn));
//if (WSAGetLastError() <> WSAEWOULDBLOCK) and (WSAGetLastError() <> 0) then showmessage(inttostr(WSAGetLastError()));
if len = SOCKET_ERROR then
showmessage('send fail');
if len <> 50 then
showmessage('Not Send all');
end;
3、win2000,XP还可以采用ARP
function inet_addr(const cp: PChar): DWord; stdcall; external 'WS2_32.DLL' name 'inet_addr';
function SendARP(const DestIP: DWord;
const SrcIP: DWord;
const pMacAddr: Pointer;
const PhyAddrLen: PULONG): DWord; stdcall; external 'IPHLPAPI.dll' name 'SendARP';
{$R *.dfm}
function TForm1.GetMacByIP(FIPAddr: string): string;
var
dwResult: DWord;
ulIPAddr: DWord;
ulMacAddr: array[0..5] of Byte;
ulAddrLen: ULONG;
begin
ulIPAddr := INet_Addr(PChar(FIPAddr));
if ulIPAddr = INADDR_NONE then
exit;
ulAddrLen := 6;
dwResult := SendARP(ulIPAddr, 0, @ulMacAddr, @ulAddrLen);
if dwResult = 0 then
result := (IntToHex(ulMacAddr[0], 2) + ':' +
IntToHex(ulMacAddr[1], 2) + ':' +
IntToHex(ulMacAddr[2], 2) + ':' +
IntToHex(ulMacAddr[3], 2) + ':' +
IntToHex(ulMacAddr[4], 2) + ':' +
IntToHex(ulMacAddr[5], 2))
else
result := '';
end;
不要用什么TNCB,这个需要引用nb30单元,这就要求系统必须安装netbios,这个已经很古老了,很多系统都不支持!!!!!