유돌이

calendar

1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

Notice

2019. 10. 4. 09:39 델파이

// IPHLPAPI.DLL 의 Delphi 헤더파일은 JEDI 에서 받을 수 있습니다
//   ftp://delphi-jedi.org/api/IPHlpAPI.zip

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Winsock, IpHlpApi, IPTypes, IpIfConst;

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}

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;
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;
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;
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;

  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;
end;

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

end.

posted by 유돌이