怎样得到网卡的MAC地址(20分)

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

ly_qiangzhen

Unregistered / Unconfirmed
GUEST, unregistred user!
在WIN2K SERVER和WIN98下怎样得到本机网卡的MAC地址
 
开始下run ipconfig/all可以得到
 
获得MAC地址
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,nb30;
type
PASTAT = ^TASTAT;
TASTAT = record
adapter : TAdapterStatus;
name_buf : TNameBuffer;
end;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
function Getmac:string;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
label1.caption:=Getmac;
end;

function TForm1.Getmac:string;
var
ncb : TNCB;
s:string;
adapt : TASTAT;
lanaEnum : TLanaEnum;
i, j, m : integer;
strPart, strMac : string;
begin
FillChar(ncb, SizeOf(TNCB), 0);
ncb.ncb_command := Char(NCBEnum);
ncb.ncb_buffer := PChar(@lanaEnum);
ncb.ncb_length := SizeOf(TLanaEnum);
s:=Netbios(@ncb);
for i := 0 to integer(lanaEnum.length)-1 do
begin
FillChar(ncb, SizeOf(TNCB), 0);
ncb.ncb_command := Char(NCBReset);
ncb.ncb_lana_num := lanaEnum.lana;
Netbios(@ncb);
Netbios(@ncb);
FillChar(ncb, SizeOf(TNCB), 0);
ncb.ncb_command := Chr(NCBAstat);
ncb.ncb_lana_num := lanaEnum.lana;
ncb.ncb_callname := '* ';
ncb.ncb_buffer := PChar(@adapt);
ncb.ncb_length := SizeOf(TASTAT);
m:=0;
if (Win32Platform = VER_PLATFORM_WIN32_NT) then
m:=1;
if m=1 then
begin
if Netbios(@ncb) = Chr(0) then
strMac := '';
for j := 0 to 5 do
begin
strPart := IntToHex(integer(adapt.adapter.adapter_address[j]), 2);
strMac := strMac + strPart + '-';
end;
SetLength(strMac, Length(strMac)-1);
end;
if m=0 then
if Netbios(@ncb) <> Chr(0) then
begin
strMac := '';
for j := 0 to 5 do
begin
strPart := IntToHex(integer(adapt.adapter.adapter_address[j]), 2);
strMac := strMac + strPart + '-';
end;
SetLength(strMac, Length(strMac)-1);
end;
end;
result:=strmac;
end;
end.
 
楼主可能是要在程序中得到吧:
这里有段VC写的,有同事改成DELPHI后可用,不过现在找不到源码.
自已改改吧:
#include "stdafx.h"
#include <windows.h>
#include <wincon.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
// 因为是通过NetAPI来获取网卡信息,所以需要包含其题头文件nb30.h
#include <nb30.h>
typedef struct _ASTAT_
{
ADAPTER_STATUS adapt;
NAME_BUFFER
NameBuff [30];
} ASTAT, * PASTAT;
ASTAT Adapter;
// 定义一个存放返回网卡信息的变量
// 输入参数:lana_num为网卡编号,一般地,从0开始,但在Windows 2000中并不一定是连续分配的
void getmac_one (int lana_num)
{
NCB ncb;
UCHAR uRetCode;
memset( &amp;ncb, 0, sizeof(ncb) );
ncb.ncb_command = NCBRESET;
ncb.ncb_lana_num = lana_num // 指定网卡号
// 首先对选定的网卡发送一个NCBRESET命令,以便进行初始化
uRetCode = Netbios( &amp;ncb );
printf( "The NCBRESET return code is: 0x%x /n", uRetCode );
memset( &amp;ncb, 0, sizeof(ncb) );
ncb.ncb_command = NCBASTAT;
ncb.ncb_lana_num = lana_num; // 指定网卡号
strcpy((char *)ncb.ncb_callname,"*" );
ncb.ncb_buffer = (unsigned char *) &amp;Adapter; // 指定返回的信息存放的变量
ncb.ncb_length = sizeof(Adapter);
// 接着,可以发送NCBASTAT命令以获取网卡的信息
uRetCode = Netbios( &amp;ncb );
printf( "The NCBASTAT return code is: 0x%x /n", uRetCode );
if ( uRetCode == 0 )
{
// 把网卡MAC地址格式化成常用的16进制形式,如0010-A4E4-5802
printf( "The Ethernet Number[%d] is: %02X%02X-%02X%02X-%02X%02X/n",
lana_num,
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] );
}
}
int main(int argc, char* argv[])
{
NCB ncb;
UCHAR uRetCode;
LANA_ENUM lana_enum;
memset( &amp;ncb, 0, sizeof(ncb) );
ncb.ncb_command = NCBENUM;
ncb.ncb_buffer = (unsigned char *) &amp;lana_enum;
ncb.ncb_length = sizeof(lana_enum);
// 向网卡发送NCBENUM命令,以获取当前机器的网卡信息,如有多少个网卡、每张网卡的编号等
uRetCode = Netbios( &amp;ncb );
printf( "The NCBENUM return code is: 0x%x /n", uRetCode );
if ( uRetCode == 0 )
{
printf( "Ethernet Count is : %d/n/n", lana_enum.length);
// 对每一张网卡,以其网卡编号为输入编号,获取其MAC地址
for ( int i=0; i<lana_enum.length; ++i)
getmac_one( lana_enum.lana);
}
return 0;
}
 
后退
顶部