unit uIPChanged;
interface
uses
windows, classes, SysUtils;
type
TIPChangedThread =
class(TThread)
private
fChanged: TNotifyEvent;
fRes : DWord;
procedure FSyncProc;
protected
procedure Execute;
override;
public
constructor Create(ANotifyProc: TNotifyEvent);
end;
TIPChanged =
class(TObject)
private
fEnabled : Boolean;
fOnChanged: TNotifyEvent;
fThread : TThread;
procedure FNotifyProc(Sender: TObject);
public
constructor Create;
destructor Destroy;
override;
property Enabled: Boolean
read fEnabled
write fEnabled
default True;
property OnChanged: TNotifyEvent
read fOnChanged
write fOnChanged;
end;
function NotifyAddrChange(AHandle: PCardinal; AOverlapped: POverlapped): DWORD;
stdcall;
External '
Iphlpapi.dll';
implementation
constructor TIPChanged.Create;
begin
inherited Create;
fEnabled := False;
fThread := TIPChangedThread.Create(FNotifyProc);
end;
destructor TIPChanged.Destroy;
begin
if Assigned(fThread)
then
TerminateThread(fThread.Handle, 0);
inherited Destroy;
end;
procedure TIPChanged.FNotifyProc(Sender: TObject);
begin
fThread :=
nil;
if Assigned(Sender)
then
begin
if fEnabled
and Assigned(fOnChanged)
then
fOnChanged(Self);
fThread := TIPChangedThread.Create(FNotifyProc);
end else
raise Exception.Create('
Überwachung der IP fehlgeschlagen.');
end;
constructor TIPChangedThread.Create(ANotifyProc: TNotifyEvent);
begin
fChanged := ANotifyProc;
inherited Create(True);
FreeOnTerminate := True;
Resume;
end;
procedure TIPChangedThread.Execute;
begin
fRes := NotifyAddrChange(
nil,
nil);
Synchronize(fSyncProc);
end;
procedure TIPChangedThread.FSyncProc;
begin
if fRes = NOERROR
then
fChanged(Self)
else
fChanged(
nil);
end;
end.