Delphi XE4 - 在状态栏中获取您的本地IP

时间:2013-09-12 02:44:02

标签: delphi-xe4

看过这里发布的大多数示例,但似乎没有一个在XE4上运行。我想在状态栏中显示我的IP。

AdvOfficeStatusBar1.Panels[0].Text := GetIP;

我在这里看到的功能大多在xe4上失败,因为它们是用较旧的delphi版本编写的。我想获得本地IP,而不是路由器背后的IP。好吧,为了学习,知道那个也是有用的......:)

我试过了:

function getIP: 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; 

但它不会起作用....许多其他人......似乎我错过了什么,但是什么?

也试过这个:

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; 

不会停止工作:(GetHostName(Buffer,SizeOf(Buffer));

然而这可行! :

Function GetIPAddress: String;
type pu_long = ^u_long;
var varTWSAData : TWSAData;
varPHostEnt : PHostEnt;
varTInAddr : TInAddr;
namebuf : Array[0..255] of ansichar;
begin
try
try
If WSAStartup($101,varTWSAData) <> 0 Then
Result := ''
Else Begin
gethostname(namebuf,sizeof(namebuf));
varPHostEnt := gethostbyname(namebuf);
varTInAddr.S_addr := u_long(pu_long(varPHostEnt^.h_addr_list^)^);
Result := inet_ntoa(varTInAddr);
End;
except
Result := '';
end;
finally
WSACleanup;
end;
end;

1 个答案:

答案 0 :(得分:0)

<?php
    $msg = $_POST['msg'];
    $client = $_POST['client'];

    require_once("TeamSpeak3/libraries/TeamSpeak3/TeamSpeak3.php");

    $ts3_VirtualServer = TeamSpeak3::factory("serverquery://serveradmin:PASSWORD@IPADDRESS:PORT/?server_port=9987&nickname=USG-Staff");

    $ts3_VirtualServer->clientGetByName("$client")->poke("$msg");
?>