MAC 地址动态分配(50分)

  • 主题发起人 主题发起人 zhongxx
  • 开始时间 开始时间
Z

zhongxx

Unregistered / Unconfirmed
GUEST, unregistred user!
 听过IP地址动态分配,有谁只知道MAC地址的动态分配。我在书上看到的,不知是否有这方面计术。(我没多少分了,50分象有点少,凑和一下)
 
mac一般是网卡做好了的,每块网卡可以保证不会重复
 
MAC地址不是动态分配的,每个网卡生产厂都分配了一段地址范围,生产的每一块
网卡的地址都在其范围内不重复.
 
MAC地址也就是网卡的物理地址。
它一般固化在网卡芯片中,每个网卡生产厂都分配了一段地址范围,但可以
通过软件更改,还得看是什么卡,有的卡还改不了。我只改过NE2000 Compatible
的,还曾把一个网段内的两块卡改成相同的MAC,两台机器上网都没有问题
只是两台机器之间不通。
 
用 nbtstat -a 127.0.0.0 可以看你的网卡的mac
 
nbtstat -a 127.0.0.0
Host not found!!!!!!!!!!!!
 
我试过了 nbtstat -a 127.0.0.1不行的。
 
试试
nbtstat -a 主机的IP地址
That's ok!
 
下面的程序可以查看自己的MAC地址。

unit MainMAC;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, NB30;
type
TGetMACForm = class(TForm)
Edit1: TEdit;
ComboBox1: TComboBox;
MACAddrButton: TButton;
ResetButton: TButton;
Label1: TLabel;
Label2: TLabel;
procedure MACAddrButtonClick(Sender: TObject);
procedure ResetButtonClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
GetMACForm: TGetMACForm;

implementation
{$R *.DFM}
type
TNBLanaResources = (lrAlloc, lrFree);
type
PMACAddress = ^TMACAddress;
TMACAddress = array[0..5] of Byte;

// Get the list of adapters
function GetLanaEnum(LanaEnum: PLanaEnum): Byte;
var
LanaEnumNCB: PNCB;
begin
New(LanaEnumNCB);
ZeroMemory(LanaEnumNCB, SizeOf(TNCB));
try
with LanaEnumNCB^ do
begin
ncb_buffer := PChar(LanaEnum);
ncb_length := SizeOf(TLanaEnum);
ncb_command := Char(NCBENUM);
NetBios(LanaEnumNCB);
Result := Byte(ncb_cmd_cplt);
end;
finally
Dispose(LanaEnumNCB);
end;
end;

procedure TGetMACForm.FormCreate(Sender: TObject);
var
LanaEnum: PLanaEnum;
I: Integer;
begin
Edit1.Text := '';
New(LanaEnum);
ZeroMemory(LanaEnum, SizeOf(TLanaEnum));
try
if GetLanaEnum(LanaEnum) = NRC_GOODRET then
begin
with ComboBox1, Items do
begin
Sorted := True;
BeginUpdate;
Clear;
for I := 0 to Byte(LanaEnum.length) - 1 do
Add(IntToStr(Byte(LanaEnum.lana)));
ItemIndex := 0;
EndUpdate;
end;
end;
finally
Dispose(LanaEnum);
end;
ResetButton.Enabled := (Win32Platform = VER_PLATFORM_WIN32_NT);
end;

function ResetLana(LanaNum, ReqSessions, ReqNames: Byte;
LanaRes: TNBLanaResources): Byte;
var
ResetNCB: PNCB;
begin
New(ResetNCB);
ZeroMemory(ResetNCB, SizeOf(TNCB));
try
with ResetNCB^ do
begin
ncb_lana_num := Char(LanaNum); // Set Lana_Num
ncb_lsn := Char(LanaRes); // Allocation of new resources
ncb_callname[0] := Char(ReqSessions); // Query of max sessions
ncb_callname[1] := #0; // Query of max NCBs (default)
ncb_callname[2] := Char(ReqNames); // Query of max names
ncb_callname[3] := #0; // Query of use NAME_NUMBER_1
ncb_command := Char(NCBRESET);
NetBios(ResetNCB);
Result := Byte(ncb_cmd_cplt);
end;
finally
Dispose(ResetNCB);
end;
end;

function GetMACAddress(LanaNum: Byte; MACAddress: PMACAddress): Byte;
var
AdapterStatus: PAdapterStatus;
StatNCB: PNCB;
begin
New(StatNCB);
ZeroMemory(StatNCB, SizeOf(TNCB));
StatNCB.ncb_length := SizeOf(TAdapterStatus) + 255 * SizeOf(TNameBuffer);
GetMem(AdapterStatus, StatNCB.ncb_length);
try
with StatNCB^ do
begin
ZeroMemory(MACAddress, SizeOf(TMACAddress));
ncb_buffer := PChar(AdapterStatus);
ncb_callname := '* ' + #0;
ncb_lana_num := Char(LanaNum);
ncb_command := Char(NCBASTAT);
NetBios(StatNCB);
Result := Byte(ncb_cmd_cplt);
if Result = NRC_GOODRET then
MoveMemory(MACAddress, AdapterStatus, SizeOf(TMACAddress));
end;
finally
FreeMem(AdapterStatus);
Dispose(StatNCB);
end;
end;

procedure TGetMACForm.MACAddrButtonClick(Sender: TObject);
var
LanaNum: Byte;
MACAddress: PMACAddress;
RetCode: Byte;
begin
LanaNum := StrToInt(ComboBox1.Text);
New(MACAddress);
try
RetCode := GetMACAddress(LanaNum, MACAddress);
if RetCode = NRC_GOODRET then
begin
Edit1.Text := Format('%2.2x-%2.2x-%2.2x-%2.2x-%2.2x-%2.2x',
[MACAddress[0], MACAddress[1], MACAddress[2],
MACAddress[3], MACAddress[4], MACAddress[5]]);
end else
begin
Beep;
Edit1.Text := 'Error';
ShowMessage('GetMACAddress Error! RetCode = $' + IntToHex(RetCode, 2));
end;
finally
Dispose(MACAddress);
end;
end;

procedure TGetMACForm.ResetButtonClick(Sender: TObject);
var
RetCode: Byte;
LanaNum: Byte;
begin
LanaNum := StrToInt(ComboBox1.Text);
RetCode := ResetLana(LanaNum, 0, 0, lrAlloc);
if RetCode <> NRC_GOODRET then
begin
Beep;
ShowMessage('Reset Error! RetCode = $' + IntToHex(RetCode, 2));
end;
end;

procedure TGetMACForm.ComboBox1Change(Sender: TObject);
begin
Edit1.Text := '';
end;

end.
 
nbtstat -a <自已机器名>
 
多人接受答案了。
 

Similar threads

回复
0
查看
978
不得闲
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
后退
顶部