delphi 根据IP获取局域网MAC


本文整理自网络,侵删。

 
unit Unit_Lan;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, RegularExpressions,ShellAPI,WinSock,StrUtils;

 function GetIPMac(IPstr:string): string;  //根据IP获取局域网MAC

implementation



function GetIPMac(IPstr:string): string;
var
hReadPipe,hWritePipe:THandle;
si:STARTUPINFO;
lsa:SECURITY_ATTRIBUTES;
pi:PROCESS_INFORMATION;
mDosScreen:String;
cchReadBuffer:DWORD;
ph:PansiChar;
fname:PChar;
i,j:integer;
getinfo:string;
///////////////////////////
str,TEMP,ip:string;
K:Integer;
match:TMatch;
///////////////////////////
begin
fname:=allocmem(255);
ph:=AllocMem(5000);
lsa.nLength :=sizeof(SECURITY_ATTRIBUTES);
lsa.lpSecurityDescriptor :=nil;
lsa.bInheritHandle :=True;
try
try
if CreatePipe(hReadPipe,hWritePipe,@lsa,0)=false then
begin
ShowMessage('Can not create pipe!');
exit;
end;
fillchar(si,sizeof(STARTUPINFO),0);
si.cb :=sizeof(STARTUPINFO);
si.dwFlags :=(STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW);
si.wShowWindow :=SW_HIDE;
si.hStdOutput :=hWritePipe;
StrPCopy(fname,'arp -a '+IPstr);

if CreateProcess( nil, fname, nil, nil, true, 0, nil, nil, si, pi) = False then
begin
ShowMessage('can not create process');
FreeMem(ph);
FreeMem(fname);
Exit;
end;

while(true) do
begin

if not PeekNamedPipe(hReadPipe,ph,1,@cchReadBuffer,nil,nil) then break;
if cchReadBuffer<>0 then
begin

if ReadFile(hReadPipe,ph^,4096,cchReadBuffer,nil)=false then break;
ph[cchReadbuffer]:=chr(0);
if ph<>'' then
begin
match:=TRegEx.Match(ph,'([A-Fa-f0-9]{2}-){5}[A-Fa-f0-9]{2}');
if match.Success then//或用一句话 if TRegEx.Match(txt, pattern).Success then 
begin
 Result :=UpperCase(match.Value);//AAA1 
end;

end;
//getinfo:=ph;
//getinfo:=StringReplace (getinfo, '接口:', '本机IP', []);
//Memo1.Lines.Add(getinfo);
end
else if(WaitForSingleObject(pi.hProcess ,0)=WAIT_OBJECT_0) then break;
Application.ProcessMessages;
Sleep(100);
end;
ph[cchReadBuffer]:=chr(0);
if ph<>'' then Result :=ph;

CloseHandle(hReadPipe);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
CloseHandle(hWritePipe);
FreeMem(ph);
FreeMem(fname);

except
end;
finally
end;

end;


end.

相关阅读 >>

Delphi xe 播放 gif 动画

Delphi获取进程pid

Delphi firemonkey限制tedit只能输入数字的完美方法

Delphi获取flash文件的影片时长,原始尺寸,帧数等信息

Delphi cxgrid:动态设计统计功能

Delphi如何自动重启程序

Delphi 先加载原内容在写入增加新内容

Delphi idhttp批量上传图片

Delphi 自带的 base64 编解码函数

Delphi常用日期函数

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...