转载请保留本网页地址:http://blog.csdn.net/sushengmiyan/article/details/8544442
先贴代码:
{----------------------------------------------------------------------------\-
作者:sushengmiyan 2013.01.26
参数:Alana: Integer = 0
返回:string
功能:通过LANA号获取Mac物理地址 (内存方式获取),整体步骤如下:
一、枚举系统上可用的所有LANA编号
二、重置计划使用的每个LANA编号
三、适配器命令获取网卡地址
\-----------------------------------------------------------------------------}
function MemGetNetCardName(Alana: Integer = 0): string;
var
NCB: TNCB;//Netbios控制块
AdapterStatus: TAdapterStatus;//取网卡状态
LanaEnum: TLanaEnum; //LANA枚举值
I: Integer;
CRC: Char;//netBios返回值
begin
Result := '';
Try
{ http://blog.csdn.net/sushengmiyan/article/details/8543811
一、枚举LANA值
①.申请分配一个TNCB结构 NCB: TNCB;
②.将TNCB结构变量初始化成O ZeroMemory(@NCB , SizeOf(NCB));
③.置命令为NCBENUM NCB.ncb_Command := chr(NCBENUM);
④.为ncb_buffer分配LANA_ENUM NCB.ncb_buffer := @LANAENUM;
⑤.为NCB_length制定长度 NCB.NCB_length := Sizeof(LANAENUM);
⑥.调用Netbios函数获取Netbios CRC := NetBios(@NCB);
⑦.返回值NRC_GOODRET表示成功 NCB.ncb_retcode = Chr(NRC_GOODRET)
}
ZeroMemory(@NCB , SizeOf(NCB));
NCB.ncb_Command := Chr(NCBENUM);
NCB.ncb_buffer := @LANAENUM;
NCB.NCB_length := Sizeof(LANAENUM);
CRC := NetBios(@NCB);
if not (NCB.ncb_retcode = Chr(NRC_GOODRET)) then Exit;
{ http://blog.csdn.net/sushengmiyan/article/details/8543811
二、重置计划使用的每个LANA编号
①.申请分配一个TNCB结构 NCB: TNCB;
②.将TNCB结构变量初始化成O ZeroMemory(@NCB , SizeOf(NCB));
③.置命令为NCBRESET NCB.ncb_Command := chr(NCBRESET);
④.给命令设置LANA编号 NCB.ncb_lana_num := LanaEnum.lana[Alana];
⑤.调用Netbios函数获取Netbios CRC := NetBios(@NCB);
⑥.返回值NRC_GOODRET表示成功 NCB.ncb_retcode = Chr(NRC_GOODRET)
}
ZeroMemory(@NCB , SizeOf(NCB));
NCb.ncb_Command := Chr(NCBRESET);
NCB.ncb_lana_num := LanaEnum.lana[Alana];
CRC := Netbios(@NCB);
if not (NCB.ncb_retcode = Chr(NRC_GOODRET)) then Exit;
{ http://blog.csdn.net/sushengmiyan/article/details/8543811
三、使用TAdapterStatus结构获取网卡地址
①.申请分配一个TNCB结构 NCB: TNCB;
②.将TNCB结构变量初始化成O ZeroMemory(@NCB , SizeOf(NCB));
③.置命令为NCBASTAT NCB.ncb_Command := chr(NCBASTAT);
④.为ncb_buffer分配LANA_ENUM NCB.ncb_buffer := @LANAENUM;
⑤.设置ncb_callname NCB.ncb_callname := '* ' + #0;
⑥.为ncb_buffer分配AdapterStatus NCB.ncb_buffer := @AdapterStatus;
⑦.为NCB_length制定长度 NCB.NCB_length := Sizeof(AdapterStatus);
⑧.调用Netbios函数获取Netbios CRC := NetBios(@NCB);
}
ZeroMemory(@NCB , SizeOf(NCB));
NCb.ncb_Command := chr(NCBASTAT);
NCB.ncb_lana_num := LANAENUM.lana[Alana];
NCB.ncb_callname := '* ' + #0;
//不明白为何如此设置,*代表啥子?
//有懂的可以邮件分享 429119108@qq.com O(∩_∩)O谢谢
NCB.ncb_buffer := @AdapterStatus;
NCB.ncb_length := SizeOf(AdapterStatus);
CRC := NetBios(@NCB);
//获取形如AA-BB-CC-DD-EE-FF形式的mac物理地址字符串
Result := '';
for I := 0 to 5 do
if SameStr(Result , '') then
Result := Result + IntToHex(Integer(AdapterStatus.adapter_address[I]) , 2)
else
Result := Result + '-' + IntToHex(Integer(AdapterStatus.adapter_address[I]) , 2);
Finally
End;
end;
查看获取的mac地址是否正确的方式:
在Windows 2000/XP/Vista/WIN8中
点击 开始-运行-进入后输入ipconfig /all
在出现的结果中查找‘物理地址’或者Physical Address后的字符串即可。