关于网卡(200分)

  • 主题发起人 主题发起人 lsys
  • 开始时间 开始时间
L

lsys

Unregistered / Unconfirmed
GUEST, unregistred user!
1、DELHPI中如何获得网卡的MAC值。<br>2、有多快网卡如何区分<br>3、猫,ADSL有没有类似网卡的MAC值<br>
 
<br>Translated by CKER <br><br>第一种方法使用Microsoft的Netbios API。 这是一套通过Winsock提供底层网络支持的命令。使用Netbios的最大缺点是您必须在系统中安装了Netbios服务(如果您在windows网络中启用了文件共享的话,这就不是问题了)。除此此外,这种方法又快又准确。<br><br>Netbios API只包括了一个函数,就叫做Netbios。这个函数使用网络控制块(network control block)结构作为参数,这个结构告诉函数要做什么。结构的定义如下:<br>&nbsp; typedef struct _NCB {<br>&nbsp; &nbsp; UCHAR &nbsp;ncb_command;<br>&nbsp; &nbsp; UCHAR &nbsp;ncb_retcode;<br>&nbsp; &nbsp; UCHAR &nbsp;ncb_lsn;<br>&nbsp; &nbsp; UCHAR &nbsp;ncb_num;<br>&nbsp; &nbsp; PUCHAR ncb_buffer;<br>&nbsp; &nbsp; WORD &nbsp; ncb_length;<br>&nbsp; &nbsp; UCHAR &nbsp;ncb_callname[NCBNAMSZ];<br>&nbsp; &nbsp; UCHAR &nbsp;ncb_name[NCBNAMSZ];<br>&nbsp; &nbsp; UCHAR &nbsp;ncb_rto;<br>&nbsp; &nbsp; UCHAR &nbsp;ncb_sto;<br>&nbsp; &nbsp; void (CALLBACK *ncb_post) (struct _NCB *);<br>&nbsp; &nbsp; UCHAR &nbsp;ncb_lana_num;<br>&nbsp; &nbsp; UCHAR &nbsp;ncb_cmd_cplt;<br>&nbsp;#ifdef _WIN64<br>&nbsp; &nbsp; UCHAR &nbsp;ncb_reserve[18];<br>&nbsp;#else<br>&nbsp; &nbsp; UCHAR &nbsp;ncb_reserve[10];<br>&nbsp;#endif<br>&nbsp; &nbsp; HANDLE ncb_event;<br>} NCB, *PNCB;<br><br>&nbsp;<br><br>重点在于ncb_command 成员。这个成员告诉Netbios该作什么。我们使用三个命令来探测MAC地址。他们在MSDN的定义如下:<br>命令描述:<br>NCBENUM Windows NT/2000: 列举系统中网卡的数量。使用此命令后,ncb_buffer成员指向由LANA_ENUM结构填充的缓冲区。<br>NCBENUM 不是标准的 NetBIOS 3.0 命令。<br><br>NCBRESET 重置网卡。网卡在接受新的NCB命令之前必须重置。<br>NCBASTAT 接受本地或远程接口卡的状态。使用此命令后,ncb_buffer成员指向由ADAPTER_STATUS结构填充的缓冲区,随后是NAME_BUFFER结构的数组。<br><br>下面就是取得您系统MAC地址的步骤:<br>1》列举所有的接口卡。<br>2》重置每块卡以取得它的正确信息。<br>3》查询接口卡,取得MAC地址并生成标准的冒号分隔格式。<br><br>下面就是实例源程序。<br>netbios.cpp<br><br>#include &lt;windows.h&gt;<br>#include &lt;stdlib.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;iostream&gt;<br>#include &lt;string&gt;<br><br>using namespace std;<br>#define bzero(thing,sz) memset(thing,0,sz)<br><br>bool GetAdapterInfo(int adapter_num, string &amp;mac_addr)<br>{<br>&nbsp; // 重置网卡,以便我们可以查询<br>&nbsp; NCB Ncb;<br>&nbsp; memset(&amp;Ncb, 0, sizeof(Ncb));<br>&nbsp; Ncb.ncb_command = NCBRESET;<br>&nbsp; Ncb.ncb_lana_num = adapter_num;<br>&nbsp; if (Netbios(&amp;Ncb) != NRC_GOODRET) {<br>&nbsp; &nbsp; mac_addr = "bad (NCBRESET): ";<br>&nbsp; &nbsp; mac_addr += string(Ncb.ncb_retcode);<br>&nbsp; &nbsp; return false;<br>&nbsp; }<br><br>&nbsp; // 准备取得接口卡的状态块<br>&nbsp; bzero(&amp;Ncb,sizeof(Ncb);<br>&nbsp; Ncb.ncb_command = NCBASTAT;<br>&nbsp; Ncb.ncb_lana_num = adapter_num;<br>&nbsp; strcpy((char *) Ncb.ncb_callname, "*");<br>&nbsp; struct ASTAT<br>&nbsp; {<br>&nbsp; &nbsp; ADAPTER_STATUS adapt;<br>&nbsp; &nbsp; NAME_BUFFER NameBuff[30];<br>&nbsp; } Adapter;<br>&nbsp; bzero(&amp;Adapter,sizeof(Adapter));<br>&nbsp; Ncb.ncb_buffer = (unsigned char *)&amp;Adapter;<br>&nbsp; Ncb.ncb_length = sizeof(Adapter);<br><br>&nbsp; // 取得网卡的信息,并且如果网卡正常工作的话,返回标准的冒号分隔格式。<br>&nbsp; if (Netbios(&amp;Ncb) == 0)<br>&nbsp; {<br>&nbsp; &nbsp; char acMAC[18];<br>&nbsp; &nbsp; sprintf(acMAC, "%02X:%02X:%02X:%02X:%02X:%02X",<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int (Adapter.adapt.adapter_address[0]),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int (Adapter.adapt.adapter_address[1]),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int (Adapter.adapt.adapter_address[2]),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int (Adapter.adapt.adapter_address[3]),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int (Adapter.adapt.adapter_address[4]),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int (Adapter.adapt.adapter_address[5]));<br>&nbsp; &nbsp; mac_addr = acMAC;<br>&nbsp; &nbsp; return true;<br>&nbsp; }<br>&nbsp; else<br>&nbsp; {<br>&nbsp; &nbsp; mac_addr = "bad (NCBASTAT): ";<br>&nbsp; &nbsp; mac_addr += string(Ncb.ncb_retcode);<br>&nbsp; &nbsp; return false;<br>&nbsp; }<br>}<br><br>int main()<br>{<br>&nbsp; // 取得网卡列表<br>&nbsp; LANA_ENUM AdapterList;<br>&nbsp; NCB Ncb;<br>&nbsp; memset(&amp;Ncb, 0, sizeof(NCB));<br>&nbsp; Ncb.ncb_command = NCBENUM;<br>&nbsp; Ncb.ncb_buffer = (unsigned char *)&amp;AdapterList;<br>&nbsp; Ncb.ncb_length = sizeof(AdapterList);<br>&nbsp; Netbios(&amp;Ncb);<br><br>&nbsp; // 取得本地以太网卡的地址<br>&nbsp; string mac_addr;<br>&nbsp; for (int i = 0; i &lt; AdapterList.length - 1; ++i)<br>&nbsp; {<br>&nbsp; &nbsp; if (GetAdapterInfo(AdapterList.lana, mac_addr))<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; cout &lt;&lt; "Adapter " &lt;&lt; int (AdapterList.lana) &lt;&lt;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "'s MAC is " &lt;&lt; mac_addr &lt;&lt; endl;<br>&nbsp; &nbsp; }<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; cerr &lt;&lt; "Failed to get MAC address! Do you" &lt;&lt; endl;<br>&nbsp; &nbsp; &nbsp; cerr &lt;&lt; "have the NetBIOS protocol installed?" &lt;&lt; endl;<br>&nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; }<br>&nbsp; }<br><br>&nbsp; return 0;<br>}<br><br><br>file://---------------------------------------------------------------------------<br><br><br>第二种方法-使用COM GUID API<br>这种方法使用COM API创建一个GUID(全局唯一标识符)并从那里继承MAC地址。GUID通常用来标识COM组件以及系统中的其他对象。它们是由MAC地址(结合其他东西)计算得来的,表面上MAC地址就包含在其中。我说表面上是因为事实上并没有包含。<br>我提供这种方法更多的是为了作为反面教材。您也许用这种方法能够得到MAC地址,但有时候您只会得到随机的十六进制数值。<br>下面的例子十分简单,无需多讲。我们使用CoCreateGuid创建GUID,并将最后六个字节放入字符串中。它们可能是MAC地址,但并不是必然的。<br><br>uuid.cpp<br>#include &lt;windows.h&gt;<br>#include &lt;iostream&gt;<br>#include &lt;conio.h&gt;<br><br>using namespace std;<br><br>int main()<br>{<br>&nbsp; &nbsp; cout &lt;&lt; "MAC address is: ";<br><br>&nbsp; &nbsp; // 向COM要求一个UUID。如果机器中有以太网卡,<br>&nbsp; &nbsp; // UUID最后的六个字节(Data4的2-7字节)应该是本地以太网卡的MAC地址。<br>&nbsp; &nbsp; GUID uuid;<br>&nbsp; &nbsp; CoCreateGuid(&amp;uuid);<br>&nbsp; &nbsp; // Spit the address out<br>&nbsp; &nbsp; char mac_addr[18];<br>&nbsp; &nbsp; sprintf(mac_addr,"%02X:%02X:%02X:%02X:%02X:%02X",<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uuid.Data4[2],uuid.Data4[3],uuid.Data4[4],<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; uuid.Data4[5],uuid.Data4[6],uuid.Data4[7]);<br>&nbsp; &nbsp; cout &lt;&lt; mac_addr &lt;&lt; endl;<br>&nbsp; &nbsp; getch();<br>&nbsp; &nbsp; return 0;<br>}<br><br><br>第三种方法- 使用SNMP扩展API<br>我要讨论的第三种方法是使用Windows的SNMP(简单网络管理协议)扩展来取得MAC地址。在我的经验里,这个协议很简单。代码也是直勾勾的向前的。基本步骤和Netbios相同:<br>1》取得网卡列表<br>2》查询每块卡的类型和MAC地址<br>3》保存当前网卡<br>我个人对SNMP了解不多,但如我刚刚所言,代码十分清楚。<br><br>snmp.cpp<br>#include &lt;snmp.h&gt;<br>#include &lt;conio.h&gt;<br>#include &lt;stdio.h&gt;<br><br>typedef bool(WINAPI * pSnmpExtensionInit) (<br>&nbsp; &nbsp; &nbsp; &nbsp; IN DWORD dwTimeZeroReference,<br>&nbsp; &nbsp; &nbsp; &nbsp; OUT HANDLE * hPollForTrapEvent,<br>&nbsp; &nbsp; &nbsp; &nbsp; OUT AsnObjectIdentifier * supportedView);<br><br>typedef bool(WINAPI * pSnmpExtensionTrap) (<br>&nbsp; &nbsp; &nbsp; &nbsp; OUT AsnObjectIdentifier * enterprise,<br>&nbsp; &nbsp; &nbsp; &nbsp; OUT AsnInteger * genericTrap,<br>&nbsp; &nbsp; &nbsp; &nbsp; OUT AsnInteger * specificTrap,<br>&nbsp; &nbsp; &nbsp; &nbsp; OUT AsnTimeticks * timeStamp,<br>&nbsp; &nbsp; &nbsp; &nbsp; OUT RFC1157VarBindList * variableBindings);<br><br>typedef bool(WINAPI * pSnmpExtensionQuery) (<br>&nbsp; &nbsp; &nbsp; &nbsp; IN BYTE requestType,<br>&nbsp; &nbsp; &nbsp; &nbsp; IN OUT RFC1157VarBindList * variableBindings,<br>&nbsp; &nbsp; &nbsp; &nbsp; OUT AsnInteger * errorStatus,<br>&nbsp; &nbsp; &nbsp; &nbsp; OUT AsnInteger * errorIndex);<br><br>typedef bool(WINAPI * pSnmpExtensionInitEx) (<br>&nbsp; &nbsp; &nbsp; &nbsp; OUT AsnObjectIdentifier * supportedView);<br><br>void main()<br>{<br>&nbsp; HINSTANCE m_hInst;<br>&nbsp; pSnmpExtensionInit m_Init;<br>&nbsp; pSnmpExtensionInitEx m_InitEx;<br>&nbsp; pSnmpExtensionQuery m_Query;<br>&nbsp; pSnmpExtensionTrap m_Trap;<br>&nbsp; HANDLE PollForTrapEvent;<br>&nbsp; AsnObjectIdentifier SupportedView;<br>&nbsp; UINT OID_ifEntryType[] = {1, 3, 6, 1, 2, 1, 2, 2, 1, 3};<br>&nbsp; UINT OID_ifEntryNum[] = {1, 3, 6, 1, 2, 1, 2, 1};<br>&nbsp; UINT OID_ipMACEntAddr[] = {1, 3, 6, 1, 2, 1, 2, 2, 1, 6};<br>&nbsp; AsnObjectIdentifier MIB_ifMACEntAddr =<br>&nbsp; &nbsp; { sizeof(OID_ipMACEntAddr) &nbsp;sizeof(UINT), OID_ipMACEntAddr };<br>&nbsp; AsnObjectIdentifier MIB_ifEntryType =<br>&nbsp; &nbsp; {sizeof(OID_ifEntryType) &nbsp;sizeof(UINT), OID_ifEntryType};<br>&nbsp; AsnObjectIdentifier MIB_ifEntryNum =<br>&nbsp; &nbsp; {sizeof(OID_ifEntryNum) &nbsp;sizeof(UINT), OID_ifEntryNum};<br>&nbsp; RFC1157VarBindList varBindList;<br>&nbsp; RFC1157VarBind varBind[2];<br>&nbsp; AsnInteger errorStatus;<br>&nbsp; AsnInteger errorIndex;<br>&nbsp; AsnObjectIdentifier MIB_NULL = {0, 0};<br>&nbsp; int ret;<br>&nbsp; int dtmp;<br>&nbsp; int i = 0, j = 0;<br>&nbsp; bool found = false;<br>&nbsp; char TempEthernet[13];<br>&nbsp; m_Init = NULL;<br>&nbsp; m_InitEx = NULL;<br>&nbsp; m_Query = NULL;<br>&nbsp; m_Trap = NULL;<br><br>&nbsp; /* 载入SNMP DLL并取得实例句柄 */<br>&nbsp; m_hInst = LoadLibrary("inetmib1.dll");<br>&nbsp; if (m_hInst &lt; (HINSTANCE) HINSTANCE_ERROR)<br>&nbsp; {<br>&nbsp; &nbsp; m_hInst = NULL;<br>&nbsp; &nbsp; return;<br>&nbsp; }<br>&nbsp; m_Init =<br>&nbsp; &nbsp; (pSnmpExtensionInit) GetProcAddress(m_hInst, "SnmpExtensionInit");<br>&nbsp; m_InitEx =<br>&nbsp; &nbsp; (pSnmpExtensionInitEx) GetProcAddress(m_hInst,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "SnmpExtensionInitEx");<br>&nbsp; m_Query =<br>&nbsp; &nbsp; (pSnmpExtensionQuery) GetProcAddress(m_hInst,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"SnmpExtensionQuery");<br>&nbsp; m_Trap =<br>&nbsp; &nbsp; (pSnmpExtensionTrap) GetProcAddress(m_hInst, "SnmpExtensionTrap");<br>&nbsp; m_Init(GetTickCount(), &amp;PollForTrapEvent, &amp;SupportedView);<br><br>&nbsp; /* 初始化用来接收m_Query查询结果的变量列表 */<br>&nbsp; varBindList.list = varBind;<br>&nbsp; varBind[0].name = MIB_NULL;<br>&nbsp; varBind[1].name = MIB_NULL;<br><br>&nbsp; /* 在OID中拷贝并查找接口表中的入口数量 */<br>&nbsp; varBindList.len = 1; &nbsp; &nbsp; &nbsp; &nbsp;/* Only retrieving one item */<br>&nbsp; SNMP_oidcpy(&amp;varBind[0].name, &amp;MIB_ifEntryNum);<br>&nbsp; ret =<br>&nbsp; &nbsp; m_Query(ASN_RFC1157_GETNEXTREQUEST, &amp;varBindList, &amp;errorStatus,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;errorIndex);<br>&nbsp; printf("# of adapters in this system : %in",<br>&nbsp; &nbsp; &nbsp; &nbsp;varBind[0].value.asnValue.number);<br>&nbsp; varBindList.len = 2;<br><br>&nbsp; /* 拷贝OID的ifType-接口类型 */<br>&nbsp; SNMP_oidcpy(&amp;varBind[0].name, &amp;MIB_ifEntryType);<br><br>&nbsp; /* 拷贝OID的ifPhysAddress-物理地址 */<br>&nbsp; SNMP_oidcpy(&amp;varBind[1].name, &amp;MIB_ifMACEntAddr);<br><br>&nbsp; do<br>&nbsp; {<br><br>&nbsp; &nbsp; /* 提交查询,结果将载入 varBindList。<br>&nbsp; &nbsp; &nbsp; &nbsp;可以预料这个循环调用的次数和系统中的接口卡数量相等 */<br>&nbsp; &nbsp; ret =<br>&nbsp; &nbsp; &nbsp; m_Query(ASN_RFC1157_GETNEXTREQUEST, &amp;varBindList, &amp;errorStatus,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;errorIndex);<br>&nbsp; &nbsp; if (!ret)<br>&nbsp; &nbsp; &nbsp; ret = 1;<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; /* 确认正确的返回类型 */<br>&nbsp; &nbsp; &nbsp; ret =<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SNMP_oidncmp(&amp;varBind[0].name, &amp;MIB_ifEntryType,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MIB_ifEntryType.idLength); if (!ret) {<br>&nbsp; &nbsp; j++;<br>&nbsp; &nbsp; dtmp = varBind[0].value.asnValue.number;<br>&nbsp; &nbsp; printf("Interface #%i type : %in", j, dtmp);<br><br>&nbsp; &nbsp; /* Type 6 describes ethernet interfaces */<br>&nbsp; &nbsp; if (dtmp == 6)<br>&nbsp; &nbsp; {<br><br>&nbsp; &nbsp; &nbsp; /* 确认我们已经在此取得地址 */<br>&nbsp; &nbsp; &nbsp; ret =<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SNMP_oidncmp(&amp;varBind[1].name, &amp;MIB_ifMACEntAddr,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;MIB_ifMACEntAddr.idLength);<br>&nbsp; &nbsp; &nbsp; if ((!ret) &amp;&amp; (varBind[1].value.asnValue.address.stream != NULL))<br>&nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; if((varBind[1].value.asnValue.address.stream[0] == 0x44)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;&amp; (varBind[1].value.asnValue.address.stream[1] == 0x45)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;&amp; (varBind[1].value.asnValue.address.stream[2] == 0x53)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;&amp; (varBind[1].value.asnValue.address.stream[3] == 0x54)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;&amp; (varBind[1].value.asnValue.address.stream[4] == 0x00))<br>&nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* 忽略所有的拨号网络接口卡 */<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("Interface #%i is a DUN adaptern", j);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp; if ((varBind[1].value.asnValue.address.stream[0] == 0x00)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;&amp; (varBind[1].value.asnValue.address.stream[1] == 0x00)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;&amp; (varBind[1].value.asnValue.address.stream[2] == 0x00)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;&amp; (varBind[1].value.asnValue.address.stream[3] == 0x00)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;&amp; (varBind[1].value.asnValue.address.stream[4] == 0x00)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &amp;&amp; (varBind[1].value.asnValue.address.stream[5] == 0x00))<br>&nbsp; &nbsp; &nbsp; &nbsp; {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /* 忽略由其他的网络接口卡返回的NULL地址 */<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("Interface #%i is a NULL addressn", j);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; &nbsp; &nbsp; sprintf(TempEthernet, "%02x%02x%02x%02x%02x%02x",<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; varBind[1].value.asnValue.address.stream[0],<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; varBind[1].value.asnValue.address.stream[1],<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; varBind[1].value.asnValue.address.stream[2],<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; varBind[1].value.asnValue.address.stream[3],<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; varBind[1].value.asnValue.address.stream[4],<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; varBind[1].value.asnValue.address.stream[5]);<br>&nbsp; &nbsp; &nbsp; &nbsp; printf("MAC Address of interface #%i: %sn", j,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TempEthernet);}<br>&nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; }<br>&nbsp; } while (!ret); &nbsp; &nbsp; &nbsp; &nbsp; /* 发生错误终止。 */<br>&nbsp; getch();<br><br>&nbsp; FreeLibrary(m_hInst);<br>&nbsp; /* 解除绑定 */<br>&nbsp; SNMP_FreeVarBind(&amp;varBind[0]);<br>&nbsp; SNMP_FreeVarBind(&amp;varBind[1]);<br>}<br><br>
 
关于第二个问题,也许可以在注册表里找到答案<br>或者 wininet.pas 里的InternetGetConnectedState能有用<br><br>第三个猫是有MAC的,可以通过上面的方法得到,<br>adsl是通过网卡接到机器上的吧,所以是网卡的MAC.<br><br>另外改网卡的MAC很简单
 
to gxcooo 如何修改网卡的MAC
 
to gxcooo 你贴一大堆C的代码干吗
 
只能得到网络适配器的Mac地址,但是并不一定是硬件本身的,也可以是注册表中设置的。<br><br>修改注册表的方法:<br>HKEY_LOCAL_MACHINE/system/Currentcontrolset/services/classes/net,在下面找到一项叫params的,<br>在里面加一项NetworkAddress的主键,默认写你要设的mac地址,要连续的写,如004040404040<br>然后再到params主键下,添一个字符串,名字为NetworkAddress,值还设为你要设的mac,也连续写。<br>关闭注册表,重新启动,你的网卡地址已改。打开网络邻居的属性,双击网卡会发现有一个高级设置,<br>实际就是你在注册表中加的新项NetworkAddress,<br><br>2、我的上面方法不行,现在使用<br>HKEY_LOCAL_MACHINE/System/CurrentControlSet/Services/Class/Net/0000<br>里面增加字符串 NetworkAddress 然后写入“12-13-14-15-16-17”<br>const<br>&nbsp; sNetBiosError = 'NetBIOS错误%d';<br><br>type<br>&nbsp; TMACAddress = packed array[0..5] of Byte;<br>&nbsp; ENetBiosError = class(Exception);<br>&nbsp; TAStat = record Adapt: TAdapterStatus;<br>&nbsp; &nbsp; NameBuff: array[0..30] of TNameBuffer;<br>&nbsp; end;<br><br>function GetMacAddress(AdapterNum: Integer): TMACAddress;<br>var<br>&nbsp; Ncb: TNCB;<br>&nbsp; uRetCode: Char;<br>&nbsp; J: Integer;<br>&nbsp; Adapter: TAStat;<br>begin<br>&nbsp; FillChar(NCB, SizeOf(NCB), 0);<br>&nbsp; with NCB do<br>&nbsp; begin<br>&nbsp; &nbsp; ncb_command := Char(NCBRESET);<br>&nbsp; &nbsp; ncb_lana_num := Char(AdapterNum);<br>&nbsp; end;<br>&nbsp; uRetCode := Netbios(@Ncb);<br>&nbsp; if uRetCode &lt;&gt; #0 then raise Exception.CreateFmt(sNetBIOSError, [Ord(uRetCode)]);<br>&nbsp; FillChar(NCB, SizeOf(NCB), 0);<br>&nbsp; with NCB do<br>&nbsp; begin<br>&nbsp; &nbsp; ncb_command := Char(NCBASTAT);<br>&nbsp; &nbsp; ncb_lana_num := Char(AdapterNum);<br>&nbsp; &nbsp; StrCopy(ncb_callname, '*');<br>&nbsp; &nbsp; ncb_buffer := @Adapter;<br>&nbsp; &nbsp; ncb_length := sizeof(Adapter);<br>&nbsp; end;<br>&nbsp; uRetCode := Netbios(@Ncb);<br>&nbsp; if uRetCode &lt;&gt; #0 then raise Exception.CreateFmt(sNetBIOSError, [Ord(uRetCode)]);<br>&nbsp; for J := 0 to 5 do<br>&nbsp; &nbsp; Result[J] := Ord(Adapter.Adapt.Adapter_address[J]);<br>end;<br><br><br>方法2:<br><br>uses nb30;<br><br>function NBGetAdapterAddress(a: Integer): string;<br>var<br>&nbsp; NCB: TNCB; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Netbios control block //NetBios控制块<br>&nbsp; ADAPTER: TADAPTERSTATUS; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Netbios adapter status//取网卡状态<br>&nbsp; LANAENUM: TLANAENUM; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Netbios lana<br>&nbsp; intIdx: Integer; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Temporary work value//临时变量<br>&nbsp; cRC: Char; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Netbios return code//NetBios返回值<br>&nbsp; strTemp: string; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Temporary string//临时变量<br>begin<br>&nbsp; Result := '';<br><br>&nbsp; try<br>&nbsp; &nbsp; ZeroMemory(@NCB, SizeOf(NCB)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Zero control blocl<br><br>&nbsp; &nbsp; NCB.ncb_command := Chr(NCBENUM); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Issue enum command<br>&nbsp; &nbsp; cRC := NetBios(@NCB);<br><br>&nbsp; &nbsp; NCB.ncb_buffer := @LANAENUM; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Reissue enum command<br>&nbsp; &nbsp; NCB.ncb_length := SizeOf(LANAENUM);<br>&nbsp; &nbsp; cRC := NetBios(@NCB);<br>&nbsp; &nbsp; if Ord(cRC) &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; exit;<br><br>&nbsp; &nbsp; ZeroMemory(@NCB, SizeOf(NCB)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Reset adapter<br>&nbsp; &nbsp; NCB.ncb_command := Chr(NCBRESET);<br>&nbsp; &nbsp; NCB.ncb_lana_num := LANAENUM.lana[a];<br>&nbsp; &nbsp; cRC := NetBios(@NCB);<br>&nbsp; &nbsp; if Ord(cRC) &lt;&gt; 0 then<br>&nbsp; &nbsp; &nbsp; exit;<br><br><br>&nbsp; &nbsp; ZeroMemory(@NCB, SizeOf(NCB)); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Get adapter address<br>&nbsp; &nbsp; NCB.ncb_command := Chr(NCBASTAT);<br>&nbsp; &nbsp; NCB.ncb_lana_num := LANAENUM.lana[a];<br>&nbsp; &nbsp; StrPCopy(NCB.ncb_callname, '*');<br>&nbsp; &nbsp; NCB.ncb_buffer := @ADAPTER;<br>&nbsp; &nbsp; NCB.ncb_length := SizeOf(ADAPTER);<br>&nbsp; &nbsp; cRC := NetBios(@NCB);<br><br>&nbsp; &nbsp; strTemp := ''; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Convert it to string<br>&nbsp; &nbsp; for intIdx := 0 to 5 do<br>&nbsp; &nbsp; &nbsp; strTemp := strTemp + InttoHex(Integer(ADAPTER.adapter_address[intIdx]), 2);<br>&nbsp; &nbsp; Result := strTemp;<br>&nbsp; finally<br>&nbsp; end;<br>end;
 
多人接受答案了。
 
后退
顶部