this slowpoke moves

Get Local Network IP

Beispiel 1 :
uses Winsock

//

procedure TForm1.Button1Click(Sender: TObject);
resourcestring
 cTxtIP = '%d.%d.%d.%d';
var rSockVer : WordRec; aWSAData : TWSAData;
    szHostName : array[0..255] of Char; pHE : PHostEnt; sIP : String;
begin
  rSockVer.Hi := 1;
  rSockVer.Lo := 1;
  WSAStartup(Word(rSockVer), aWSAData );
   try
     FillChar(szHostName, SizeOf(szHostName), #0);
     GetHostName(szHostName, SizeOf(szHostName));
     pHE := GetHostByName(szHostName);
     if Assigned(pHE) then with pHE^ do
      sIP := Format(cTxtIP,[Byte(h_addr^[0]), Byte(h_addr^[1]),
                    Byte(h_addr^[2]), Byte(h_addr^[3])]);
   finally
   WSACleanup;
  end;
  ShowMessage(sIP);
end;
Beispiel 2 :
uses Winsock

//

function getIPs: Tstrings;
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 := TstringList.Create;
 Result.Clear;
 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.Add(inet_ntoa(pptr^[I]^));
 Inc(I);
 end;
 WSACleanup;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Memo1.Lines := GetIps;
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate