获取网卡地址的程序(100分)

  • 主题发起人 主题发起人 尘莽
  • 开始时间 开始时间

尘莽

Unregistered / Unconfirmed
GUEST, unregistred user!
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, Itemsdo
begin
Sorted := True;
begin
Update;
Clear;
for I := 0 to Byte(LanaEnum.length) - 1do
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.
 
//Form
object GetMACForm: TGetMACForm
Left = 208
Top = 171
Width = 324
Height = 100
Caption = '度取网卡地址程序'
Color = clBtnFace
Font.Charset = GB2312_CHARSET
Font.Color = clWindowText
Font.Height = -14
Font.Name = '宋体'
Font.Style = []
OldCreateOrder = True
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 14
object Label1: TLabel
Left = 14
Top = 46
Width = 70
Height = 14
Caption = '网卡编号:'
end
object Label2: TLabel
Left = 14
Top = 12
Width = 70
Height = 14
Caption = '网卡地址:'
end
object GetNetAddr: TButton
Left = 232
Top = 7
Width = 75
Height = 24
Caption = '&amp;G.地址'
Default = True
TabOrder = 2
OnClick = GetNetAddrClick
end
object Edit1: TEdit
Left = 94
Top = 8
Width = 121
Height = 22
TabOrder = 1
end
object ComboBox1: TComboBox
Left = 94
Top = 42
Width = 121
Height = 22
ItemHeight = 14
MaxLength = 5
TabOrder = 0
OnChange = GetNetAddrClick
end
object Button1: TButton
Left = 232
Top = 42
Width = 75
Height = 23
Cancel = True
Caption = '&amp;X.退出'
TabOrder = 3
end
end
//程序
unit MainMAC;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, NB30;
type
TGetMACForm = class(TForm)
Edit1: TEdit;
ComboBox1: TComboBox;
GetNetAddr: TButton;
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
procedure GetNetAddrClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
GetMACForm: TGetMACForm;
implementation
{$R *.DFM}
// 枚举当前安装的网络适配器
function GetLanaEnum(LanaEnum: PLanaEnum): Char;
var
LanaEnumNCB: PNCB;
begin
New(LanaEnumNCB);
ZeroMemory(LanaEnumNCB, SizeOf(TNCB));
LanaEnumNCB.ncb_buffer := PChar(LanaEnum);
LanaEnumNCB.ncb_length := SizeOf(TLanaEnum);
LanaEnumNCB.ncb_command := Char(NCBENUM);
NetBios(LanaEnumNCB);
Result := LanaEnumNCB.ncb_cmd_cplt;
Dispose(LanaEnumNCB);
end;

procedure TGetMACForm.FormCreate(Sender: TObject);
var
LanaEnum: PLanaEnum;
I: Integer;
begin
exit;
New(LanaEnum);
ZeroMemory(LanaEnum, SizeOf(TLanaEnum));
ComboBox1.Items.Clear;
if GetLanaEnum(LanaEnum) = Char(NRC_GOODRET) then
begin
for I := 0 to Byte(LanaEnum.length) - 1do
ComboBox1.Items.Add(IntToStr(Byte(LanaEnum.lana)));
ComboBox1.ItemIndex := 0;
GetNetAddrClick(nil);
end;
Dispose(LanaEnum);
end;

function GetMACAddress(LanaNum: Byte): String;
var
AdapterStatus: PAdapterStatus;
StatNCB: PNCB;
I: Integer;
MACAddress: array [1 .. 6] of Byte;
begin
New(StatNCB);
ZeroMemory(StatNCB, SizeOf(TNCB));
StatNCB.ncb_length := SizeOf(TAdapterStatus) + $FF * SizeOf(TNameBuffer);
GetMem(AdapterStatus, StatNCB.ncb_length);
StatNCB.ncb_buffer := PChar(AdapterStatus);
StatNCB.ncb_callname := '*'+ #0;
StatNCB.ncb_lana_num := Char(LanaNum);
StatNCB.ncb_command := Char(NCBASTAT);
NetBios(StatNCB);
if Byte(StatNCB.ncb_cmd_cplt) = NRC_GOODRET then
begin
MoveMemory(@MACAddress, AdapterStatus, SizeOf(MACAddress));
For i := 1 to 6do
Result := Result + Format('%2.2x', [MACAddress]);
End else
Result := '';
FreeMem(AdapterStatus);

Dispose(StatNCB);
end;

procedure TGetMACForm.GetNetAddrClick(Sender: TObject);
begin
if ComboBox1.Text = '' then
exit;
Edit1.Text := GetMACAddress(StrToInt(ComboBox1.Text));
end;

end.
 

Adapter 10's MAC is 00:E0:4C:DD:26:8B
Adapter 8's MAC is 80:E5:20:52:41:53
Adapter 7's MAC is 7E:5C:20:52:41:53
Adapter 6's MAC is 7E:5C:20:52:41:53
Adapter 9's MAC is 00:E0:4C:DD:26:8B
返回这么多,都是什么意思呢?
 
怎么写了 这么长?
100分给我80分吧!
 
返回的就是网卡地址,你的电脑软件系统安装的各种网卡。
0是对应安装的硬件的网卡地址。
 
那个10 应该是0 吧
 
接受答案了.
 
后退
顶部