unit MainMAC;interfaceuses
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;{
The LanaEnum parameter specifies structure describing accessible numbers Lana.
The amounty Lana is defined by number of the installed network cards and network
protocols for each of them. Each accessible Lana identifies a unique combination
of one driver of the protocol and one network card. Each structure NCB contains
a field ncb_lana_num, in which necessary number Lana (i.e. identifier of pair
protocol/MAC) should be written.
In systems with one network card, Lana = 0, usually corresponds
"to the protocol by default".
}// 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[I])));
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);
ShowMessage(ComboBox1.Text);
ShowMessage(char(LanaNum));
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.
mainMac.dfm的内容:
==========================
object GetMACForm: TGetMACForm
Left = 208
Top = 171
Width = 324
Height = 100
Caption = 'Retrieving of MAC-address'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = True
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 20
Top = 50
Width = 61
Height = 13
Caption = 'LanaNumber'
end
object Label2: TLabel
Left = 22
Top = 20
Width = 63
Height = 13
Caption = 'MAC-address'
end
object MACAddrButton: TButton
Left = 232
Top = 14
Width = 75
Height = 25
Caption = 'GetMACAddr'
TabOrder = 0
OnClick = MACAddrButtonClick
end
object Edit1: TEdit
Left = 94
Top = 14
Width = 121
Height = 21
TabOrder = 1
Text = 'Edit1'
end
object ResetButton: TButton
Left = 232
Top = 44
Width = 75
Height = 25
Caption = 'Reset Lana'
TabOrder = 2
OnClick = ResetButtonClick
end
object ComboBox1: TComboBox
Left = 94
Top = 46
Width = 121
Height = 21
Style = csDropDownList
ItemHeight = 13
TabOrder = 3
OnChange = ComboBox1Change
end
end
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;{
The LanaEnum parameter specifies structure describing accessible numbers Lana.
The amounty Lana is defined by number of the installed network cards and network
protocols for each of them. Each accessible Lana identifies a unique combination
of one driver of the protocol and one network card. Each structure NCB contains
a field ncb_lana_num, in which necessary number Lana (i.e. identifier of pair
protocol/MAC) should be written.
In systems with one network card, Lana = 0, usually corresponds
"to the protocol by default".
}// 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[I])));
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);
ShowMessage(ComboBox1.Text);
ShowMessage(char(LanaNum));
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.
mainMac.dfm的内容:
==========================
object GetMACForm: TGetMACForm
Left = 208
Top = 171
Width = 324
Height = 100
Caption = 'Retrieving of MAC-address'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = True
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 20
Top = 50
Width = 61
Height = 13
Caption = 'LanaNumber'
end
object Label2: TLabel
Left = 22
Top = 20
Width = 63
Height = 13
Caption = 'MAC-address'
end
object MACAddrButton: TButton
Left = 232
Top = 14
Width = 75
Height = 25
Caption = 'GetMACAddr'
TabOrder = 0
OnClick = MACAddrButtonClick
end
object Edit1: TEdit
Left = 94
Top = 14
Width = 121
Height = 21
TabOrder = 1
Text = 'Edit1'
end
object ResetButton: TButton
Left = 232
Top = 44
Width = 75
Height = 25
Caption = 'Reset Lana'
TabOrder = 2
OnClick = ResetButtonClick
end
object ComboBox1: TComboBox
Left = 94
Top = 46
Width = 121
Height = 21
Style = csDropDownList
ItemHeight = 13
TabOrder = 3
OnChange = ComboBox1Change
end
end
1. 要获得其它机器的MAC地址,事先要知道其IP地址
2. 命令行nbtstat -a IP可以获得该IP的MAC地址
3. 程序中根据IP地址动态创建一个*.bat文件,写入
@echo off
nbtstat -a IP>c:\temp.txt
4. 在程序中运行这个*.bat,运行结果就保存到了c:\temp.txt
winexec('*.bat',sw_hide);
5. 程序中读出c:\temp.txt的最后一行,就是IP地址对应的MAC地址
能不能解释memo里的内容是做什么的啊???