发信人: dwxiao()
整理人: girlrong(1999-11-13 14:55:38), 站内信件
|
-- ※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.104.37.102] 发信人: dunhill (烟灰飞扬), 信区: CLanguage 标 题: Re: 如何用程序取得网卡硬件序列号? 发信站: 网易虚拟社区 (Mon Sep 20 14:08:08 1999), 站内信件
HOWTO: Get the MAC Address for an Ethernet Adapter
Article ID: Q118623
The information in this article applies to:
Microsoft Win32 Software Development Kit (SDK) on the following platfo rms:
- Microsoft Windows NT versions 3.1, 3.5, 3.51
- Microsoft Windows 95
SUMMARY
This article demonstrates how to get the Media Access Control (MAC) ad dress for an ethernet adapter programmatically by using NetBIOS, if yo ur card is bound to NetBIOS.
MORE INFORMATION
To get the Media Access Control (MAC) address for an ethernet adapter programmatically, use the Netbios() NCBASTAT command and provide a "*" as the name in the NCB.ncb_CallName field. This is demonstrated in th e sample code below.
For machines with multiple network adapters you need to enumerate the LANA numbers and perform the NCBASTAT command on each. Even when you h ave a single network adapter, it is a good idea to enumerate valid LAN A numbers first and perform the NCBASTAT on one of the valid LANA numb ers. It is considered bad programming to hardcode the LANA number to 0 .
In addition, other hardware and software may be assigned their own MAC addresses. For example, a modem can have a MAC address. Also, a RAS c lient or server can install "dummy" network adapters that correspond t o a dialup or serial connection. Normally, these MAC addresses are ran domly generated. If an adapter status is called on a LANA that corresp onds to one of these adapters when no connection is present, Netbios r eturns error 0x34 (NRC_ENVNOTDEF) even if a reset was previously perfo rmed.
With the NetBEUI and IPX transports, the same information can be obtai ned at a command prompt by using:
net config workstation
The ID given is the MAC address.
The following code enumerates all LANA numbers, performs a reset (NCBR EST), and an adapter status (NCBASTAT).
Sample Code
#include <windows.h>
#include <wincon.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
typedef struct _ASTAT_
{
ADAPTER_STATUS adapt;
NAME_BUFFER NameBuff [30];
}ASTAT, * PASTAT;
ASTAT Adapter;
void main (void)
{
NCB Ncb;
UCHAR uRetCode;
char NetName[50];
LANA_ENUM lenum;
int i;
memset( &Ncb, 0, sizeof(Ncb) );
Ncb.ncb_command = NCBENUM;
Ncb.ncb_buffer = (UCHAR *)&lenum;
Ncb.ncb_length = sizeof(lenum);
uRetCode = Netbios( &Ncb );
printf( "The NCBENUM return code is: 0x%x \n", uRetCode );
for(i=0; i < lenum.length ;i++)
{
memset( &Ncb, 0, sizeof(Ncb) );
Ncb.ncb_command = NCBRESET;
Ncb.ncb_lana_num = lenum.lana[i];
uRetCode = Netbios( &Ncb );
printf( "The NCBRESET on LANA %d return code is: 0x%x \n",
lenum.lana[i], uRetCode );
memset( &Ncb, 0, sizeof (Ncb) );
Ncb.ncb_command = NCBASTAT;
Ncb.ncb_lana_num = lenum.lana[i];
strcpy( Ncb.ncb_callname, "* " );
Ncb.ncb_buffer = (char *) &Adapter;
Ncb.ncb_length = sizeof(Adapter);
uRetCode = Netbios( &Ncb );
printf( "The NCBASTAT on LANA %d return code is: 0x%x \n",
lenum.lana[i], uRetCode );
if ( uRetCode == 0 )
{
printf( "The Ethernet Number on LANA %d is:
%02x%02x%02x%02x%02x%02x\n",
lenum.lana[i],
Adapter.adapt.adapter_address[0],
Adapter.adapt.adapter_address[1],
Adapter.adapt.adapter_address[2],
Adapter.adapt.adapter_address[3],
Adapter.adapt.adapter_address[4],
Adapter.adapt.adapter_address[5] );
}
}
}
Keywords : kbnetwork kbAPI kbNetBIOS kbNTOS310 kbNTOS350 kbNTOS351 kbS DKPlatform kbWinOS95 kbGrpNet
Last Reviewed: September 7, 1998
© 1999 Microsoft Corporation. All rights reserved. Terms of Use.
-- 烟水寻常事,荒村一钓徒。
深宵沉醉起,无处觅菰蒲。
※ 来源:.月光软件站 http://www.moon-soft.com.[FROM: 202.104.38.176]
|
|