中国开发网: 论坛: 程序员情感CBD: 贴子 392062
pcplayer: IP 地址,gateWay, 有关自己机器的IP信息等
unit NetInfo;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

uses
JwaIpHlpApi, JwaIPTypes, JwaIpIfCons;

function GetAdaterTypeName(Type_:Uint):string;
begin
case Type_ of
MIB_IF_TYPE_ETHERNET: begin
result:='Ethernet adapter';
end;
MIB_IF_TYPE_TOKENRING: begin
result:='oken Ring adapter';
end;
MIB_IF_TYPE_FDDI: begin
result:='FDDI adapter';
end;
MIB_IF_TYPE_PPP: begin
result:='PPP adapter';
end;
MIB_IF_TYPE_LOOPBACK: begin
result:='Loopback adapter';
end;
MIB_IF_TYPE_SLIP: begin
result:='Slip adapter';
end;
else begin
result:='Unknow Adapter type';
end;
end; // case
end;

function GetNodeTypeName(Type_:Uint):string;
begin
case Type_ of
1: result:='Broadcast';
2: result:='Peer to peer';
4: result:='Mixed';
8: result:='Hybrid';
else begin
result:='Unknown';
end;
end; // case
end;

function FormatMAC(pAdapt:PIP_ADAPTER_INFO):string;
var
i:Integer;
begin
result:='';
for i:=0 to pAdapt^.AddressLength-1 do begin
if (i = (pAdapt^.AddressLength - 1)) then begin
result:=result+Format('%.2X', [Integer(pAdapt^.Address[i])]);
end else begin
result:=result+Format('%.2X-', [Integer(pAdapt^.Address[i])]);
end;
end; // for
end;

function B2S(b:Boolean):String;
begin
if b then result:='Yes' else result:='No';
end;

Procedure IPConfig(Target:TStrings);
var
Err:DWORD;
pFixedInfo:PFIXED_INFO;
FixedInfoSize:DWORD;
pAdapterInfo, pAdapt:PIP_ADAPTER_INFO ;
AdapterInfoSize:DWORD;
pAddrStr:PIP_ADDR_STRING;
begin
// Get the main IP configuration information for this machine using a
FIXED_INFO structure
Err := GetNetworkParams(NIL, FixedInfoSize);
if (Err <> 0) then begin
if (Err <> ERROR_BUFFER_OVERFLOW) then begin
raise Exception.CreateFmt(
'GetNetworkParams sizing failed with error %d',
[Err]
);
end;
end;

// Allocate memory from sizing information
pFixedInfo := PFIXED_INFO(GlobalAlloc(GPTR, FixedInfoSize));
if not Assigned(pFixedInfo) then begin
raise Exception.Create(
'Memory allocation error'
);
end;

Err := GetNetworkParams(pFixedInfo, FixedInfoSize);
if (Err = 0) then begin
Target.Add(Format('Host Name . . . . . . . . . : %s',
[pFixedInfo^.HostName]));
Target.Add(Format('DNS Servers . . . . . . . . : %s',
[pFixedInfo^.DnsServerList.IpAddress.S]));
pAddrStr := pFixedInfo^.DnsServerList.Next;
while (pAddrStr<>NIL) do begin
Target.Add(Format('%s', [pAddrStr^.IpAddress.S]));
pAddrStr := pAddrStr^.Next;
end;

Target.Add('Node Type . . . . . . . . . :
'+GetNodeTypeName(pFixedInfo^.NodeType));

// if you really need it....
// printf("\tNetBIOS Scope ID. . . . . . : %s\n", pFixedInfo->ScopeId);
// printf("\tIP Routing Enabled. . . . . : %s\n",
(pFixedInfo->EnableRouting ? "yes" : "no"));
// printf("\tWINS Proxy Enabled. . . . . : %s\n",
(pFixedInfo->EnableProxy ? "yes" : "no"));
// printf("\tNetBIOS Resolution Uses DNS : %s\n", (pFixedInfo->EnableDns
?"yes" : "no"));
end; // if

//
// Enumerate all of the adapter specific information using the
IP_ADAPTER_INFO structure
// Note: IP_ADAPTER_INFO contains a linked list of adapter entries.
//
AdapterInfoSize := 0;
Err := GetAdaptersInfo(NIL, AdapterInfoSize);
if (Err <> 0) then begin
if (Err <> ERROR_BUFFER_OVERFLOW) then begin
raise Exception.CreateFmt(
'GetAdaptersInfo sizing failed with error %d',
[Err]
);
end;
end;

// Allocate memory from sizing information
pAdapterInfo := PIP_ADAPTER_INFO(GlobalAlloc(GPTR, AdapterInfoSize));
if not Assigned(pAdapterInfo) then begin
raise Exception.Create(
'Memory allocation error'
);
end;

// Get actual adapter information
Err := GetAdaptersInfo(pAdapterInfo, AdapterInfoSize);
if (Err <> 0) then begin
raise Exception.CreateFmt('GetAdaptersInfo failed with error %d',
[Err]);
end;

pAdapt := pAdapterInfo;
// loop adapters
while (pAdapt<>NIL) do begin
Target.Add(GetAdaterTypeName(pAdapt^.Type_));
Target.Add(pAdapt^.AdapterName);
Target.Add(Format('Description . . . . . . . . : %s',
[pAdapt^.Description]));
// MAC address
Target.Add('Physical Addresses (MAC). . . . : '+FormatMAC(pAdapt));
Target.Add('DHCP Enabled. . . . . . . . :
'+B2S(pAdapt^.DhcpEnabled<>0));

// list ip addresses
pAddrStr := @pAdapt^.IpAddressList;
while (pAddrStr<>NIL) do begin
Target.Add('IP Address. . . . . . . . . : '+pAddrStr^.IpAddress.S);
Target.Add('Subnet Mask . . . . . . . . : '+pAddrStr^.IpMask.S);
pAddrStr := pAddrStr^.Next;
end; // end

// list gateways
Target.Add('Default Gateway . . . . . . : '+
pAdapt^.GatewayList.IpAddress.S);
pAddrStr := pAdapt^.GatewayList.Next;
while(pAddrStr <>NIL) do begin
Target.Add(pAddrStr^.IpAddress.S);
pAddrStr := pAddrStr^.Next;
end; // while

// DCHP
Target.Add('DHCP Server . . . . . . . . : '+
pAdapt^.DhcpServer.IpAddress.S);

// WINS
Target.Add('Primary WINS Server . . . . : '+
pAdapt^.PrimaryWinsServer.IpAddress.S);
Target.Add('Secondary WINS Server . . . : '+
pAdapt^.SecondaryWinsServer.IpAddress.S);

(*

// if you really need it....
struct tm *newtime;

// Display coordinated universal time - GMT
newtime = gmtime(&pAdapt->LeaseObtained);
printf( "\tLease Obtained. . . . . . . : %s", asctime( newtime ) );

newtime = gmtime(&pAdapt->LeaseExpires);
printf( "\tLease Expires . . . . . . . : %s", asctime( newtime ) );
*)

// next adapter
pAdapt := pAdapt^.Next;
end; // while
end; // procedure

procedure TForm1.Button1Click(Sender: TObject);
begin
IPConfig(Memo1.Lines);
end;

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录