⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0,1410,26040,00.html

📁 C++builder学习资料C++builder
💻 HTML
📖 第 1 页 / 共 2 页
字号:


<HTML>



<HEAD>



<TITLE>Three Ways To Get Your System's Network MAC Address</TITLE>

</HEAD>

<BODY MARGINWIDTH="5" MARGINHEIGHT="5" TOPMARGIN="0" LEFTMARGIN="0" BGCOLOR="#FFFFFF">



<A NAME="top"></A>



<table>

 



<TR>

  

   <td>

 

<SPAN CLASS="title3"><b>Three Ways To Get Your System's Network MAC Address</b></SPAN>  

  

			  

	<BR>  

  

    <P>  

<BLOCKQUOTE CLASS="abstract"><B>Abstract:</B>This article describes three different ways to programatically get the MAC address of your system's network card (NIC).</BLOCKQUOTE><P>  

   

  </table>  

<a name="top"></a>  

<table width="100%" border=0 cellpadding=5 cellspacing=0>  

<tr><td  valign=center align=middle bgcolor="black">  

<font color="white" face="Arial Black">  

Determining The MAC Address of Your System in Three Parts  

</font></td></tr>  

<tr><td align=left><br>  

  

<p><big>I</big> have searched endlessly through newsgroups and websites for a simple way to determine the MAC (network interface card)  

adress of my system. You would think that because of the overwhelming demand (especially in newsgroups) for an answer  

to this question, that there would be many examples available. This is not so. These examples are as a result of tireless searching  

and some actual learning on my part (imagine that!). Note: none of these examples involve parsing the output of ipconfig.exe /all &lt;g&gt;</p>  

  

<table cellpadding="0" cellspacing="0" border="1">  

<tr><td align=middle bgcolor=black><font color=white>Table Of Contents</font></td></tr>  

<tr><td><A style="text-decoration: none;" href="#objective" onMouseover="window.status='Objective';return true" onMouseOut="window.status='';return true;">Objective</a></td></tr>  

<tr><td><A style="text-decoration: none;" href="#method1" onMouseover="window.status='Netbios';return true" onMouseOut="window.status='';return true;">Method 1 - Netbios API</a></td></tr>  

<tr><td><A style="text-decoration: none;" href="#method2" onMouseover="window.status='COM - GUID';return true" onMouseOut="window.status='';return true;">Method 2 - Using the COM API</a></td></tr>  

<tr><td><A style="text-decoration: none;" href="#method3" onMouseover="window.status='SNMP';return true" onMouseOut="window.status='';return true;">Method 3 Using SNMP</a></td></tr>  

<tr><td><A style="text-decoration: none;" href="http://codecentral.borland.com/codecentral/ccweb.exe/download?id=15294" onMouseover="window.status='Download Project Source';return true" onMouseOut="window.status='';return true;">Example Projects</a></td></tr>  

</table><br>  

  

</td></tr>  

  

<tr><td align = left bgcolor=black>  

<a name="objective"></a><A class=loginLink href="#top"><font color=white face="Arial" size=+1>Objective</font></a>  

</td></tr>  

  

<tr><td>  

<big>T</big>he purpose of this article is to give easy methods of determining your MAC address. I will  

explain how the code works and provide sample projects to illustrate it. I am assuming that you have  

a grasp of the following concepts:<br><br>  

  

<ul>  

	<li>Borland C++Builder</li>  

          

	<li>Simple network concepts</li>  

          

	<li>Some Win32 API</li>  

</ul><br>  

</td></tr>  

  

<tr><td align = left bgcolor=black>  

<a name="method1"></a><A class=loginLink href="#top"><font color=white face="Arial" size=+1>Method 1 - Using the Netbios API</font></a>  

&nbsp;&nbsp;<font color=white face="Arial" size=+1>&#91;<a href="#code1" class=loginLink onMouseover="window.status='netbios.cpp';return true" onMouseout="window.status='';return true">code</a>&#93;</font>  

</td></tr>  

  

<tr><td>  

<P>  

<big>T</big>his method of getting your computer's MAC address uses Microsoft's Netbios API. This is a set  

of commands that enables lower-level network support than provided by say Winsock. The disadvantage to using  

Netbios to determine your address is that you must have Netbios installed (not really a problem if you are on  

a Windows network and use file sharing). Otherwise this method is quick and accurate.  

</p>  

<p>  

The Netbios API includes only one function, called simply Netbios. This function takes a network control block  

structure as its parameter, which tells it what it needs to do. The structure is defined as follows:  

<br><br>  

<table cellpadding=2 cellspacing=0 border=0>  

<tr>  

<td>&nbsp;</td>  

<td bgcolor="#b1b1b1"><pre>

<b>typedef struct</b> _NCB { 

    UCHAR  ncb_command; 

    UCHAR  ncb_retcode; 

    UCHAR  ncb_lsn; 

    UCHAR  ncb_num; 

    PUCHAR ncb_buffer; 

    WORD   ncb_length; 

    UCHAR  ncb_callname[NCBNAMSZ]; 

    UCHAR  ncb_name[NCBNAMSZ]; 

    UCHAR  ncb_rto; 

    UCHAR  ncb_sto; 

    void (CALLBACK *ncb_post) (struct _NCB *); 

    UCHAR  ncb_lana_num; 

    UCHAR  ncb_cmd_cplt; 

#ifdef _WIN64

    UCHAR  ncb_reserve[18];

#else

    UCHAR  ncb_reserve[10]; 

#endif

    HANDLE ncb_event; 

} NCB, *PNCB;</pre>  

</td></tr>  

</table>  

<p>  

Focus especially on the ncb_command member. This is the member that tells Netbios what to do. We will use 3  

commands in determining the MAC address. They are defined on MSDN as follows:  

</p><p>  

<table width=87% align=center cellpacing=1 cellpadding=3 border=0>  

<tr bgcolor="#a1a1a1"><th align=left>Command</th><th align=left>Description</th></tr>  

<tr bgcolor="#b1b1b1"><td align=left valign=top>NCBENUM</td>  

<td align=left valign=top>  

<b>Windows NT/2000</b>: Enumerates LAN adapter (LANA) numbers. When this code is specified, the <b>ncb_buffer</b> member points to a buffer to be filled with a LANA_ENUM structure.<br><br>  

NCBENUM is not a standard NetBIOS 3.0 command.   

</td>  

<tr bgcolor="#b1b1b1"><td align=left valign=top>NCBRESET</td>  

<td align=left valign=top>  

Resets a LAN adapter. An adapter must be reset before it can accept any other NCB command that specifies the same number in the <b>ncb_lana_num</b> member.  

</td></tr>  

<tr bgcolor="#b1b1b1"><td align=left valign=top>NCBASTAT</td>  

<td align=left valign=top>Retrieves the status of either a local or remote adapter. When this code is specified, the <b>ncb_buffer</b> member points to a buffer to be filled with an ADAPTER_STATUS structure, followed by an array of NAME_BUFFER structures.</td>  

</tr>  

</table>  

<P>  

Here are the steps to getting the MAC address(es) of your system:<ul>  

<li>Enumerate the adaptors</li>  

<li>Reset each adaptor to get proper information from it</li>  

<li>Query the adaptor to get the MAC address and put it in standard colon-separated format</li>  

</ul>  

The code below provides a simple illustration of these concepts. For more information on Netbios functions,  

refer to the Microsoft help files or MSDN.  

  

<P>  

<table width=100% cellpadding=3 cellspacing=0 border=1>  

<tr><td bgcolor=blue><font color=white><a name="code1">netbios.cpp</a></font></td></tr>  

  

<tr><td>  

<pre>

<font color="202020">#include &lt;windows.h&gt;</font>

<font color="202020">#include &lt;stdlib.h&gt;</font>

<font color="202020">#include &lt;stdio.h&gt;</font>

<font color="202020">#include &lt;iostream&gt;</font>

<font color="202020">#include &lt;string&gt;</font>



<b>using</b> <b>namespace</b> std;

<font color="202020">#define bzero(thing,sz) memset(thing,0,sz)</font>



<b>bool</b> GetAdapterInfo(<b>int</b> adapter_num, string &amp;mac_addr)

{

    <font color="#0000AA"><i>// Reset the LAN adapter so that we can begin querying it</i></font>

  NCB Ncb;

  memset(&amp;Ncb, 0, <b>sizeof</b>(Ncb));

  Ncb.ncb_command = NCBRESET;

  Ncb.ncb_lana_num = adapter_num;

  <b>if</b> (Netbios(&amp;Ncb) != NRC_GOODRET) {

    mac_addr = <font color="#0000FF">&quot;bad (NCBRESET): &quot;</font>;

    mac_addr += string(Ncb.ncb_retcode);

    <b>return</b> <b>false</b>;

  }



  <font color="#0000AA"><i>// Prepare to get the adapter status block</i></font>

  bzero(&amp;Ncb,<b>sizeof</b>(Ncb);

  Ncb.ncb_command = NCBASTAT;

  Ncb.ncb_lana_num = adapter_num;

  strcpy((<b>char</b> *) Ncb.ncb_callname, <font color="#0000FF">&quot;*&quot;</font>);

  <b>struct</b> ASTAT

  {

    ADAPTER_STATUS adapt;

    NAME_BUFFER NameBuff[30];

  } Adapter;

  bzero(&amp;Adapter,<b>sizeof</b>(Adapter));

  Ncb.ncb_buffer = (<b>unsigned</b> <b>char</b> *)&amp;Adapter;

  Ncb.ncb_length = <b>sizeof</b>(Adapter);



  <font color="#0000AA"><i>// Get the adapter's info and, if this works, return it in standard,</i></font>

  <font color="#0000AA"><i>// colon-delimited form.</i></font>

  <b>if</b> (Netbios(&amp;Ncb) == 0)

  {

    <b>char</b> acMAC[18];

    sprintf(acMAC, <font color="#0000FF">&quot;%02X:%02X:%02X:%02X:%02X:%02X&quot;</font>,

            <b>int</b> (Adapter.adapt.adapter_address[0]),

            <b>int</b> (Adapter.adapt.adapter_address[1]),

            <b>int</b> (Adapter.adapt.adapter_address[2]),

            <b>int</b> (Adapter.adapt.adapter_address[3]),

            <b>int</b> (Adapter.adapt.adapter_address[4]),

            <b>int</b> (Adapter.adapt.adapter_address[5]));

    mac_addr = acMAC;

    <b>return</b> true;

  }

  <b>else</b>

  {

    mac_addr = <font color="#0000FF">&quot;bad (NCBASTAT): &quot;</font>;

    mac_addr += string(Ncb.ncb_retcode);

    <b>return</b> <b>false</b>;

  }

}



<b>int</b> main()

{

  <font color="#0000AA"><i>// Get adapter list</i></font>

  LANA_ENUM AdapterList;

  NCB Ncb;

  memset(&amp;Ncb, 0, <b>sizeof</b>(NCB));

  Ncb.ncb_command = NCBENUM;

  Ncb.ncb_buffer = (<b>unsigned</b> <b>char</b> *)&amp;AdapterList;

  Ncb.ncb_length = <b>sizeof</b>(AdapterList);

  Netbios(&amp;Ncb);



  <font color="#0000AA"><i>// Get all of the local ethernet addresses</i></font>

  string mac_addr;

  <b>for</b> (<b>int</b> i = 0; i &lt; AdapterList.length - 1; ++i)

  {

    <b>if</b> (GetAdapterInfo(AdapterList.lana[i], mac_addr))

    {

      cout &lt;&lt; <font color="#0000FF">&quot;Adapter &quot;</font> &lt;&lt; <b>int</b> (AdapterList.lana[i]) &lt;&lt;

              <font color="#0000FF">&quot;'s MAC is &quot;</font> &lt;&lt; mac_addr &lt;&lt; endl;

    }

    <b>else</b>

    {

      cerr &lt;&lt; <font color="#0000FF">&quot;Failed to get MAC address! Do you&quot;</font> &lt;&lt; endl;

      cerr &lt;&lt; <font color="#0000FF">&quot;have the NetBIOS protocol installed?&quot;</font> &lt;&lt; endl;

      <b>break</b>;

    }

  }



  <b>return</b> 0;

}





<font color="#0000AA"><i>//---------------------------------------------------------------------------</i></font>

</pre>  

<br><br>  

</td></tr>  

  

</table>  

</td></tr>  

  

<tr><td align = left bgcolor=black>  

<a name="method2"></a><A class=loginLink href="#top"><font color=white face="Arial" size=+1>Method 2 - COM GUID API</font></a>  

&nbsp;&nbsp;<font color=white face="Arial" size=+1>&#91;<a href="#code2" class=loginLink onMouseover="window.status='uuid.cpp';return true" onMouseout="window.status='';return true">code</a>&#93;</font>  

</td></tr>  

  

<tr><td>  

<p>  

<big>T</big>his method uses the COM API to create a GUID (globably unique identifier) and derive the MAC address therefrom.   

GUIDs are used commonly to identify COM components and other objects in the system. They are computed from the   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -