哪位能告诉我这个函数怎么用?(50分)

  • 主题发起人 主题发起人 HY_IHFGHY_HY
  • 开始时间 开始时间
H

HY_IHFGHY_HY

Unregistered / Unconfirmed
GUEST, unregistred user!
本人的程序中需要有一个获取本机网卡地址的功能,于是驾猫狂搜N小时,终于被我找到了一个返回网卡地址的函数,但是本人不会用(自知才疏学浅),不知哪位能人义士能为我指点迷津,内容如下:

//定义部份
var s4,s5:string;
type
TNBLanaResources = (lrAlloc, lrFree);
type
PMACAddress = ^TMACAddress;
TMACAddress = array[0..5] of Byte;
type
TGate = record
Off2,op,seg,off1:WORD;
end;
LONGDWORD = INT64;
var
IDTR: LONGDWORD;
SavedGate:TGate;
OurGate: TGate;
dd: array [0..256] of word;
dsn:array [0..20] of char;

//函数实现部份
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;
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;
 
uses nb30

function GetNetBIOSAddress :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;
 
{********************************************************}
{ unit MainMAC }
{ }
{ Retrieving the MAC-Address (Media Access Control) }
{ }
{ Copyright (c) 1997,1998 Vlad Sharnin }
{ }
{ E-mail: vladshar@ufanet.ru }
{ Home page: http://vladshar.ufanet.ru }
{ }
{********************************************************}
{

My other products:

- NBLib32 (NetBios Library for Win32) - shareware native
components collection for peer-to-peer networking
LAN&WAN via NetBios under Windows 95/98/NT.
For D2/3/4 & BCB 1/3.

- Port95 - freeware Delphi-unit for access to the I/O ports
in traditional by Pascal-style under Win95/98.
For D2/3/4 & BCB 1/3.

Coming soon:

- Shareware NBLib32 as ActiveX Control (OCX)
- Freeware NBLib16 1.00b - compatible with NBLib32
components collection for Delphi 1

{********************************************************}

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;

{
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)));
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.
 
到google上搜索,可以找到一些说明
 
闈炲父鎰熻阿weiwei81123鐨勭瓟妗堬紝鎴戝悜浣犺〃绀虹敱琛风殑鎰熻阿锛岀?鍚勪綅鏂板勾蹇?箰锛屼竾浜嬪?鎰忥紒
 
后退
顶部