这是以前收藏的一个:
wanglong0 (2003-01-18 13:56:00)
怎样在我的局域网里通过ip地址来得到计算机名???比如我想知道192.168.0.1的计算机名是什么?怎么实现呢?
远帆 (2003-01-09 14:41:00)
function TCusBSock.IPToHost(IP: Integer): string;
//将IP地址(int类型)转换为主机名
var
Host: PHostEnt;
begin
Host := gethostbyaddr(@ip, SizeOf(IP), AF_INET);
if (host = nil) then Exit; //Default Result := False
Result := StrPas(Host.h_name);
end;
lovewh119 (2003-01-09 14:43:00)
这是我以前写的,跟你的要求不太一样,不过能找到局域网里所计算机名及IP啦`~
我没时间写注解了,你自己慢慢看吧`~
Function GetServerList(var List:TStringList ):Boolean; //找工作组
var
NetResource:TNetResource;
Buf
ointer;
Count,BufSize,Res
WORD;
lphEnum:THandle;
p:TNetResourceArray;
i,j:Smallint;
NetworkTypeList:Tlist;
begin
result:=false;
NetworkTypelist:=Tlist.create;
List.clear;
Res:=WnetopenEnum(Resource_GLOBALNET,RESOURCETYPE_DISK,
RESOURCEUSAGE_CONTAINER,Nil,lphEnum);
if Res<>NO_ERROR Then exit;
Count:=$FFFFFFFF;
BufSize:=8192;
GetMem(Buf,BufSize);
Res:=WNETEnumResource(lphEnum,Count,Pointer(BUf),Bufsize);
if (Res=Error_no_more_items) or(Res<>NO_ERROR)
Then exit;
P:=TnetResourceArray(BUf);
for i:=0 to Count-1 do
begin
networktypelist.Add(p);
inc(p);
end;
Res:=WNetCloseEnum(lphEnum);
if Res<>NO_ERROR then exit;
For j:=0 to NetWorkTypeList.Count-1 do
begin
NetResource:=TnetResource(NetworkTypeList.Items[J]^);
Res:=WNetOpenEnum(Resource_Globalnet,Resourcetype_Disk,
Resourceusage_Container,@NetResource,lphEnum);
if Res<>NO_Error then break;
While true do
begin
count:=$FFFFFFFF;
BufSize:=8192;
GetMem(Buf,BUfSize);
Res:=WNetEnumResource(lphEnum,Count,Pointer(Buf),Bufsize);
if(Res=ERROR_NO_MORE_ITEMS) or (Res<>NO_ERROR)
then break;
P:=TNetResourceArray(Buf);
For I:=0 to Count-1 do
begin
List.add(StrPas(p^.lpRemoteName));
Inc(P);
end;
end;
Res:=WNetCloseEnum(lphEnum);
if Res<>NO_ERROR then break;
end;
Result:=true;
freeMem(Buf);
NetworkTypeList.Destroy;
End;
Function TForm1.GetUsers(GroupName:string;var List:TStringList):Boolean;
Var
NetResource:TNetResource;
Buf : Pointer;
Count,BufSize,Res : DWord;
Ind : Integer;
lphEnum : THandle;
Temp:TNetResourceArray;
Begin
Result := False;
List.Clear;
FillChar(NetResource, SizeOf(NetResource), 0); //初始化网络层次信息
NetResource.lpRemoteName := @GroupName[1]; //指定工作组名称
NetResource.dwDisplayType := RESOURCEDISPLAYTYPE_SERVER;//类型为服务器(工作组)
NetResource.dwUsage := RESOURCEUSAGE_CONTAINER;
NetResource.dwScope := RESOURCETYPE_DISK; //列举文件资源信息
Res := WNetOpenEnum( RESOURCE_GLOBALNET, RESOURCETYPE_DISK,RESOURCEUSAGE_CONTAINER, @NetResource,lphEnum);
If Res <> NO_ERROR Then Exit; //执行失败
While True Do //列举指定工作组的网络资源
Begin
Count := $FFFFFFFF; //不限资源数目
BufSize := 8192; //缓冲区大小设置为8K
GetMem(Buf, BufSize);//申请内存,用于获取工作组信息,获取计算机名称
Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
If Res = ERROR_NO_MORE_ITEMS Then break;//资源列举完毕
If (Res <> NO_ERROR) then Exit;//执行失败
Temp := TNetResourceArray(Buf);
For Ind := 0 to Count - 1 do//列举工作组的计算机名称
Begin
List.Add(Temp^.lpRemoteName);
Inc(Temp);
End;
End;
Res := WNetCloseEnum(lphEnum);//关闭一次列举
If Res <> NO_ERROR Then exit;//执行失败
Result:=True;
FreeMem(Buf);
end;
function getIp(name:string):string; //得到IP地址
var
WSAData:TWSAData;
HostEnt
HostEnt;
begin
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 GetName:string; //得到自己的计算机名
var
ComputerName: array[0..MAX_COMPUTERNAME_LENGTH+1] of char; // holds the name
Size: DWORD; // holds the size
begin
{initialize the computer name size variable}
Size := MAX_COMPUTERNAME_LENGTH+1;
{retrieve the computer name}
if GetComputerName(ComputerName, Size) then
result := StrPas(Computername);
end;
procedure TForm1.btn_findClick(Sender: TObject);
var
list,sl:Tstringlist;
i,j,k:integer;
begin
cmb_pc.Items.Clear;
try
k:=0;
list:=tstringlist.Create;
sl:=Tstringlist.create;
if getserverlist(sl) then
begin
for i:=0 to sl.count-1 do
if GetUsers(sl.Strings
,List) then
if List.count=0 then //工作组下没找到计算机
begin
statusbar1.SimpleText:='没有别的计算机在线!';
end
else
begin
for j:=0 to List.Count-1 do
begin
cmb_pc.Items.add(copy(List.strings[j],3,50));
inc(k);
end;
cmb_pc.Text:=copy(list.Strings[0],3,50);
statusbar1.SimpleText:='此时有'+inttostr(k)+'台计算机在线!';
end
else
cmb_pc.Items.add('没有找到工作组');
end;
finally
list:=tstringlist.Create;
sl:=tstringlist.Create;
end;
end;
wanglong0 (2003-01-09 15:49:00)
远帆,你好,是不是要引用别的什么单元啊?
远帆 (2003-01-09 16:25:00)
上面的代码是从以前写的一个Sock控件中拷下来的。
代码是网络方面的,所以要引用
winsock单元。
我现在不记得是否需要进行socket的初始化了。
初始化的相关代码是:
//初始化WinSock
if not WSAStartup(MakeWord(2, 0), WsData) = 0 then //初始化winsock
begin
Skt := SOCKET_ERROR;
DoOnError(0, Skt); //失败
Exit;
end;
WSACleanup; //释放Winsock
另外注意这个IP地址是integer类型的,是IP地址在计算机中的实际存储方式。
老赵 (2003-01-09 16:29:00)
看老贴吧,N多答案。
yanghai0437 (2003-01-09 16:39:00)
实现IP到域名的转换
Uses winsock
function GetDomainName(Ip:string):string;
var
pHHostent;
data:twsadata;
ii:dword;
begin
WSAStartup($101, Data);
ii:=inet_addr(pchar(ip));
pH:=gethostbyaddr(@ii,sizeof(ii),PF_INET);
if (ph<>nil) then
result:=pH.h_name
else
result:='';
WSACleanup;
end;