如何通过delphi查询到本机的IP地址,或是网卡址?

解决方案 »

  1.   

    indy misc里面有个IdIPWatchprocedure TForm1.Button1Click(Sender: TObject);
    begin
        IdIPWatch1.Active:=true;
        Edit1.text:=IdIPWatch1.LocalIP;
        IdIPWatch1.Active:=false;
    end;
      

  2.   

    uses WinSock;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:=StrPas(inet_ntoa(pptr^[I]^));
        Inc(I);
      end;
      WSACleanup;
    end;
      

  3.   

    unit client;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls ,nb30,WinSock,ScktComp, Sockets, IdBaseComponent,
      IdComponent, IdTCPServer, IdTCPConnection, IdTCPClient, SkinData,math,
      DynamicSkinForm, ExtCtrls, ComCtrls,inifiles;Const
    MP_QUERY ='11111';
    MP_REFUSE ='22222';
    MP_ACCEPT ='33333';
    MP_NEXTWILLBEDATA='44444';
    MP_DATA ='55555';
    MP_ABORT ='66666';
    MP_OVER ='77777';
    MP_CHAT ='88888';
    MP_END='99999';
    MP_FILEPROPERTY='00000';
    iBYTEPERSEND=1024;type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        Edit2: TEdit;
        Edit3: TEdit;    procedure Button1Click(Sender: TObject);    function  GetHostName:String;
        function  NameToIP(Name:string):String;  private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}function TForm1.GetHostName:String;
    var
      ComputerName: array[0..MAX_COMPUTERNAME_LENGTH+1] of char;
      Size: Cardinal;
    begin
      result:='';
      Size := MAX_COMPUTERNAME_LENGTH+1;
      GetComputerName(ComputerName, Size);
      Result:=StrPas(ComputerName);
    end;function TForm1.NameToIP(Name:string):String;
    var
      WSAData: TWSAData;
      HostEnt: PHostEnt;
    begin
      result:='';
      WSAStartup(2, WSAData);
      HostEnt := GetHostByName(PChar(Name));
     if HostEnt <> nil then
      begin
        with HostEnt^ do
         result:= Format('%d.%d.%d.%d',[Byte(h_addr^[0]), Byte(h_addr^[1]),
                                    Byte(h_addr^[2]), Byte(h_addr^[3])]);
      end;
      WSACleanup;
    end;
    function MacAddress: string;
    var
      Lib: Cardinal;
      Func: function(GUID: PGUID): Longint; stdcall;
      GUID1, GUID2: TGUID;begin
      Result :=' ';
      Lib := LoadLibrary('rpcrt4.dll');
      if Lib <> 0 then 
      begin
        if Win32Platform <>VER_PLATFORM_WIN32_NT then 
          @Func := GetProcAddress(Lib, 'UuidCreate')
          else @Func := GetProcAddress(Lib, 'UuidCreateSequential'); 
        if Assigned(Func) then
        begin 
          if (Func(@GUID1) = 0) and
            (Func(@GUID2) = 0) and 
            (GUID1.D4[2] = GUID2.D4[2]) and
            (GUID1.D4[3] = GUID2.D4[3]) and 
            (GUID1.D4[4] = GUID2.D4[4]) and
            (GUID1.D4[5] = GUID2.D4[5]) and 
            (GUID1.D4[6] = GUID2.D4[6]) and
            (GUID1.D4[7] = GUID2.D4[7]) then
          begin
            Result :=
             IntToHex(GUID1.D4[2], 2) + '' +
             IntToHex(GUID1.D4[3], 2) + '' +
             IntToHex(GUID1.D4[4], 2) + '' +
             IntToHex(GUID1.D4[5], 2) + '' +
             IntToHex(GUID1.D4[6], 2) + '' +
             IntToHex(GUID1.D4[7], 2);
          end;
        end;
        FreeLibrary(Lib);
      end;
    end;