线程类:
unit T_GetComputer;interfaceuses
Windows, SysUtils, Classes, Winsock, Dialogs;
type
TGetMacThread = class(TThread)
private
FIP:string;
FMacAddr: string; //物理MAC地址
FHostName: string; //主机名
protected
function GetMacByIP(FIPAddr: string): string;
procedure Execute; override;
procedure SynchronizeIsMac;
function IPToName(IP: string): string;
public
constructor Create(const Para_IP: string); virtual;
end;
const
INADDR_NONE = $FFFFFFFF;implementation
uses Unit1;
{获取客户机Mac地址等信息}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';
function TGetMacThread.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;function TGetMacThread.IPToName(IP: string): string;
var
WSAData: TWSAData;
p: PHostEnt;
InetAddr: DWord;
begin
WSAStartup(2, WSAData);
InetAddr := inet_addr(PChar(IP));
try
p := GetHostByAddr(@InetAddr, Length(IP), PF_Inet);
if not (p = nil) then
result := p^.h_name
else
result := '';
except
result := '';
end;
end;constructor TGetMacThread.Create(const Para_IP: string);
begin
FIP := Para_IP;
FMacAddr := '';
FHostName := '';
inherited Create(False);
FreeOnTerminate := True;
end;procedure TGetMacThread.Execute;
begin
FMacAddr := GetMacByIP(FIP);
if FMacAddr > '' then
FHostName := IPToName(FIP);
if ((FMacAddr > '') and (FHostName > '')) then
Synchronize(SynchronizeIsMac)
end;procedure TGetMacThread.SynchronizeIsMac;
begin
Form1.Memo1.Lines.Add(FIP + ' ' + FMacAddr + ' ' + FHostName);
end;end.主窗体:unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, T_GetComputer;type
TForm1 = class(TForm)
Edit1: TEdit;
Memo1: TMemo;
Button1: TButton;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
implementation{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
// Edit1.Text := IntToStr(StrToInt(Copy('1234560000025', 7, 7)));
end;procedure TForm1.Button1Click(Sender: TObject);
begin
if Timer1.Enabled = True then
Timer1.Enabled := False
else
Timer1.Enabled := True;
end;procedure TForm1.Timer1Timer(Sender: TObject);
var
Tmp_TGetMacThread: TGetMacThread;
i: Integer;
begin
Edit1.Text := DateTimeToStr(Now);
Memo1.Clear;
for I := 1 to 256 do // Iterate
begin
Tmp_TGetMacThread := TGetMacThread.Create('192.168.0.' + IntToStr(i));
end; // for
end;end.在Timer实践中定时循环检测网段内的IP及其对应的Mac地址和主机名称!运行一段时间后会出现 "存储空间不足,无法执行命令" 的错误信息!
unit T_GetComputer;interfaceuses
Windows, SysUtils, Classes, Winsock, Dialogs;
type
TGetMacThread = class(TThread)
private
FIP:string;
FMacAddr: string; //物理MAC地址
FHostName: string; //主机名
protected
function GetMacByIP(FIPAddr: string): string;
procedure Execute; override;
procedure SynchronizeIsMac;
function IPToName(IP: string): string;
public
constructor Create(const Para_IP: string); virtual;
end;
const
INADDR_NONE = $FFFFFFFF;implementation
uses Unit1;
{获取客户机Mac地址等信息}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';
function TGetMacThread.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;function TGetMacThread.IPToName(IP: string): string;
var
WSAData: TWSAData;
p: PHostEnt;
InetAddr: DWord;
begin
WSAStartup(2, WSAData);
InetAddr := inet_addr(PChar(IP));
try
p := GetHostByAddr(@InetAddr, Length(IP), PF_Inet);
if not (p = nil) then
result := p^.h_name
else
result := '';
except
result := '';
end;
end;constructor TGetMacThread.Create(const Para_IP: string);
begin
FIP := Para_IP;
FMacAddr := '';
FHostName := '';
inherited Create(False);
FreeOnTerminate := True;
end;procedure TGetMacThread.Execute;
begin
FMacAddr := GetMacByIP(FIP);
if FMacAddr > '' then
FHostName := IPToName(FIP);
if ((FMacAddr > '') and (FHostName > '')) then
Synchronize(SynchronizeIsMac)
end;procedure TGetMacThread.SynchronizeIsMac;
begin
Form1.Memo1.Lines.Add(FIP + ' ' + FMacAddr + ' ' + FHostName);
end;end.主窗体:unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, T_GetComputer;type
TForm1 = class(TForm)
Edit1: TEdit;
Memo1: TMemo;
Button1: TButton;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
implementation{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
// Edit1.Text := IntToStr(StrToInt(Copy('1234560000025', 7, 7)));
end;procedure TForm1.Button1Click(Sender: TObject);
begin
if Timer1.Enabled = True then
Timer1.Enabled := False
else
Timer1.Enabled := True;
end;procedure TForm1.Timer1Timer(Sender: TObject);
var
Tmp_TGetMacThread: TGetMacThread;
i: Integer;
begin
Edit1.Text := DateTimeToStr(Now);
Memo1.Clear;
for I := 1 to 256 do // Iterate
begin
Tmp_TGetMacThread := TGetMacThread.Create('192.168.0.' + IntToStr(i));
end; // for
end;end.在Timer实践中定时循环检测网段内的IP及其对应的Mac地址和主机名称!运行一段时间后会出现 "存储空间不足,无法执行命令" 的错误信息!
解决方案 »
- 哪位大哥知道用程序修改DNS服务的A记录等
- 求动态设置dbgrib的columns属性的代码,
- 关于sql server的数据分发与复制程序实现的问题!!很急!!
- 请问在D5+ADO+ACCESS的程序中是否还需要BDE?如何手工打包BDE?
- 请教,安装VS.NET时的如何进行WINDOWS组件更新?
- 我是Delphi初学者,向大家请教几个问题?
- 请问在D6里如何实现断点下载,或者有控件吗?
- 在windowsXP里能否检查出几天前是否发生了红外传输的行为?
- 诚邀高手加盟:
- 为什么同样的加、解密算法在Win98上正常,在NT4.0则不正常?
- 在DELPHI中怎样编辑一个资源文件,我知道VC中是可以,但不知DELPHI怎用
- 求助:-->> 通过OLE方式创建Access数据库后,怎样在数据库里创建表呢?
var
Tmp_TGetMacThread: array of TGetMacThread;
i: Integer;
begin
Edit1.Text := DateTimeToStr(Now);
Memo1.Clear;
setlength(tmp_tgetmachthread,256);
for I := 1 to 256 do // Iterate
begin
Tmp_TGetMacThread[i] := TGetMacThread.Create('192.168.0.' + IntToStr(i));
end; // for
end;