真的是大把代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, inifiles,Graphics, Controls, Forms,
winsock,dialogs, StdCtrls, WinSkinForm, WinSkinData,shellapi;
type
Tmain = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
SkinData1: TSkinData;
WinSkinForm1: TWinSkinForm;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
procedure FormCreate(Sender: TObject);
procedure Label10Click(Sender: TObject);
private
procedure readini;
public
{ Public declarations }
end;
var
main: Tmain;
gatemac:string;
implementation
{$R *.dfm}
function SendARP(const DestIP: DWord;const SrcIP: DWord;const pMacAddr: Pointer;const PhyAddrLen: PULONG): DWord;
stdcall;
external 'IPHLPAPI.DLL' name 'SendARP';
function inet_addr(const cp: PChar): DWord;
stdcall;
external 'WS2_32.DLL' name 'inet_addr';
function LocalIP : string;
type
TaPInAddr = array [0..10] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..63] of char;
I : Integer;
GInitData : TWSADATA;
begin
WSAStartup($101, GInitData);
Result := '127.0.0.1';
GetHostName(Buffer, SizeOf(Buffer));
phe :=GetHostByName(buffer);
if phe = nil then
begin
wsacleanup;
Exit;
end;
pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^ <> nildo
begin
result:=StrPas(inet_ntoa(pptr^^));
Inc(I);
end;
WSACleanup;
end;
function IPMACAD(TMp:STRING):string;
var
dwResult: DWord;
ulIPAddr: DWord;
ulMACAddr: array[0..5] of Byte;
ulAddrLen: ULONG;
begin
ulIPAddr := inet_addr(PChar(TMp));
result:= IntToHex(ulIPAddr, 8);
//ShowMessage(IntToHex(ulIPAddr, 8));
if ulIPAddr = $FFFFFFFF then
begin
result:='';
Exit;
end;
ulAddrLen := 6;
dwResult := SendARP(ulIPAddr, 0, @ulMACAddr, @ulAddrLen);
if dwResult <> 0 then
begin
result:='';
Exit;
end;
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);
end;
function WinExecAndWait32(FileName:String;
Visibility:integer):integer;
var
zAppName: array[0..512] of char;
zCurDir: array[0..255] of char;
WorkDir: String;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
lpExitCode: longword;
begin
StrPCopy(zAppName,FileName);
GetDir(0,WorkDir);
StrPCopy(zCurDir,WorkDir);
FillChar(StartupInfo,Sizeof(StartupInfo),#0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := Visibility;
if not CreateProcess(nil,zAppName,{ pointer to command line string }
nil, { pointer to process security attributes }
nil, { pointer to thread security attributes }
false, { handle inheritance flag }
CREATE_NEW_CONSOLE or { creation flags }
NORMAL_PRIORITY_CLASS,
nil, { pointer to new environment block }
nil, { pointer to current directory name }
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) then
Result:=-1 { pointer to PROCESS_INF }
else
begin
WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,lpExitCode);
end;
end;
procedure tmain.readini;
var
f:tinifile;
begin
try
f:=TIniFile.Create(ExtractFilePath(ParamStr(0))+'Mac_Ip.ini');
label8.Caption:=f.Readstring('GateWay','IP','');
gatemac:=f.Readstring('GateWay','MAC','');
except
//
end;
f.Free;
end;
procedure Tmain.FormCreate(Sender: TObject);
begin
readini;
if LocalIP='127.0.0.1' then
label2.Caption:='网络未连接'
else
begin
label2.Caption:=LocalIP;
label4.Caption:=IPMACAD(localip);
WinExecAndWait32('arp -s '+label2.Caption+' '+label4.Caption,0);
if label8.Caption<>'' then
WinExecAndWait32('arp -s '+label8.Caption+' '+gatemac,0);
label6.Caption:='是';
end;
end;
procedure Tmain.Label10Click(Sender: TObject);
begin
shellexecute(handle,nil,pchar('http://www.xnzy.net/'),nil,nil,sw_shownormal);
end;
end.