delphi 读取网卡mac的3种方式
来源:互联网 发布:c 数据段 编辑:程序博客网 时间:2024/06/06 01:25
7月11日
用硬件id加密程序,其中一项加密是用mac地址。
mac如果是真实网卡信息是唯一的,不好的消息是网卡很容易就能虚拟,所以需要配合其它手段。
现在有三种方式,各有利弊
第一种 最简单也最实用,通过调用ipconfig -all 来取得网卡。
第二种 通过netbios,因为是网络协议,如果网卡没有连接上,取不到mac地址
第三种 通过rpc调用,只能得到一块网卡.在有多块网卡时会出现有时取到的MAC不一致.
第一种
function MacAddress(): string;
implementation
function GetIpMacAddress(): TStringList;
var
files,re:TStringList;
i:integer;
row:String;
begin
re:=TStringList.Create;
files:= TStringList.Create;
winexec('cmd /c ipconfig -all >c:/fastspeeed.txt',SW_HIDE);
sleep(500);
files.LoadFromFile('c:/fastspeeed.txt');
for I := 0 to files.Count -1 do
begin
row:= files.Strings[i];
if ContainsText(row,'Physical Address') then
begin
try
re.Add(RightStr(row,17));
Except
implementation
function GetIpMacAddress(): TStringList;
var
files,re:TStringList;
i:integer;
row:String;
begin
re:=TStringList.Create;
files:= TStringList.Create;
winexec('cmd /c ipconfig -all >c:/fastspeeed.txt',SW_HIDE);
sleep(500);
files.LoadFromFile('c:/fastspeeed.txt');
for I := 0 to files.Count -1 do
begin
row:= files.Strings[i];
if ContainsText(row,'Physical Address') then
begin
try
re.Add(RightStr(row,17));
Except
end;
end;
end;
winexec('cmd /c del c:/fastspeeed.txt',SW_HIDE);
Result:= re;
end;
end;
end;
winexec('cmd /c del c:/fastspeeed.txt',SW_HIDE);
Result:= re;
end;
第二种
function GetMacAddress(): TStringList ;
var
re:TStringList;
i:integer;
begin
re:=TStringList.Create;
for I := 0 to 10 do
begin
try
re.add(NBGetAdapterAddress(i));
Except
continue;
end;
end;
Result:= re;
end;
var
re:TStringList;
i:integer;
begin
re:=TStringList.Create;
for I := 0 to 10 do
begin
try
re.add(NBGetAdapterAddress(i));
Except
continue;
end;
end;
Result:= re;
end;
function NBGetAdapterAddress(adapterNum: integer): String;
//adapter00为指定的网卡适配器
Var
NCB:TNCB; // NetBios控制块
ADAPTER : TADAPTERSTATUS; // 获取网卡状态
LANAENUM : TLANAENUM; // Netbios lana
intIdx : Integer; // 临时变量
cRC : AnsiChar; // NetBios返回值
strTemp : String; // 临时变量
Begin
Result := '';
Try
// 清空控制块
ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command:=Chr(NCBENUM);
cRC := NetBios(@NCB);
// 重新设置enum命令
NCB.ncb_buffer := @LANAENUM;
NCB.ncb_length := SizeOf(LANAENUM);
cRC := NetBios(@NCB);
If Ord(cRC)<>0 Then
exit;
// 重新设定网卡
ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command := Chr(NCBRESET);
NCB.ncb_lana_num := LANAENUM.lana[adapterNum];
cRC := NetBios(@NCB);
If Ord(cRC)<>0 Then
exit;
// 获取网卡地址
ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command := Chr(NCBASTAT);
NCB.ncb_lana_num := LANAENUM.lana[adapterNum];
StrPCopy(NCB.ncb_callname, '*');
NCB.ncb_buffer := @ADAPTER;
NCB.ncb_length := SizeOf(ADAPTER);
cRC := NetBios(@NCB);
// 将获得信息转换为字符串
strTemp := '';
For intIdx := 0 To 5 Do
strTemp := strTemp + InttoHex(Integer(ADAPTER.adapter_address[intIdx]),2);
Result := strTemp;
Finally
End;
end;
//adapter00为指定的网卡适配器
Var
NCB:TNCB; // NetBios控制块
ADAPTER : TADAPTERSTATUS; // 获取网卡状态
LANAENUM : TLANAENUM; // Netbios lana
intIdx : Integer; // 临时变量
cRC : AnsiChar; // NetBios返回值
strTemp : String; // 临时变量
Begin
Result := '';
Try
// 清空控制块
ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command:=Chr(NCBENUM);
cRC := NetBios(@NCB);
// 重新设置enum命令
NCB.ncb_buffer := @LANAENUM;
NCB.ncb_length := SizeOf(LANAENUM);
cRC := NetBios(@NCB);
If Ord(cRC)<>0 Then
exit;
// 重新设定网卡
ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command := Chr(NCBRESET);
NCB.ncb_lana_num := LANAENUM.lana[adapterNum];
cRC := NetBios(@NCB);
If Ord(cRC)<>0 Then
exit;
// 获取网卡地址
ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command := Chr(NCBASTAT);
NCB.ncb_lana_num := LANAENUM.lana[adapterNum];
StrPCopy(NCB.ncb_callname, '*');
NCB.ncb_buffer := @ADAPTER;
NCB.ncb_length := SizeOf(ADAPTER);
cRC := NetBios(@NCB);
// 将获得信息转换为字符串
strTemp := '';
For intIdx := 0 To 5 Do
strTemp := strTemp + InttoHex(Integer(ADAPTER.adapter_address[intIdx]),2);
Result := strTemp;
Finally
End;
end;
第三种
function MacAddress(): string;
var
Lib: Cardinal;
Func: function(GUID: PGUID): Longint; stdcall;
GUID1, GUID2: TGUID;
begin
Result := '';
Lib := LoadLibrary('rpcrt4.dll');
if Lib <> 0 then
begin
@Func := GetProcAddress(Lib, 'UuidCreateSequential');
if Assigned(Func) then
begin
if (Func(@GUID1) = 0) and
(Func(@GUID2) = 0) and
(GUID1.D4[2] = GUID2.D4[2]) and
(GUID1.D4[3] = GUID2.D4[3]) and
(GUID1.D4[4] = GUID2.D4[4]) and
(GUID1.D4[5] = GUID2.D4[5]) and
(GUID1.D4[6] = GUID2.D4[6]) and
(GUID1.D4[7] = GUID2.D4[7]) then
begin
Result :=
IntToHex(GUID1.D4[2], 2) + '-' +
IntToHex(GUID1.D4[3], 2) + '-' +
IntToHex(GUID1.D4[4], 2) + '-' +
IntToHex(GUID1.D4[5], 2) + '-' +
IntToHex(GUID1.D4[6], 2) + '-' +
IntToHex(GUID1.D4[7], 2);
end;
end;
end;
end;
var
Lib: Cardinal;
Func: function(GUID: PGUID): Longint; stdcall;
GUID1, GUID2: TGUID;
begin
Result := '';
Lib := LoadLibrary('rpcrt4.dll');
if Lib <> 0 then
begin
@Func := GetProcAddress(Lib, 'UuidCreateSequential');
if Assigned(Func) then
begin
if (Func(@GUID1) = 0) and
(Func(@GUID2) = 0) and
(GUID1.D4[2] = GUID2.D4[2]) and
(GUID1.D4[3] = GUID2.D4[3]) and
(GUID1.D4[4] = GUID2.D4[4]) and
(GUID1.D4[5] = GUID2.D4[5]) and
(GUID1.D4[6] = GUID2.D4[6]) and
(GUID1.D4[7] = GUID2.D4[7]) then
begin
Result :=
IntToHex(GUID1.D4[2], 2) + '-' +
IntToHex(GUID1.D4[3], 2) + '-' +
IntToHex(GUID1.D4[4], 2) + '-' +
IntToHex(GUID1.D4[5], 2) + '-' +
IntToHex(GUID1.D4[6], 2) + '-' +
IntToHex(GUID1.D4[7], 2);
end;
end;
end;
end;
- delphi 读取网卡mac的3种方式
- delphi 读取网卡mac的3种方式
- 读取网卡的MAC地址
- delphi获得网卡的MAC地址
- DELPHI获取网卡MAC地址
- asp 读取网卡mac地址
- asp 读取网卡mac地址
- VC++中怎样读取网卡的MAC地址
- 如何用VC++开发读取网卡MAC地址的程序
- asp程序 读取某IP的网卡MAC地址
- 如何用VC++开发读取网卡MAC地址的程序
- 如何用VC++开发读取网卡MAC地址的程序
- 读取网卡信息IP,MAC的GetAdaptersInfo函数
- Delphi自定义获取网卡MAC地址过程
- DELPHI获取网卡MAC地址 转
- 用delphi读取网卡物理号
- Win10秘笈:两种方式修改网卡物理地址(MAC)
- 网卡MAC地址相关信息大全-3 如何实现修改网卡物理地址的三种方法
- 解决问题:Asianux3下Oracle 10g无法启动
- Apache Tomcat 6.0 配置 SSL
- 最新SqlServer2005分页存储过程备份
- GNU GENERAL PUBLIC LICENSE
- BizTalk Server 基于消息的基本数据交换(二) - 使用属性提升 实现根据消息内容的路由及订阅
- delphi 读取网卡mac的3种方式
- c++字符串大小写转换
- 一道简单性能调优题
- 数字人生
- System.out.write()和System.out.println()有什么区别?
- 网站迁移一周的状况,301永久重定向似乎没什么作用
- 面试题(二)
- C#基础系列(10)-- 第三部分 面向对象相关 -- 浅复制与深复制(3)
- 记忆中的计算机-13 (IBM T60 Pentium-M 2GHz)