My Study/Delphi

델파이 NIC 통신상태 구해오는 소스

Kduks 2009. 1. 15. 14:49
반응형


IPHelper 를 이용한 NIC 상태 구해오는 소스입니다!


unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;

    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

const
  MAX_INTERFACE_NAME_LEN             = $100;
  ERROR_SUCCESS                      = 0;
  MAXLEN_IFDESCR                     = $100;
  MAXLEN_PHYSADDR                    = 8;

  MIB_IF_OPER_STATUS_NON_OPERATIONAL = 0 ;
  MIB_IF_OPER_STATUS_UNREACHABLE     = 1;
  MIB_IF_OPER_STATUS_DISCONNECTED    = 2;
  MIB_IF_OPER_STATUS_CONNECTING      = 3;
  MIB_IF_OPER_STATUS_CONNECTED       = 4;
  MIB_IF_OPER_STATUS_OPERATIONAL     = 5;

  MIB_IF_TYPE_OTHER                  = 1;
  MIB_IF_TYPE_ETHERNET               = 6;
  MIB_IF_TYPE_TOKENRING              = 9;
  MIB_IF_TYPE_FDDI                   = 15;
  MIB_IF_TYPE_PPP                    = 23;
  MIB_IF_TYPE_LOOPBACK               = 24;
  MIB_IF_TYPE_SLip                   = 28;

  MIB_IF_ADMIN_STATUS_UP             = 1;
  MIB_IF_ADMIN_STATUS_DOWN           = 2;
  MIB_IF_ADMIN_STATUS_TESTING        = 3;

type
  MIB_IFROW = record
    wszName           : array[0..(MAX_INTERFACE_NAME_LEN * 2-1)] of char;
    dwIndex           : LongInt;
    dwType            : LongInt;  // type of interface 
    dwMtu             : LongInt;  // max transmission unit
    dwSpeed           : LongInt;  // speed of the interface
    dwPhysAddrLen     : LongInt;  // length of physical address 
    bPhysAddr         : array[0..(MAXLEN_PHYSADDR - 1)] of Byte;   // physical address of adapter
    dwAdminStatus     : LongInt;  // administrative status 
    dwOperStatus      : LongInt;  // operational status 
    dwLastChange      : LongInt;  // last time operational status changed 
    dwInOctets        : LongInt;  // octets received 
    dwInUcastPkts     : LongInt;  // unicast packets received 
    dwInNUcastPkts    : LongInt;  // non-unicast packets received 
    dwInDiscards      : LongInt;  // received packets discarded 
    dwInErrors        : LongInt;  // erroneous packets received 
    dwInUnknownProtos : LongInt;  // unknown protocol packets received 
    dwOutOctets       : LongInt;  // octets sent 
    dwOutUcastPkts    : LongInt;  // unicast packets sent 
    dwOutNUcastPkts   : LongInt;  // non-unicast packets sent 
    dwOutDiscards     : LongInt;  // outgoing packets discarded 
    dwOutErrors       : LongInt;  // erroneous packets sent 
    dwOutQLen         : LongInt;  // output queue length 
    dwDescrLen        : LongInt;  // length of bDescr member 
    bDescr            : array[0..(MAXLEN_IFDESCR - 1)] of char;  // interface description 
  end;

  MIB_IFTABLE = record
    dwNumEntries: DWORD;
    table       : array[0..(MAXLEN_PHYSADDR-1)] of MIB_IFROW;
  end;
  PMIB_IFTABLE = ^MIB_IFTABLE;

  function GetIfTable(pIfTable: Pointer; var pdwSize: LongInt; bOrder: LongInt): LongInt;  stdcall;

  function GetIfTable; stdcall; external 'ipHLPAPI.DLL';

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  d: Integer;
  IfTable: PMIB_IFTABLE;
  i: integer;
  Buf: string;
begin
  d := 0;
  GetIfTable(nil, d, 0);
  IfTable := SysGetMem(d);

  if Assigned(IfTable) then
  begin
    if GetIfTable(IfTable, d, 0) = NO_ERROR then
    begin
      for i := 0 to IfTable.dwNumEntries - 1 do
      begin
        Memo1.Lines.Add('');

        Buf := Format('dwIndex           0x%8.8x', [IfTable.table[i].dwIndex]);
        Memo1.Lines.Add(Buf);

        case IfTable.table[i].dwType of
        MIB_IF_TYPE_OTHER    : Buf := 'dwType            MIB_IF_TYPE_OTHER';
        MIB_IF_TYPE_ETHERNET : Buf := 'dwType            MIB_IF_TYPE_ETHERNET';
        MIB_IF_TYPE_TOKENRING: Buf := 'dwType            MIB_IF_TYPE_TOKENRING';
        MIB_IF_TYPE_FDDI     : Buf := 'dwType            MIB_IF_TYPE_FDDI';
        MIB_IF_TYPE_PPP      : Buf := 'dwType            MIB_IF_TYPE_PPP';
        MIB_IF_TYPE_LOOPBACK : Buf := 'dwType            MIB_IF_TYPE_LOOPBACK';
        MIB_IF_TYPE_SLIP     : Buf := 'dwType            MIB_IF_TYPE_SLip';
        end;
        Memo1.Lines.Add(Buf);

        Buf := Format('dwMtu             %d', [IfTable.table[i].dwMtu]);
        Memo1.Lines.Add(Buf);

        Buf := Format('dwSpeed           %d', [IfTable.table[i].dwSpeed]);
        Memo1.Lines.Add(Buf);

        Buf := Format('dwPhysAddrLen     %d', [IfTable.table[i].dwPhysAddrLen]);
        Memo1.Lines.Add(Buf);

        Buf := Format('bPhysAddr         %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x', [
              IfTable.table[i].bPhysAddr[0]
            , IfTable.table[i].bPhysAddr[1]
            , IfTable.table[i].bPhysAddr[2]
            , IfTable.table[i].bPhysAddr[3]
            , IfTable.table[i].bPhysAddr[4]
            , IfTable.table[i].bPhysAddr[5]
            ]);
        Memo1.Lines.Add(Buf);

        Buf := Format('dwAdminStatus     %d', [IfTable.table[i].dwAdminStatus]);
        Memo1.Lines.Add(Buf);

        Buf := Format('dwOperStatus      %d', [IfTable.table[i].dwOperStatus]);
        Memo1.Lines.Add(Buf);

        Buf := Format('dwLastChange      %d', [IfTable.table[i].dwLastChange]);
        Memo1.Lines.Add(Buf);

        Buf := Format('dwInOctets        %d', [IfTable.table[i].dwInOctets]);
        Memo1.Lines.Add(Buf);

        Buf := Format('dwInUcastPkts     %d', [IfTable.table[i].dwInUcastPkts]);
        Memo1.Lines.Add(Buf);

        Buf := Format('dwInNUcastPkts    %d', [IfTable.table[i].dwInNUcastPkts]);
        Memo1.Lines.Add(Buf);

        Buf := Format('dwInDiscards      %d', [IfTable.table[i].dwInDiscards]);
        Memo1.Lines.Add(Buf);

        Buf := Format('dwInErrors        %d', [IfTable.table[i].dwInErrors]);
        Memo1.Lines.Add(Buf);

        Buf := Format('dwInUnknownProtos %d', [IfTable.table[i].dwInUnknownProtos]);
        Memo1.Lines.Add(Buf);

        Buf := Format('dwOutOctets       %d', [IfTable.table[i].dwOutOctets]);
        Memo1.Lines.Add(Buf);

        Buf := Format('dwOutUcastPkts    %d', [IfTable.table[i].dwOutUcastPkts]);
        Memo1.Lines.Add(Buf);

        Buf := Format('dwOutNUcastPkts   %d', [IfTable.table[i].dwOutNUcastPkts]);
        Memo1.Lines.Add(Buf);

        Buf := Format('dwOutDiscards     %d', [IfTable.table[i].dwOutDiscards]);
        Memo1.Lines.Add(Buf);

        Buf := Format('dwOutErrors       %d', [IfTable.table[i].dwOutErrors]);
        Memo1.Lines.Add(Buf);

        Buf := Format('dwOutQLen         %d', [IfTable.table[i].dwOutQLen]);
        Memo1.Lines.Add(Buf);

        Buf := Format('dwDescrLen        %d', [IfTable.table[i].dwDescrLen]);
        Memo1.Lines.Add(Buf);

        IfTable.table[i].bDescr[IfTable.table[i].dwDescrLen] := #0;
        Buf := Format('bDescr            %s', [IfTable.table[i].bDescr]);
        Memo1.Lines.Add(Buf);
      end;
    end;
    SysFreeMem(IfTable);
  end;
end;

end.




참 좋아요!

출처 -- 김영대님의 Howto


반응형