关于如何取终端IP地址的问题(100分)

  • 主题发起人 主题发起人 oyxb1
  • 开始时间 开始时间
O

oyxb1

Unregistered / Unconfirmed
GUEST, unregistred user!
我想在终端上运行程序取得终端上的ip地址,但是得到的是终端服务器的ip,我的取本地ip
地址的函数如下:
function GetLocalIP(var LocalIp: string): Boolean;//获得IP地址函数
var
HostEnt: PHostEnt;
Ip: string;
addr: pchar;
Buffer: array [0..63] of char;
GInitData: TWSADATA;
begin
Result := False;
try
WSAStartup(2, GInitData);
GetHostName(Buffer, SizeOf(Buffer));
HostEnt := GetHostByName(buffer);
if HostEnt = nil then Exit;
addr := HostEnt^.h_addr_list^;
ip := Format('%d.%d.%d.%d', [byte(addr [0]),
byte (addr [1]), byte (addr [2]), byte (addr [3])]);
localip := Ip;
Result := True;
finally
WSACleanup;
end;
end;

请问如何取到终端本地的ip,请高手不吝赐教,谢谢!
 
怎么没有人知道吗?自己顶。。。
 
我有点搞不明白终端上的ip地址,但是得到的是终端服务器的ip
他们有什么区别吗?
 
终端机是有自己的ip地址的,比如说终端机A的ip地址是192.168.0.5,服务器的ip地址是
192.168.0.1,那么我在终端机上通过function GetLocalIP得到的是服务器的地址192.168.0.1,而不是我要的终端机的地址192.168.0.5,不知我说明白没有
 
你的函数怎么读不到IP ?
 
虽然不懂,顶一下
 
◇[DELPHI]解析服务器IP地址
uses winsock
function IPAddrToName(IPAddr : String): String;
var
SockAddrIn: TSockAddrIn;
HostEnt: PHostEnt;
WSAData: TWSAData;
begin
WSAStartup($101, WSAData);
SockAddrIn.sin_addr.s_addr:= inet_addr(PChar(IPAddr));
HostEnt:= gethostbyaddr(@SockAddrIn.sin_addr.S_addr, 4, AF_INET);
if HostEnt<>nil then result:=StrPas(Hostent^.h_name) else result:='';
end;
◇[DELPHI]获取网上邻居
procedure getnethood();//NT做服务器,WIN98上调试通过。
var
a,i:integer;
errcode:integer;
netres:array[0..1023] of netresource;
enumhandle:thandle;
enumentries:dword;
buffersize:dword;
s:string;
mylistitems:tlistitems;
mylistitem:tlistitem;
alldomain:tstrings;
begin //listcomputer is a listview to list all computers;controlcenter is a form.
alldomain:=tstringlist.Create ;
with netres[0] do begin
dwscope :=RESOURCE_GLOBALNET;
dwtype :=RESOURCETYPE_ANY;
dwdisplaytype :=RESOURCEDISPLAYTYPE_DOMAIN;
dwusage :=RESOURCEUSAGE_CONTAINER;
lplocalname :=nil;
lpremotename :=nil;
lpcomment :=nil;
lpprovider :=nil;
end; // 获取所有的域
errcode:=wnetopenenum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,RESOURCEUSAGE_CONTAINER,@netres[0],enumhandle);
if errcode=NO_ERROR then begin
enumentries:=1024;
buffersize:=sizeof(netres);
errcode:=wnetenumresource(enumhandle,enumentries,@netres[0],buffersize);
end;
a:=0;
mylistitems :=controlcenter.lstcomputer.Items ;
mylistitems.Clear ;
while (string(netres[a].lpprovider)<>'') and (errcode=NO_ERROR) do
begin
alldomain.Add (netres[a].lpremotename);
a:=a+1;
end;
wnetcloseenum(enumhandle);
// 获取所有的计算机
mylistitems :=controlcenter.lstcomputer.Items ;
mylistitems.Clear ;
for i:=0 to alldomain.Count-1 do
begin
with netres[0] do begin
dwscope :=RESOURCE_GLOBALNET;
dwtype :=RESOURCETYPE_ANY;
dwdisplaytype :=RESOURCEDISPLAYTYPE_SERVER;
dwusage :=RESOURCEUSAGE_CONTAINER;
lplocalname :=nil;
lpremotename :=pchar(alldomain);
lpcomment :=nil;
lpprovider :=nil;
end;
ErrCode:=WNetOpenEnum(RESOURCE_GLOBALNET,RESOURCETYPE_ANY,RESOURCEUSAGE_CONTAINER,@netres[0],EnumHandle);
if errcode=NO_ERROR then
begin
EnumEntries:=1024;
BufferSize:=SizeOf(NetRes);
ErrCode:=WNetEnumResource(EnumHandle,EnumEntries,@NetRes[0],BufferSize);
end;
a:=0;
while (string(netres[a].lpprovider)<>'') and (errcode=NO_ERROR) do
begin
mylistitem :=mylistitems.Add ;
mylistitem.ImageIndex :=0;
mylistitem.Caption :=uppercase(stringreplace(string(NetRes[a].lpremotename),'//','',[rfReplaceAll]));
a:=a+1;
end;
wnetcloseenum(enumhandle);
end;
end;

你研究一下上面的两个函数,结合起来是不是能完成你的要求。
 
沒取過終端機的ip
因為使用的系統是服務器端的
你的程式也是在服務端運行的
 
得到服务器端IP应该是正确的。
至于如何得到终端机的ip,要看这个总端机运行的软件。
具体怎么实现,关注中。。。。
 
问题还是没有解决,有没有高手知道啊,分不够可以再加
 
这个问题我上周刚好做过,如果用delphi需要自己导出wtsapi32.dll中WTSQuerySessionInformation函数。
wtsapi.pas
============
unit wtsapi;

interface
uses Windows;

const
WTS_CURRENT_SERVER: THandle = 0;
WTS_CURRENT_SERVER_HANDLE: THandle = 0;
WTS_CURRENT_SERVER_NAME = nil;
WTS_CURRENT_SESSION: integer = -1;
type
_WTS_CLIENT_ADDRESS = record
AddressFamily: LongInt;
AddRess: array[0..20 - 1] of BYTE;
end {_WTS_CLIENT_ADDRESS};
WTS_CLIENT_ADDRESS = _WTS_CLIENT_ADDRESS;
PWTSClientAddress = ^_WTS_CLIENT_ADDRESS;

type
_WTS_INFO_CLASS = (
WTSInitialProgram,
WTSApplicationName,
WTSWorkingDirectory,
WTSOEMId,
WTSSessionId,
WTSUserName,
WTSWinStationName,
WTSDomainName,
WTSConnectState,
WTSClientBuildNumber,
WTSClientName,
WTSClientDirectory,
WTSClientProductId,
WTSClientHardwareId,
WTSClientAddress,
WTSClientDisplay,
WTSClientProtocolType);
WTS_INFO_CLASS = _WTS_INFO_CLASS;

function WTSQuerySessionInformation(hServer: THandle;
SessionId: DWORD;
WTSInfoClass: WTS_INFO_CLASS;
var ppBuffer: PWTSClientAddress;
var pBytesReturned: DWORD): BOOL; stdcall; external 'wtsapi32.dll'
name 'WTSQuerySessionInformationA';
implementation
end.

调用示例:

unit test;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
uses wtsapi;

procedure TForm1.Button1Click(Sender: TObject);
var
i: DWORD;
buf: PWTSClientAddress;
j: integer;
Address,str: string;
begin
if WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTSClientAddress, buf, i) then
for j := 2 to 5 do
str := str + IntToStr(buf.AddRess[j]) + '.';
address := Copy(str, 1, Length(str) - 1);
showmessage(address);
end;

end.

 
给你个控件:

unit GetLocalIP;

interface

uses
Windows, Messages, SysUtils, Classes, WinSock;

type
TGetLocalIP = class(TComponent)
private
{ Private declarations }
FLocalIP:string;
protected
function GetLocIP : string; //获取本地机器IP地址
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property LocalIP:string read FLocalIP;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TGetLocalIP]);
end;
constructor TGetLocalIP.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FLocalIP:=GetLocIP;
end;

destructor TGetLocalIP.Destroy;
begin
inherited Destroy;
end;
function TGetLocalIP.GetLocIP : string; //获取本地机器IP地址
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
Result := '';
try
WSAStartup($101, GInitData);
GetHostName(Buffer, SizeOf(Buffer));
phe :=GetHostByName(buffer);
if phe = nil then Exit;
pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^ <> nil do
begin
result:=StrPas(inet_ntoa(pptr^^));
Inc(I);
end;
WSACleanup;
except
WSACleanup;
end;
end;

end.
 
谢谢大家参与!欢迎继续讨论
to easykey:
刚试过你的方法,可以得到一个不同于终端服务器的地址,但是所得到的地址却不是我所
分配的终端的静态IP地址,比如,某台终端我分配的静态ip是192.168.9.68 那么我用你的方法得到的ip是190.112.52.0 ,重新启动终端又得到另一个ip190.112.84.0,不知怎么回事
 
to 荷塘新月:
你的方法不行,和我自己的方法一样。
有没有高手知道啊?
 
问题还是没有解决,结贴了。
如果有能解决问题的,请继续讨论,可以再开贴加分(300分),决不食言。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部