秋·风

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  278 随笔 :: 0 文章 :: 308 评论 :: 20万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
lazarus 获取硬件ID的函数,适用于linux和win,只需调用GetHWID就可以返回相应的ID。
复制代码
unit HWTools;

{$mode ObjFPC}{$H+}

interface

uses
  Classes, SysUtils,process;

function GetHWID:String;

implementation

function GetHWID:String;
var s:string;
    function GetInfo(cmd,s1,s2:string):string;
    var Output:String;
        i,j:integer;
        str:string;
        readdmi:TStringList;
    begin
      result:='';
      readdmi:=TStringList.Create;
      {$ifdef linux}
      RunCommand(cmd ,Output);
      readdmi.Text:=Output;
      j:=0;
      str:='';
      for i:=1 to readdmi.Count-1 do
      begin
        if pos(s1, readdmi[i])>0 then j:=1; //CPU Info
        if (pos(s2, readdmi[i])>0) and (j=1) then
        begin
          str:=trim(copy(readdmi[i],pos(s2, readdmi[i])+Length(s2),Length(readdmi[i])));
          str:=StringReplace(str,' ','',[rfReplaceAll]);
          str:=StringReplace(str,':','',[rfReplaceAll]);
          str:=StringReplace(str,'-','',[rfReplaceAll]);
          Break;
        end;
      end;
      {$endif}
      {$ifdef windows}
      RunCommand(cmd ,Output);
      readdmi.Text:=Output;
      j:=0;
      for i:=1 to readdmi.Count-1 do
      begin
        Output:=readdmi.Strings[i];
        if Length(Output)>17 Then
        begin
          Output:=Copy(Output,length(output)-16,17);
          if (output[3]='-') and (output[6]='-') and (output[9]='-') and
            (output[12]='-') and (output[15]='-') and (j=0) Then
          begin
            j:=1;
            str:=Output;
            str:=StringReplace(str,' ','',[rfReplaceAll]);
            str:=StringReplace(str,':','',[rfReplaceAll]);
            str:=StringReplace(str,'-','',[rfReplaceAll]);
            break;
        end;
        end;
      end;
      {$endif}
      readdmi.Free;
      result:=trim(str).ToUpper;
    end;
    function GetCPUID:string;
    var str:String;
        i,j:integer;
        o:TStringList;
    begin
      Result:='';
      o:=TStringList.Create;
      o.LoadFromFile('/proc/cpuinfo');
      j:=0;
      str:='';
      for i:=1 to o.Count-1 do
      begin
        if (pos('Serial', o[i])>0) and (j=0) then
        begin
          str:=trim(copy(o[i],pos('Serial', o[i])+6,Length(o[i])));
          str:=StringReplace(str,' ','',[rfReplaceAll]);
          str:=StringReplace(str,':','',[rfReplaceAll]);
          Break;
        end;
      end;
      o.Free;
      result:=str.ToUpper;
    end;
begin
  result:='';
  {$ifdef linux}
  s:=GetInfo('dmidecode','DMI type 4,','ID:');//CPU Info
  if s='' then
    s:=GetInfo('dmidecode','DMI type 2,','Serial Number:');//主板Info
  if s='' then
    s:=GetCPUID;
  if s='' then
    s:=copy(GetInfo('ifconfig','硬件地址','硬件地址'),1,12);//网卡MAC
  if s='' then
    s:=copy(GetInfo('ifconfig','ether','ether'),1,12);//网卡MAC
  if s='' then
    s:=copy(GetInfo('ifconfig','HWaddr','HWaddr'),1,12);//网卡MAC
  {$endif}
  {$ifdef windows}
  s:=copy(GetInfo('ipconfig /all','',''),1,12);//网卡MAC
  {$endif}
  result:=s;
end;

end.
复制代码

 


 

posted on   秋·风  阅读(694)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示