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

📄 getmac-rpc.html

📁 SDK FAQ集
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"><html lang="en"><head><title>Winsock Programmer's FAQ: Get MAC Address, RPC Method</title><link rel="Stylesheet" type="text/css" href="../faq.css"></head><body bgcolor="#ffffee" text="#000000" link="#491e00" vlink="#7d2e01" alink="#da7417"><!--  ---- Header Bar ----  --><table border="0" width="95%" bgcolor="#006000" cellpadding="5" cellspacing="3" align="center">	<tr>		<td align="left" bgcolor="#e0e0c0">			<font size="2" face=Verdana,Arial,Helvetica>				<b><a href="../examples/getmac-netbios.html">&lt;&lt;</a></b>			</font>		</td>		<td align="center">			<font face=Verdana,Arial,Helvetica color="#ffffee">				<p align=center class=bigger3><b>				Winsock Programmer's FAQ<br>				Example: How to Get the Ethernet MAC Address, RPC Method				</b></p>			</font>			</td>		<td align="right" bgcolor="#e0e0c0">			<font size="2" face=Verdana,Arial,Helvetica>				<b><a href="../examples/getmac-snmp.html">&gt;&gt;</a></b>			</font>		</td>	</tr></table><!--  ---- Body Table ----  --><table width="95%" border="0" cellpadding="10">	<tr valign="top">		<td><p>This example shows you how to use the RPC API to retrieve the MACaddresses of at least one of the network adapters in a system. There aretwo weaknesses to this approach. The first is that it is not guaranteedto work. All it does is rely on the way UUIDs are defined by RPC, whichis that the last six bytes are derived from the hardware address of themachine's network adapter. The second, potentially more important problemis that this won't work if there is more than one Ethernet adapter in thesystem. I've not tried it, but I suspect that this program will returnthe address of the "first" Ethernet adapter. If you need the second aswell, better try the <a href="getmac-netbios.html">NetBIOS method</a>.</p><p>On the other hand, this method has several advantages. First, itdoes not require anything extra to be installed on the user's machine:RPC comes with all Win32 variants. Second, it's a lot smaller, codewise,than the NetBIOS method. Third, it doesn't have the 00:00:00:00:00:00problem of the NetBIOS code. That could probably be fixed, but thatwould make the second advantage (code size) even more obvious.</p><p>Nevertheless, this is in fact a demented kludge. Caveat hacker.</p><hr noshade size=1 color=#404040><h4 class=lmargin><a href="../examples/src/getmac-rpc.cpp">getmac-rpc.cpp</a></h4><pre><font color="#444444">// Visual C++ 5.0:  cl -GX getmac-rpc.cpp rpcrt4.lib</font><font color="#444444">// Borland C++ 5.0: bcc32 getmac-rpc.cpp</font><font color="0000ff"><strong>#include <font color="#008000">&lt;rpc.h&gt;</font></strong></font><font color="0000ff"><strong>#include <font color="#008000">&lt;iostream&gt;</font></strong></font><font color="0000ff"><strong>#ifdef _MSC_VER</strong></font><strong>using</strong> <strong>namespace</strong> <font color="#2040a0">std</font><font color="4444FF">;</font><font color="0000ff"><strong>#endif</strong></font><strong>int</strong> <font color="#2040a0">main</font><font color="4444FF">(</font><font color="4444FF">)</font><font color="4444FF"><strong>{</strong></font>    <font color="#2040a0">cout</font> <font color="4444FF">&lt;</font><font color="4444FF">&lt;</font> <font color="#008000">&quot;MAC address is: &quot;</font><font color="4444FF">;</font>    <font color="#444444">// Ask RPC to create a UUID for us.  If this machine has an Ethernet</font>    <font color="#444444">// adapter, the last six bytes of the UUID (bytes 2-7 inclusive in</font>    <font color="#444444">// the Data4 element) should be the MAC address of the local</font>    <font color="#444444">// Ethernet adapter.</font>    <font color="#2040a0">UUID</font> <font color="#2040a0">uuid</font><font color="4444FF">;</font>    <font color="#2040a0">UuidCreate</font><font color="4444FF">(</font><font color="4444FF">&amp;</font><font color="#2040a0">uuid</font><font color="4444FF">)</font><font color="4444FF">;</font>    <font color="#444444">// Spit the address out</font>    <strong>for</strong> <font color="4444FF">(</font><strong>int</strong> <font color="#2040a0">i</font> <font color="4444FF">=</font> <font color="#FF0000">2</font><font color="4444FF">;</font> <font color="#2040a0">i</font> <font color="4444FF">&lt;</font> <font color="#FF0000">8</font><font color="4444FF">;</font> <font color="4444FF">+</font><font color="4444FF">+</font><font color="#2040a0">i</font><font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font>        <font color="#2040a0">cout</font> <font color="4444FF">&lt;</font><font color="4444FF">&lt;</font> <font color="#2040a0">hex</font><font color="4444FF">;</font>        <font color="#2040a0">cout</font>.<font color="#2040a0">fill</font><font color="4444FF">(</font><font color="#008000">'0'</font><font color="4444FF">)</font><font color="4444FF">;</font>        <font color="#2040a0">cout</font>.<font color="#2040a0">width</font><font color="4444FF">(</font><font color="#FF0000">2</font><font color="4444FF">)</font><font color="4444FF">;</font>        <font color="#2040a0">cout</font> <font color="4444FF">&lt;</font><font color="4444FF">&lt;</font> <strong>int</strong> <font color="4444FF">(</font><font color="#2040a0">uuid</font>.<font color="#2040a0">Data4</font><font color="4444FF">[</font><font color="#2040a0">i</font><font color="4444FF">]</font><font color="4444FF">)</font><font color="4444FF">;</font>        <strong>if</strong> <font color="4444FF">(</font><font color="#2040a0">i</font> <font color="4444FF">&lt;</font> <font color="#FF0000">7</font><font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font>            <font color="#2040a0">cout</font> <font color="4444FF">&lt;</font><font color="4444FF">&lt;</font> <font color="#008000">&quot;:&quot;</font><font color="4444FF">;</font>        <font color="4444FF"><strong>}</strong></font>    <font color="4444FF"><strong>}</strong></font>    <font color="#2040a0">cout</font> <font color="4444FF">&lt;</font><font color="4444FF">&lt;</font> <font color="#2040a0">endl</font><font color="4444FF">;</font>    <strong>return</strong> <font color="#FF0000">0</font><font color="4444FF">;</font><font color="4444FF"><strong>}</strong></font></pre>		</td>	</tr></table><!--  ---- Document Footer ----  --><hr noshade size=1 color=#404040><table cellpadding=5 cellspacing=0 border=0 width=95% align=center> 	<tr>		<td align=left>		    <a href="../examples/getmac-netbios.html">&lt;&lt; Get MAC Address</a>		</td>		<td align=right>		    <a href="../examples/getmac-snmp.html">Get MAC Address, SNMP Method &gt;&gt;</a>		</td>	</tr>	<tr>		<td align=left>			<i>Last modified on 29 April 2000 at 15:52 UTC-7</i>		</td>		<td align=right>			<font size=-1>Please send corrections to <a href="mailto:tangent@cyberport.com">tangent@cyberport.com</a>.</font>		</td>	</tr>	</table>	<table cellpadding=5 cellspacing=0 border=0 width=95% align=center> 	<tr>		<td align=left width=33%>			<font size=-1>				<a href="../index.html"><b>&lt;</b> Go to the main FAQ page</a>			</font>		</td>		<td width=33%>			<font size=-1>			<center>				<a href="http://www.cyberport.com/~tangent/programming"><b>&lt;&lt;</b> Go to my Programming pages</a>			</center>			</font>		</td>		<td align=right width=33%>			<font size=-1>				<a href="http://www.cyberport.com/~tangent/"><b>&lt;&lt;&lt;</b> Go to my Home Page</a>			</font>		</td>	</tr>	</table>	</body></html>

⌨️ 快捷键说明

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