急!!求获取局域网内IP地址和网卡的物理地址原代码!! ( 积分: 50 )

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

hnw1982

Unregistered / Unconfirmed
GUEST, unregistred user!
我就这么多分了,下次不上.
 
我就这么多分了,下次不上.
 
----------------------以下转自网络
http://www.delphibbs.com/delphibbs/dispq.asp?lid=092696

//取网卡地址(MAC)列表
procedure NBGetMac(Strings: TStrings);
//取第一个非零的网卡地址
function NBGetFirstMac: String;
//判断指定网卡地址是否存在
function NBIsMacInList(Mac: String): Boolean;

procedure NBGetMac(Strings: TStrings);
function HexBL(by: Byte): String;
begin
Result := Format('%x', [by]);
if Length(Result) < 2 then
Result := '0' + Result;
end;
var
NCB: TNCB;
Adapter: TAdapterStatus;
LanaEnum: TLanaEnum;
I, J: Integer;
Str: String;
begin
Strings.Clear;
ZeroMemory(@NCB,SizeOf(NCB));

NCB.ncb_command := Chr(NCBENUM);
NetBios(@NCB);

NCB.ncb_buffer := @LanaEnum;
NCB.ncb_length := SizeOf(LanaEnum);
NCB.ncb_command := Chr(NCBENUM);
NetBios(@NCB);

for I := 0 to Ord(LanaEnum.length) - 1 do
begin
ZeroMemory(@NCB,SizeOf(NCB));
NCB.ncb_command := Chr(NCBRESET);
NCB.ncb_lana_num := LanaEnum.lana;
NetBios(@NCB);

ZeroMemory(@NCB,SizeOf(NCB));
NCB.ncb_command := Chr(NCBASTAT);
NCB.ncb_lana_num := LanaEnum.lana;
StrPCopy(NCB.ncb_callname,'*');
NCB.ncb_buffer := @Adapter;
NCB.ncb_length := SizeOf(Adapter);
NetBios(@NCB);

Str := '';
for J := 0 to 5 do
begin
if J > 0 then Str := Str + '-';
Str := Str + HexBL(Byte(Adapter.adapter_address[J]));
end;
Strings.Add(Str);
end;
end;

function NBGetFirstMac: String;
var
Strings: TStringList;
I, J: Integer;
begin
Result := '';
Strings := TStringList.Create;
try
NBGetMac(Strings);
for I := 0 to Strings.Count - 1 do
begin
for J := 1 to Length(Strings.Strings) do
if (Strings.Strings[J] <> '0') and
(Strings.Strings[J] <> '-') then
begin
Result := Strings.Strings;
Break;
end;
if Result <> '' then Break;
end;
finally
Strings.Free;
end;
end;

function NBIsMacInList(Mac: String): Boolean;
var
Strings: TStringList;
I: Integer;
begin
Result := False;
Strings := TStringList.Create;
try
NBGetMac(Strings);
for I := 0 to Strings.Count - 1 do
begin
if Strings.Strings = Mac then
begin
Result := True;
Break;
end;
end;
finally
Strings.Free;
end;
end;
 
我想把所有能得到的局域网内的mac都列到一个memo里
能不能给个完整的代码。
上面我也有试过,可是出现很多错误啊!!
[Error] Unit1.pas(24): Undeclared identifier: 'TNCB'
[Error] Unit1.pas(25): Undeclared identifier: 'TAdapterStatus'
[Error] Unit1.pas(26): Undeclared identifier: 'TLanaEnum'
[Error] Unit1.pas(70): Missing operator or semicolon
[Error] Unit1.pas(70): Undeclared identifier: 'NCBENUM'
[Error] Unit1.pas(71): Undeclared identifier: 'NetBios'
[Error] Unit1.pas(73): Missing operator or semicolon
[Error] Unit1.pas(74): Missing operator or semicolon
[Error] Unit1.pas(75): Missing operator or semicolon
[Warning] Unit1.pas(78): For loop control variable must be simple local variable
[Error] Unit1.pas(78): ')' expected but identifier 'length' found
[Error] Unit1.pas(81): Missing operator or semicolon
[Error] Unit1.pas(81): Undeclared identifier: 'NCBRESET'
[Error] Unit1.pas(82): Missing operator or semicolon
[Error] Unit1.pas(82): Missing operator or semicolon
[Error] Unit1.pas(86): Missing operator or semicolon
[Error] Unit1.pas(86): Undeclared identifier: 'NCBASTAT'
[Error] Unit1.pas(87): Missing operator or semicolon
[Error] Unit1.pas(87): Missing operator or semicolon
[Error] Unit1.pas(88): Not enough actual parameters
[Error] Unit1.pas(88): 'END' expected but ',' found
麻烦你了!!
 
把下面这个单元加到你的工程里面

代码是我从全文检索里面找出来的,如果再出问题,你自己也动手去去找找

unit NetProcs;

interface

uses Classes, Windows;

type
TAdapterStatus = record
adapter_address: array [0..5] of Char;
filler: array [1..4*SizeOf(Char)+19*SizeOf(Word)
+3*SizeOf(DWORD)] of Byte;
end;
THostInfo = record
username: PWideChar;
logon_domain: PWideChar;
oth_domains: PWideChar;
logon_server: PWideChar;
end;{record}

function IsNetConnect : Boolean;

function AdapterToString(Adapter: TAdapterStatus): string;

function GetMacAddresses(const Machine: string;
const Addresses: TStrings): Integer;


function GetNetUser(HostName: WideString): THostInfo;

implementation

uses NB30, SysUtils;

function IsNetConnect : Boolean;
begin
if GetSystemMetrics(SM_NETWORK) AND $01 = $01
then Result:= True
else Result:= False;
end;{function}

function AdapterToString(Adapter: TAdapterStatus): string;
begin
with Adapter do Result :=
Format('%2.2x-%2.2x-%2.2x-%2.2x-%2.2x-%2.2x', [
Integer(adapter_address[0]), Integer(adapter_address[1]),
Integer(adapter_address[2]), Integer(adapter_address[3]),
Integer(adapter_address[4]), Integer(adapter_address[5])]);
end;{function}

function GetMacAddresses(const Machine: string;
const Addresses: TStrings): Integer;
const NCBNAMSZ = 16; // absolute length of a net name
MAX_LANA = 254; // lana's in range 0 to MAX_LANA inclusive
NRC_GOODRET = $00; // good return
NCBASTAT = $33; // NCB ADAPTER STATUS
NCBRESET = $32; // NCB RESET
NCBENUM = $37; // NCB ENUMERATE LANA NUMBERS
type
PNCB = ^TNCB;
TNCBPostProc = procedure (P: PNCB); stdcall;
TNCB = record
ncb_command: Byte;
ncb_retcode: Byte;
ncb_lsn: Byte;
ncb_num: Byte;
ncb_buffer: PChar;
ncb_length: Word;
ncb_callname: array [0..NCBNAMSZ - 1] of Char;
ncb_name: array [0..NCBNAMSZ - 1] of Char;
ncb_rto: Byte;
ncb_sto: Byte;
ncb_post: TNCBPostProc;
ncb_lana_num: Byte;
ncb_cmd_cplt: Byte;
ncb_reserve: array [0..9] of Char;
ncb_event: THandle;
end;
PLanaEnum = ^TLanaEnum;
TLanaEnum = record
length: Byte;
lana: array [0..MAX_LANA] of Byte;
end;
ASTAT = record
adapt: TAdapterStatus;
namebuf: array [0..29] of TNameBuffer;
end;
var
NCB: TNCB;
Enum: TLanaEnum;
I: Integer;
Adapter: ASTAT;
MachineName: string;
begin
Result := -1;
Addresses.Clear;
MachineName := UpperCase(Machine);
if MachineName = ''
then MachineName := '*';
FillChar(NCB, SizeOf(NCB), #0);
NCB.ncb_command := NCBENUM;
NCB.ncb_buffer := Pointer(@Enum);
NCB.ncb_length := SizeOf(Enum);
if Word(NetBios(@NCB)) = NRC_GOODRET
then begin
Result := Enum.Length;
for I := 0 to Ord(Enum.Length) - 1
do begin
FillChar(NCB, SizeOf(TNCB), #0);
NCB.ncb_command := NCBRESET;
NCB.ncb_lana_num := Enum.lana;
if Word(NetBios(@NCB)) = NRC_GOODRET
then begin
FillChar(NCB, SizeOf(TNCB), #0);
NCB.ncb_command := NCBASTAT;
NCB.ncb_lana_num := Enum.lana;
StrLCopy(NCB.ncb_callname, PChar(MachineName),NCBNAMSZ);
StrPCopy(@NCB.ncb_callname[Length(MachineName)],
StringOfChar(' ', NCBNAMSZ - Length(MachineName)));
NCB.ncb_buffer := PChar(@Adapter);
NCB.ncb_length := SizeOf(Adapter);
if Word(NetBios(@NCB)) = NRC_GOODRET
then Addresses.Add(AdapterToString(Adapter.adapt));
end;
end;
end;
end;{function}

function
NetWkstaUserEnum(servername: PWideChar;
level : DWord;
var bufptr: Pointer;
prefmaxlen: DWord;
var entriesread: PDWord;
var totalentries: PDWord;
var resumehandle: PDWord ): LongInt ;
stdcall; external 'netapi32.dll' name 'NetWkstaUserEnum';

function GetNetUser(HostName: WideString): THostInfo;
var
Info: Pointer;
ElTotal: PDWord;
ElCount: PDWord;
Resume: PDWord;
Error: LongInt;
begin
Resume:=0;
NetWkstaUserEnum(PWideChar(HostName),1, Info,0,
ElCount,ElTotal,Resume);
Error:=NetWkstaUserEnum(PWideChar(HostName),1,Info,256*Integer(ElTotal),
ElCount,ElTotal,Resume);
case Error of
ERROR_ACCESS_DENIED: Result.UserName:= 'ERROR - ACCESS DENIED';
ERROR_MORE_DATA: Result.UserName:= 'ERROR - MORE DATA';
ERROR_INVALID_LEVEL: Result.UserName:= 'ERROR - INVALID LEVEL';
else if Info <> nil
then Result:=THostInfo(info^)
else begin
Result.username:= '???';
Result.logon_domain:= '???';
Result.oth_domains:= '???';
Result.logon_server:= '???';
end;{if}
end;{case}
end; {function}

end.
 
谢谢了!!,给分
 

Similar threads

D
回复
0
查看
752
DelphiTeacher的专栏
D
D
回复
0
查看
658
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部