求助:如何取得网卡的MAC和IP地址?(50分)

  • 主题发起人 主题发起人 whhtao
  • 开始时间 开始时间
W

whhtao

Unregistered / Unconfirmed
GUEST, unregistred user!
同标题。
 
function TForm1.NBGetAdapterAddress(a: integer): String;
//a指定多个网卡适配器中的哪一个0,1,2...
Var
NCB:TNCB; // Netbios control block file://NetBios控制块
ADAPTER : TADAPTERSTATUS; // Netbios adapter status//取网卡状态
LANAENUM : TLANAENUM; // Netbios lana
intIdx : Integer; // Temporary work value//临时变量
cRC : Char; // Netbios return code//NetBios返回值
strTemp : String; // Temporary string//临时变量

Begin
// Initialize
Result := '';
Try
// Zero control blocl
ZeroMemory(@NCB, SizeOf(NCB));
// Issue enum command
NCB.ncb_command:=Chr(NCBENUM);
cRC := NetBios(@NCB);
// Reissue enum command
NCB.ncb_buffer := @LANAENUM;
NCB.ncb_length := SizeOf(LANAENUM);
cRC := NetBios(@NCB);
If Ord(cRC)0 Then
exit;
// Reset adapter
ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command := Chr(NCBRESET);
NCB.ncb_lana_num := LANAENUM.lana[a];
cRC := NetBios(@NCB);
If Ord(cRC)0 Then
exit;
// Get adapter address
ZeroMemory(@NCB, SizeOf(NCB));
NCB.ncb_command := Chr(NCBASTAT);
NCB.ncb_lana_num := LANAENUM.lana[a];
StrPCopy(NCB.ncb_callname, '*');
NCB.ncb_buffer := @ADAPTER;
NCB.ncb_length := SizeOf(ADAPTER);
cRC := NetBios(@NCB);
// Convert it to string
strTemp := '';
For intIdx := 0 To 5 Do
strTemp := strTemp + InttoHex(Integer(ADAPTER.adapter_address[intIdx]),2);
Result := strTemp;
Finally
End;

最后别忘了uses nb30.pas
 
function LocalIP:string;

type

TaPInAddr = array [0..10] of PInAddr;

PaPInAddr = ^TaPInAddr;

var

phe : PHostEnt;

pptr : PaPInAddr;

Buffer : array [0..63] of char;

I : Integer;

GInitData : TWSADATA;


begin

WSAStartup($101, GInitData);

Result := '';

GetHostName(Buffer, SizeOf(Buffer));

phe :=GetHostByName(buffer);

if phe = nil then Exit;

pptr := PaPInAddr(Phe^.h_addr_list);

I := 0;

while pptr^ <> nil do begin

result:=StrPas(inet_ntoa(pptr^^));

Inc(I);

end;

WSACleanup;

end;

========================================

program get_ip;

uses

winsock,sysutils;

VAR

ch : ARRAY[1..32] OF Char;

i : Integer;

WSData: TWSAData;

MyHost: PHostEnt;

begin

IF WSAstartup(2,wsdata)<>0 THEN

BEGIN

Writeln('can''t start Winsock: Error ',WSAGetLastError);

Halt(2);

END;

try

IF getHostName(@ch[1],32)<>0 THEN

BEGIN

Writeln('getHostName failed');

Halt(3);

END;

except

Writeln('getHostName failed');

halt(3);

end;

MyHost:=GetHostByName(@ch[1]);

IF MyHost=NIL THEN

BEGIN

Writeln(GetHostName('+StrPas(@ch[1])+') failed : Error

'+IntToStr(WSAGetLastError));

Halt(4);

END

ELSE

BEGIN

Write('address ');

FOR i:=1 TO 4 DO

BEGIN

Write(Ord(MyHost.h_addr^[i-1]));

IF i<4 THEN

then write('.')

ELSE

writeln;

END;

END;

end.

 
多人接受答案了。
 
后退
顶部