unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Button2: TButton;
    Memo1: TMemo;
    Button3: TButton;
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }

  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function   NBGetAdapterAddress(a:   integer):   String;
  //a指定多个网卡适配器中的哪一个0,1,2...  
  Var  
      NCB:TNCB;   //   Netbios   control   block   http://www.cnblogs.com/xiaoxingchi/admin/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
              begin
                  strTemp   :=   strTemp   +   InttoHex(Integer(ADAPTER.adapter_address[intIdx]),2);
                  if intIdx <> 5 then
                  strTemp   :=   strTemp + '-';
              end;
          Result   :=   strTemp;  
      Finally  
      End;  

end;

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^[i] <> nil do
  begin
    Result := inet_ntoa(pptr^[i]^);
    //form1.Memo1.Lines.Add(Result); //例出多重 IP
    Inc(i);
  end;
  WSACleanup;
end;


procedure TForm1.Button2Click(Sender: TObject);
begin
label4.Caption := LocalIP;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
label4.Caption := NBGetAdapterAddress(0);
end;

end.

----------------------------

 

posted on 2010-01-17 02:48  oKmAn.Org  阅读(282)  评论(0编辑  收藏  举报