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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~