请问如何根据机器名得到机器的ip地址?
解决方案 »
- 请问 DELPHI 2007 用来开发 BS 结构的程序好吗,能开发APSX 的程序吗?
- 界面交叉引用出问题了,搜索遍csdn,delphibbs还解决不了。
- 本人今天實現了動態增加子菜單,現公布源碼
- class function 或class procedure 是什么意思?
- 1500-2500 RMB求一功能实现,高手一日可搞掂
- 如何获得硬盘序号,主板号
- 大家觉得什么样子的界面漂亮一点啊,最好能提供实例说明
- ■怎样得到系统键盘输入的内容?■
- delphi操作Excel,大家来帮忙!
- 没分了,但是还有很多的问题,是个关于数据库的!
- 请问大家哪里能找到object pascal的中文文档,谢谢!
- 感谢[email protected],来接分!!
LPGUID lpServiceType, // the type of the service
LPTSTR lpServiceName, // the name of the service
LPINT lpiProtocols, // points to array of protocol identifiers
DWORD dwResolution, // set of bit flags that specify aspects of name resolution
LPSERVICE_ASYNC_INFO lpServiceAsyncInfo, // reserved for future use, must be NULL
LPVOID lpCsaddrBuffer, // points to buffer to receive address information
LPDWORD lpdwBufferLength, // points to variable with address buffer size information
LPTSTR lpAliasBuffer, // points to buffer to receive alias information
LPDWORD lpdwAliasBufferLength // points to variable with alias buffer size information
);
var
WSAData:TWSAData;
begin
if (wsastartup(MAKEWORD(2,0),WSADATA)<>0 ) THEN
begin
raise exception.Create('Socket version error!');
end;
end;procedure TForm2.FormDestroy(Sender: TObject);
begin
wsacleanup;
end;procedure TForm2.Button1Click(Sender: TObject);
var
host:phostent;
result:string;
begin
try
host:=GETHOSTBYNAME('newry');
EXCEPT
SHOWMESSAGE('ERROR!');
EXIT;
END;
edit1.text:=host.h_name;
RESULT:=COPY(HOST.H_ADDR^,0,4);edit1.text:=format('%d.%d.%d.%d',
[ord(result[1]),
ord(result[2]),
ord(result[3]),
ord(result[5])]);end;
...
type
TAddrList = array[0..5] of PInAddr;
PAddrList = ^TAddrList;
var
AName: array[0..255] of Char;
ASize: Cardinal;
AHost: PHostEnt;
Addrs: PAddrList;
WSA: WSAData;
I: Integer;
IP: String;
begin
ASize := 0;
FillChar(AName, 255, 0);
WSAStartup(MakeWord(1, 1), WSA);
try
gethostname(AName, ASize); // 取当前机器名
AHost := gethostbyname(AName);
Addrs := Pointer(AHost.h_addr_list); // h_addr_list指向ip表
I := 0;
while Addrs[I] <> nil do // 可能不止一个ip
begin
IP := String(inet_ntoa(Addrs^[I]^)); // 转换成字符串形式的ip
//Memo1.Add(IP);
Inc(I);
end;
finally;
WSACleanup;
end;
end;
var Ip,IpStr:string; ch:array[1..32]of char; i:integer;
WSData:TWSAData; MyHost:PHostEnt;
begin
if WSAstartup(2,WSData)<>0 then
begin
ShowMessage('没有成功返回!');
Halt(2);
end;
try
if getHostName(@ch[1],32)<>0 then
begin
ShowMessage('没有成功返回!');
Halt(3);
end;
except
ShowMessage('没有成功返回!');
Halt(3);
end;
MyHost:=GetHostByName(@ch[1]);
if MyHost=NIL then
begin
ShowMessage('没有成功返回!');
Halt(4);
end
else
begin
for i:=1 to 4 do
begin
Ip:=inttostr(Ord(MyHost.h_addr^[i-1]));
ShowMessage('IP分段地址为:'+Ip);
IPStr:=IPStr+Ip;
if i<4 then IPStr:=IPStr+'.'
else ShowMessage('IP地址为:'+IPStr);
end;
end;
end;