⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uipmacmanager.~pas

📁 定时扫描局域网络中的电脑的Mac地址情况
💻 ~PAS
字号:
unit UIPMacManager;

interface
uses Classes,SysUtils,StdCtrls,Controls,Windows, Messages, Forms;

const
  ScanResultFileName='扫描Mac地址结果';

Type


     TIPMacManager = class(Tobject)
     private
        IPMacList:TstringList;
        MutexObject:TMutex;
        FileName:string;

        OnLineIPList:TstringList;
        OnLineMutexObject:TMutex;
        OnLineFileName:string;

        NameList:TstringList;
        NameMutexObject:TMutex;
        NameFileName:string;

        procedure LoadFromFile();
        procedure SaveToFile();
        procedure OnLineLoadFromFile();
        procedure OnLineSaveToFile();
        procedure NameLoadFromFile();
        procedure NameSaveToFile();

        function Lock():boolean;
        function UnLock():boolean;
        function OnLineLock():boolean;
        function OnLineUnLock():boolean;
        function NameLock():boolean;
        function NameUnLock():boolean;

     public
        constructor  Create(FName:string);
        destructor Destroy;override;
        Function  FindByIP(IP:string):String;
        Procedure AddMac(IP:string;Mac:string);
        function GetIPMacList:string;

        Function IsOnLine(IP:string):boolean;
        Procedure OnLineModify(IP:string;IsOnLine:boolean);
        procedure NameModify(Mac:string;IP:string;Name:string);

     end;
     procedure Delay(aTimeOut:longint);
VAR
     IPMacManager:TIPMacManager;

implementation


constructor  TIPMacManager.Create(FName:string);
begin
   FileName      :=FName+'.Txt';
   OnLineFileName:=FName+'_OnLine.Txt';
   NameFileName  :=FName+'_Name.Txt';

   IPMacList:=TstringList.Create();
   MutexObject:=TMutex.Create(self.ClassName);
   OnLineIPList:=TstringList.Create();
   OnLineMutexObject:=TMutex.Create(self.ClassName+'_OnLine');
   NameList:=TstringList.Create();
   NameMutexObject:=TMutex.Create(self.ClassName+'_Name');

   LoadFromFile();
   OnLineLoadFromFile();
   NameLoadFromFile();
   OnLineIPList.Sorted:=true;
   IPMacList.Sorted:=true;
   NameList.Sorted:=true;

end;
destructor TIPMacManager.Destroy;
begin
   IPMacList.Free;
   OnLineIPList.Free;
   MutexObject.Free;
   OnLineMutexObject.Free;
end;

Function  TIPMacManager.FindByIP(IP:string):String;
begin
   if (Lock) then
    try
       try
       result:=IPMacList.Values[IP];
       except
         On Exception do ;
       end;
    finally
       UnLock;
    end;
end;
Procedure TIPMacManager.AddMac(IP:string;Mac:string);
var Index:integer;
    s:string;
begin


  if(IP='') or (Mac='') then exit;
  if (Lock) then
  try
     try
        Index:=IPMacList.IndexOfName(IP);
        s:=IPMacList.Values[IP];
        if(s='') then
           s:=Mac
        else
           s:=s+','+Mac;
        if((Index>=0) and (Index<IPMacList.Count)) then
           IPMacList.Values[IP]:=s
        else
           IPMacList.Add(IP+'='+s);
        SaveToFile();
      except
        On Exception do ;
      end;
  finally
     UnLock;
  end;


end;




Function TIPMacManager.IsOnLine(IP:string):boolean;
begin
   if (OnLineLock) then
    try
       try
         result:=OnLineIPList.Indexof(IP)>=0;
       except
         On Exception do ;
       end;
    finally
       OnLineUnLock;
    end;

end;
Procedure TIPMacManager.OnLineModify(IP:string;IsOnLine:boolean);
var Index:integer;
begin
   if (OnLineLock) then
    try
       try
         if(IsOnLine) then
         begin
           Index:=OnLineIPList.IndexOf(IP);
           if(IsOnLine) and (Index=-1) then
           begin
             OnLineIPList.Add(IP);
             OnLineSaveToFile();
           end
           else
           if(not IsOnLine) and (Index>-1) then
           begin
             OnLineIPList.Delete(Index);
             OnLineSaveToFile();
           end;
         end;

       except
         On Exception do ;
       end;
    finally
       OnLineUnLock;
    end;

end;

Procedure TIPMacManager.NameModify(Mac:string;IP:string;Name:string);
var Index:integer;
    s:string;
begin

   if (Mac='') or (IP='') or (Name='') then exit;
   if (NameLock) then
    try
       try
        Index:=NameList.IndexOfName(Mac);
        s:=NameList.Values[Mac];
        if(s='') then
           s:=IP+Name
        else
        begin
           if(pos(IP+Name,s)=-1) then
              s:=s+','+IP+Name
           else
              s:='';
        end;
        if(s<>'')then
        begin
          if((Index>=0) and (Index<NameList.Count)) then
             NameList.Values[Mac]:=s
          else
             NameList.Add(Mac+'='+s);
          NameSaveToFile();
        end;

       except
         On Exception do ;
       end;
    finally
       NameUnLock;
    end;

end;

procedure TIPMacManager.LoadFromFile();
begin
  if(not FileExists(FileName)) then
     exit;
  try
     IPMacList.LoadFromFile(FileName);
  except
     On Exception do ;
  end;
end;
procedure TIPMacManager.SaveToFile();
begin
  try
     IPMacList.Sorted:=true;
     IPMacList.SaveToFile(FileName);
     //IPMacList.Sorted:=false;
  except
     On Exception do ;
  end;
end;
procedure TIPMacManager.OnLineLoadFromFile();
begin
  if(not FileExists(OnLineFileName)) then
     exit;
  try
     OnLineIPList.LoadFromFile(OnLineFileName);
  except
     On Exception do ;
  end;
end;

procedure TIPMacManager.OnLineSaveToFile();
begin
  try
     OnLineIPList.Sorted:=true;
     OnLineIPList.SaveToFile(OnLineFileName);
     //OnLineIPList.Sorted:=false;
  except
     On Exception do ;
  end;
end;
procedure TIPMacManager.NameLoadFromFile();
begin
  if(not FileExists(NameFileName)) then
     exit;
  try
     NameList.LoadFromFile(NameFileName);
  except
     On Exception do ;
  end;
end;

procedure TIPMacManager.NameSaveToFile();
begin
  try
     NameList.Sorted:=true;
     NameList.SaveToFile(NameFileName);
     //NameList.Sorted:=false;
  except
     On Exception do ;
  end;
end;

function TIPMacManager.Lock():boolean;
begin
  result :=self.MutexObject.Get(3000);
end;
function TIPMacManager.UnLock():boolean;
begin
  result :=self.MutexObject.Release;
end;

function TIPMacManager.OnLineLock():boolean;
begin
  try
  result :=self.OnLineMutexObject.Get(1000);
  except
     ;
  end;
end;
function TIPMacManager.OnLineUnLock():boolean;
begin
  try
  result :=self.OnLineMutexObject.Release;
  except
    ;
  end;
end;

function TIPMacManager.NameLock():boolean;
begin
  try
  result :=self.NameMutexObject.Get(1000);
  except
    ;
  end;
end;
function TIPMacManager.NameUnLock():boolean;
begin
  try
  result :=self.NameMutexObject.Release;
  except
    ;
  end;
end;

function TIPMacManager.GetIPMacList: string;
begin
if (Lock) then
  try
     try
        result:=IPMacList.Text;
      except
        On Exception do ;
      end;
  finally
     UnLock;
  end;
end;





end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -